获取jqGrid中的所有行ID

如何获取网格中每一行的ID,甚至跨页面?

getDataIDsgetRowData只给出当前页面的ID。

谢谢!

只有当您具有本地网格( datatype:'local'或具有loadonce:trueloadonce:true 。 在这种情况下,所有页面的所有数据包含ID都已在本地。 在这种情况下,您可以使用_index参数,该参数通常与另一个更为已知的参数data一起使用。 同

 var idToDataIndex = $("#list").jqGrid('getGridParam','_index'); 

你会得到_index参数。 它是一个具有网格所有id的属性的对象。 所以你可以枚举id

 var id; for (id in idToDataIndex) { if (idToDataIndex.hasOwnProperty(id)) { // id is the rowid. // to get the data you can use // mydata[idToDataIndex[id]] where // var mydata = $("#list").jqGrid('getGridParam','data'); } } 

在更高版本的jqGrid中,他们推出了一个更适合这种情况的函数,因为它会考虑可能存在的任何工具栏过滤。 请参阅Oleg的例子。 因此,如果您有一个jqGrid(loadonce:true和/或数据类型:local),则以下内容将返回与当前过滤匹配的所有行ID(显示在当前页面及更高版本中)。

 var allIdsWithFiltering = grid.jqGrid('getGridParam', 'lastSelectedData'); 

与原始答案不同,它返回一个普通数组,它返回一个具有必须枚举的属性的对象。

还有另一种方法可以在jqgrid上的旧版本中获取此数据:

 gRowNum = grid.jqGrid('getGridParam','rowNum'); grid.setGridParam({rowNum: '9999'}); grid.trigger("reloadGrid"); myList = grid.jqGrid('getDataIDs'); grid.setGridParam({rowNum: gRowNum}); grid.trigger("reloadGrid"); 

考虑到自IE9以来支持Object.keys , 如果你只需要ID ,现在我会使用:

 var idToDataIndex = $("#list").jqGrid('getGridParam','_index'); var ids = Object.keys(idToDataIndex); 
 $(function () { "use strict"; $("#list").jqGrid({ colModel: [ { name: "name", label: "Client", width: 53 }, { name: "invdate", label: "Date", width: 90, align: "center", sorttype: "date", formatter: "date", formatoptions: { newformat: "dMY" }, searchoptions: { sopt: ["eq"] } }, { name: "amount", label: "Amount", width: 65, template: "number" }, { name: "tax", label: "Tax", width: 41, template: "number" }, { name: "total", label: "Total", width: 51, template: "number" }, { name: "closed", label: "Closed", width: 59, template: "booleanCheckbox", firstsortorder: "desc" }, { name: "ship_via", label: "Shipped via", width: 87, align: "center", formatter: "select", formatoptions: { value: "FE:FedEx;TN:TNT;DH:DHL", defaultValue: "DH" }, stype: "select", searchoptions: { value: ":Any;FE:FedEx;TN:TNT;DH:DHL" } } ], data: [ { id: "10", invdate: "2015-10-01", name: "test", amount: "" }, { id: "20", invdate: "2015-09-01", name: "test2", amount: "300.00", tax: "20.00", closed: false, ship_via: "DH", total: "320.00" }, { id: "30", invdate: "2015-09-01", name: "test3", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, { id: "40", invdate: "2015-10-04", name: "test4", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, { id: "50", invdate: "2015-10-31", name: "test5", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, { id: "60", invdate: "2015-09-06", name: "test6", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, { id: "70", invdate: "2015-10-04", name: "test7", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, { id: "80", invdate: "2015-10-03", name: "test8", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, { id: "90", invdate: "2015-09-01", name: "test9", amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00" }, { id: "100", invdate: "2015-09-08", name: "test10", amount: "500.00", tax: "30.00", closed: true, ship_via: "TN", total: "530.00" }, { id: "110", invdate: "2015-09-08", name: "test11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }, { id: "120", invdate: "2015-09-10", name: "test12", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" } ], iconSet: "fontAwesome", idPrefix: "", rownumbers: true, sortname: "invdate", sortorder: "desc", threeStateSort: true, sortIconsBeforeText: true, headertitles: true, toppager: true, pager: true, rowNum: 5, viewrecords: true, searching: { defaultSearch: "cn" }, caption: "The grid, which demonstrates formatters, templates and the pager" }).jqGrid("filterToolbar"); }); $('#btnGetAllIDs').click(function() { var idToDataIndex = $("#list").jqGrid('getGridParam','_index'); var ids = Object.keys(idToDataIndex); console.log(ids); });