在jqwidget网格编辑示例中(在jqxGrid左侧菜单下,打开cellediting /Editing),数据是在客户端生成的。如何将dropdownlist绑定到asp .net MVC3项目中的数据库?(您可以在演示选项卡中的产品栏下看到dropdownlist )
发布于 2012-07-12 17:27:47
使用数据库初始化dropdownlist时,应将其绑定到数据源(或数据适配器),并设置selectedIndex。然后,对于行更新,所选值应该保留在select上。
列定义可能如下所示:
{ text: 'Urun', columntype: 'dropdownlist', datafield: 'UrunAdi', width: 177,
initeditor: function (row, cellvalue, editor) {
var urunId = $('#jqxgrid').jqxGrid('getcellvalue', row, "UrunId");
editor.jqxDropDownList({ displayMember: 'UrunAdi', source: dropdownListAdapter, selectedIndex: urunId });
$(document).on('select', editor, function (event) {
selectedUrunId = editor.jqxDropDownList('getSelectedIndex');
});
}
}变量"selectedUrunId“应该是全局定义的,可以在初始化jqxgrid之前像var selectedUrunId = -1;一样定义。然后在updaterow定义中(它在source定义中),应该使用dropdown的选定值。它可能是这样的:
if (selectedUrunId != undefined && selectedUrunId != -1) {
rowdata.UrunId = selectedUrunId;
selectedUrunId = -1;
}此场景的整体场景为:
// prepare the data
var gridSource = {
datatype: "json",
datafields: [{ name: 'KargoId' }, { name: 'UrunAdi' }, { name: 'UrunId', type: 'int' }],
url: 'BindGrid',
updaterow: function (rowid, rowdata) {
// synchronize with the server - send update command
if (selectedUrunId != undefined && selectedUrunId != -1) {
rowdata.UrunId = selectedUrunId;
selectedUrunId = -1;
}
var data = $.param(rowdata);
$.ajax({
dataType: 'json',
url: 'UpdateEditGrid',
data: data,
success: function (data, status, xhr) {
gridDataAdapter.dataBind();
},
error: function (xhr, status, error) {
alert(JSON.stringify(xhr));
}
});
}
};
var gridDataAdapter = new $.jqx.dataAdapter(gridSource);
var dropdownSource = {
datatype: "json",
datafields: [{ name: 'UrunId' }, { name: 'UrunAdi'}],
url: 'BindDropdown'
};
var selectedUrunId = -1;
var dropdownListAdapter = new $.jqx.dataAdapter(dropdownSource);
// initialize jqxGrid
$("#jqxgrid").jqxGrid(
{
width: 670,
source: gridDataAdapter,
editable: true,
theme: theme,
selectionmode: 'singlecell',
columns: [
{ text: '#', datafield: 'KargoId', width: 40 },
{ text: 'Urun', columntype: 'dropdownlist', datafield: 'UrunAdi', width: 177,
initeditor: function (row, cellvalue, editor) {
var urunId = $('#jqxgrid').jqxGrid('getcellvalue', row, "UrunId");
editor.jqxDropDownList({ displayMember: 'UrunAdi', source: dropdownListAdapter, selectedIndex: urunId });
$(document).on('select', editor, function (event) {
selectedUrunId = editor.jqxDropDownList('getSelectedIndex');
});
}
}]
});发布于 2012-07-10 18:10:23
您可以使用名为“createeditor”的函数并在其中初始化DropDownList。
列定义:
{ text: 'Proyecto', columntype: 'dropdownlist', datafield: 'jobid', width: 10,
createeditor: function (row, cellvalue, editor) {
editor.jqxDropDownList({ displayMember: 'displaylabel', valueMember: 'catalogvalue', source: dropdownListAdapter });
}
}DropDownList的数据适配器可以使用类似的代码进行初始化:
source = {
datatype: "xml",
datafields: [
{ name: 'CompanyName' },
{ name: 'ContactName' },
{ name: 'ContactTitle' },
{ name: 'City' },
{ name: 'Country' },
{ name: 'Address' }
],
async: false,
record: 'Table',
url: 'Default.aspx/GetCustomers',
};
var dropdownListAdapter = new $.jqx.dataAdapter(source,
{ contentType: 'application/json; charset=utf-8'}
); https://stackoverflow.com/questions/11408519
复制相似问题