jqGrid:无法在子网格中获取数据

我正在使用jqGrid 4.4.1和subGrid。

我正在提取不同类型的数据,但对于此示例数据,单击“展开”时我无法获得subGrid。

这是示例代码。

   SubGrid Real Data      .ui-jqgrid .ui-subgrid td.subgrid-data { border-top: 0 none !important; border-right: 0 none !important; border-bottom: 0 none !important; } .ui-jqgrid .ui-subgrid span.ui-icon ui-icon-carat-1-sw { background-color: #FFFFFF !important; background-image: none !important; border: 0px 0px 1px 1px; }      $.jgrid.no_legacy_api = true; $.jgrid.useJSON = true;    //<![CDATA[ /*global $ */ /*jslint browser: true, eqeq: true, plusplus: true */ $(function () { "use strict"; var colModelData =[ {"width":"300","name":"itemName","editable":false} ], colNamesArray = ["Name"], subColModel = [ {"width":"300","name":"itemName","editable":false} ], myData = [{"id":"metlab panel","itemName":"Metlab Panel", "subGridData":[ {"itemName":"CMP BILIRUBIN,TOTAL"}, {"itemName":"CMP CALCIUM"}, {"itemName":"CMP CREATININE"}, {"itemName":"CMP POTASSIUM"}, {"itemName":"CMP PROTEIN,TOTAL"}, {"itemName":"CMP SODIUM"}, {"itemName":"CMP GLUCOSE"}] }]; $("#list").jqGrid({ datatype: "local", data: myData, colNames: colNamesArray, colModel: colModelData, gridview: true, height: "100%", width: "100%", caption: "Create subgrid from local data", subGrid: true, subGridOptions: { reloadOnExpand: false, selectOnExpand: false }, subGridRowExpanded: function (subgridDivId, parentRowId) { var $subgrid = $("
"); $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId)); var data = $(this).jqGrid("getLocalRow", parentRowId); console.log(data.subGridData); // I can see data $subgrid.jqGrid({ datatype: "local", data: data.subGridData, colModel:subColModel, height: "100%", width: "100%", autoencode: true, gridview: true, rowNum: 200 }); $subgrid.closest("div.ui-jqgrid-view") .children("div.ui-jqgrid-hdiv").hide(); } }); }); //]]>

输出:

产量

使用id你的问题是不正确的。

第一个错误是使用具有空白的id 。 如果用"id":"metlab panel"代替"id":"metlab panel" "id":"metlabpanel"就会看到子网格。

下一个问题是subgrid的数据没有id作为all。 在jqGrid将使用值1,2,3,…作为rowid的情况下。 如果一个人扩展至少两个子网格,你将有id重复,这将是一个问题。 要解决此问题,可以使用idPrefix作为基于父级rowid创建的子网格。 例如

 idPrefix: "s_" + parentRowId + "_" 

无论如何,您应该始终考虑为主网格或子网格的每一行提供唯一的rowid。

修改后的演示正确显示子网格

在此处输入图像描述

最好的方法是为您创建的每个行或网格或子网格填充id属性。 如果您不确定要分配哪个值,则只需调用$.jgrid.randId() ,它将提供可用作id安全唯一值。