【问题标题】:JQGrid getRowData for all rows in certain orderJQGrid getRowData 按特定顺序获取所有行
【发布时间】:2013-09-03 06:12:51
【问题描述】:
我想按特定顺序在 javascript 变量中获取 JQGrid 的所有行。
我能够通过以下方式获取所有行的数据:
var rowData = $("#gridId").jqGrid("getRowData");
但我希望行的显示顺序与我在网格中输入的顺序相同。
我正在尝试创建一个表并通过网格获取其列详细信息。所以我要求列的顺序保持不变。有什么建议吗?
【问题讨论】:
标签:
javascript
jquery
jqgrid
【解决方案1】:
您可以将 jqgrid 行数据显式推送到数组,然后将其转换为 json 字符串。
为此,您可以使用下面给出的函数来遍历 jqgrid 行:(来自Oleg's answer)
var gridData= {};
var $grid = jQuery("#gridId"), rows = $grid[0].rows, cRows = rows.length,
iRow, rowId, row, cellsOfRow;
for (iRow = 0; iRow < cRows; iRow++) {
row = rows[iRow];
if ($(row).hasClass("jqgrow")) {
cellsOfRow = row.cells;
// row represent the row with cellsOfRow.length cells.
// So one can use for loop with iCol between 0 and cellsOfRow.length
// cellsOfRow[iCol] represent the cell with the index iCol
// and $(cellsOfRow[iCol]).text() - the text of the cell
}
}
在循环中,您可以将单元格数据推送到数组中。
gridData['column_index1'] = $(cellsOfRow[0]).text();
gridData['column_index2'] = $(cellsOfRow[1]).text();
.............................................
最后将gridData转换为json字符串使用
var result = JSON.stringify(gridData) ;