我想将jqgrid与嵌套子网格一起使用。但是,我发现的唯一示例是使用一个url负载数据调用和单独的调用来填充主网格,以填充子网格。
我的子网格数据以嵌套文档的形式存在于一个JSON结构中,如下面的代码片段所示(我希望各章显示为本书的子网格,而文件则应作为各章中的子网格)。
我可以使用jqgrid从嵌套的JSON文档创建子网格吗?
{ _id: {"509403957ae7d3929edcb812"}, name: {"MYBOOK"}, layout: { chapters [ { name: "myfirstchapter", sequence: 1, title: "My First Chapter", files: [ { filetype: "tex", name: "myfirstfile" }, { filetype: "tmpl", name: "myfileb", } ], }, { name: "mysecondchapter", sequence: 2, title: "My Second Chapter", files: [ { filetype: "tex", name: "myintro" }, { filetype: "tex", name: "myfilec", } ], ], } }
我进行了演示,演示了如何执行此操作:
该演示基于此处描述的思想以及内部data选项以 未修改 形式保存输入数据的事实。因此,您无需创建一些隐藏的列即可保存与该行关联的其他信息。有关更多详细信息,。我idPrefix强烈建议您另外在子网格中使用选项。
data
idPrefix
下面在演示中使用的代码:
var myData = { _id: "509403957ae7d3929edcb812", name: "MYBOOK", layout: { chapters: [ { name: "myfirstchapter", sequence: 1, title: "My First Chapter", files: [ { filetype: "tex", name: "myfirstfile" }, { filetype: "tmpl", name: "myfileb" } ] }, { name: "mysecondchapter", sequence: 2, title: "My Second Chapter", files: [ { filetype: "tex", name: "myintro" }, { filetype: "tex", name: "myfilec" } ] } ] } }, $grid = $("#list"); $grid.jqGrid({ datatype: "local", data: myData.layout.chapters, colNames: ["Sequence", "Name", "Title"], colModel: [ {name: "sequence", width: 65, key: true }, {name: "name", width: 150 }, {name: "title", width: 150} ], rowNum: 10, rowList: [5, 10, 20], pager: "#pager", gridview: true, ignoreCase: true, rownumbers: true, sortname: "sequence", viewrecords: true, height: "100%", subGrid: true, subGridRowExpanded: function (subgridId, rowid) { var subgridTableId = subgridId + "_t"; $("#" + subgridId).html("<table id='" + subgridTableId + "'></table>"); $("#" + subgridTableId).jqGrid({ datatype: "local", data: $(this).jqGrid("getLocalRow", rowid).files, colNames: ["Name", "Filetype"], colModel: [ {name: "name", width: 130, key: true}, {name: "filetype", width: 130} ], height: "100%", rowNum: 10, sortname: "name", idPrefix: "s_" + rowid + "_" }); } }); $grid.jqGrid("navGrid", "#pager", {add: false, edit: false, del: false});
在上面的代码中,我修复了您发布的数据中的一些语法错误。