首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网格单元中jqwidget dropdownlist的数据库绑定

网格单元中jqwidget dropdownlist的数据库绑定
EN

Stack Overflow用户
提问于 2012-07-10 15:28:13
回答 2查看 9.7K关注 0票数 1

在jqwidget网格编辑示例中(在jqxGrid左侧菜单下,打开cellediting /Editing),数据是在客户端生成的。如何将dropdownlist绑定到asp .net MVC3项目中的数据库?(您可以在演示选项卡中的产品栏下看到dropdownlist )

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-12 17:27:47

使用数据库初始化dropdownlist时,应将其绑定到数据源(或数据适配器),并设置selectedIndex。然后,对于行更新,所选值应该保留在select上。

列定义可能如下所示:

代码语言:javascript
复制
{ 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的选定值。它可能是这样的:

代码语言:javascript
复制
if (selectedUrunId != undefined && selectedUrunId != -1) {
                    rowdata.UrunId = selectedUrunId;
                    selectedUrunId = -1;
                }

此场景的整体场景为:

代码语言:javascript
复制
        // 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');
                      });
                  }
              }]
        });
票数 3
EN

Stack Overflow用户

发布于 2012-07-10 18:10:23

您可以使用名为“createeditor”的函数并在其中初始化DropDownList。

列定义:

代码语言:javascript
复制
{ text: 'Proyecto', columntype: 'dropdownlist', datafield: 'jobid', width: 10,
                      createeditor: function (row, cellvalue, editor) {
                          editor.jqxDropDownList({ displayMember: 'displaylabel', valueMember: 'catalogvalue', source: dropdownListAdapter });
                      }
}

DropDownList的数据适配器可以使用类似的代码进行初始化:

代码语言:javascript
复制
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'}
);   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11408519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档