jquery - 使用客户端数据类型时向 jqgrid 子网格添加数据

标签 jquery jqgrid

当父jqgrid的数据类型为clientSide类型时,是否可以向子网格添加数据?

我可以使用以下方法向父 jqgrid 添加一行:

jQuery("#myId").addRowData(0, someRowData);

但我不确定如何将数据添加到子网格。有什么想法吗?

最佳答案

我无法确定如何实际将数据添加到 jqGrid 的子网格,但我能够将数据添加到另一个网格内的网格。

我的主网格看起来像:

$("#" + tblNm).jqGrid({
                datatype: "local",
                colNames: ['id', 'Last Name', 'First Name', 'Address'],
                colModel: [{
                    name: 'id',
                    index: 'id',
                    width: 75,
                    sortable: false
                }, {
                    name: 'lastNm',
                    index: 'lastNm',
                    width: 90,
                    sortable: false
                }, {
                    name: 'firstNm',
                    index: 'firstNm',
                    width: 100,
                    sortable: false
                }, {
                    name: 'address',
                    index: 'address',
                    width: 200,
                    align: "left",
                    sortable: false
                }],
                width: "100%",
                height: "100%",
                caption: "Clients",
                jsonReader: {
                    root: "clients",
                    page: "currpage",
                    total: "totalpages",
                    records: "totalrecords",
                    repeatitems: false,
                    id: "id"
                },
                subGrid: true,
                subGridRowExpanded: function(subgrid_id, row_id){
                    console.debug("SubGrid_ID:  " + subgrid_id +
                    "   / row_id:  " +
                    row_id);
                    var subgrid_table_id, pager_id;
                    subgrid_table_id = "mySubGridName" + row_id;
                    pager_id = "p_" + subgrid_table_id;
                    $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "'></table>");
                    jQuery("#" + subgrid_table_id).jqGrid({
                        datatype: "local",
                        colNames: ['Order Id', 'Item Name', 'Cost'],
                        colModel: [{
                            name: "ordId",
                            index: "ordId",
                            width: 20,
                            key: true
                        }, {
                            name: "itemName",
                            index: "itemName",
                            width: 130
                        }, {
                            name: "cost",
                            index: "cost",
                            width: 200,
                            align: "left"
                        }],
                        rowNum: 20,
                        caption: "Orders",
                        height: '100%',
                        width: '100%',
                        jsonReader: {
                            root: "orders",
                            page: "currpage",
                            total: "totalpages",
                            records: "totalrecords",
                            repeatitems: false,
                            id: "num"
                        }
                    });
                }
            });

我通过执行以下操作来加载主网格:

$("#" + tblNm)[0].addJSONData(wrapper);

然后,当我获取子网格数据时,我会执行以下操作:

 // click on the subgrid so that the table is added to the DOM
 // I think there are better ways of doing this... you'll want to look @ the jqGrid documentation
 $("#" + masterTblId + " tr[id='1'] td[role='grid'] a span").click();
 // add the data to the subgrid
 $("#" + subGridId)[0].addJSONData(wrapper);

最重要的部分是在 jqGrid 的 subgridexpanded 属性中定义代码,然后能够强制显示子网格。

此方法的唯一问题是,如果用户单击子网格区域,则它会被切换,当他们再次单击它时,它不会正确显示。我已向社区询问如何解决此问题:

Subgrid Caching or Stopping Subgrid data from being removed (jqGrid)

关于jquery - 使用客户端数据类型时向 jqgrid 子网格添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/773128/

相关文章:

asp.net-mvc - 如何为 jqGrid 启用 excel 导出按钮

javascript - 在 iframe 中访问 jqgrid

javascript - 删除属性为空的 TD

javascript - 单击时部分淡入,然后 - 淡出

javascript - 我的平滑滚动 js 不会触发动画?

javascript - 将鼠标悬停在 TD 中的某些元素上时,jqGrid 在 IE 中闪烁

css - 将 CSS 应用于 JQGrid 中的表格单元格(td)?

jquery - 如何在自动调整大小时设置日期搜索工具栏字段宽度

javascript - 将 CSS 应用于脚本

javascript - 无法弄清楚正则表达式匹配,我还有多远?