如何导出所有应该只有可见列而不考虑分页的jqgrid数据

how to export all jqgrid data which should have only visible columns irrespective of paging

我想知道是否有任何方法可以不分页地获取可见列的所有jqGrid数据。

$("#listTableSupply").jqGrid("getGridParam", "data");

但它显示了我传递给 jqgrid 的所有 json 数据。如果我使用

在 jqgrid 中使用分页
$('#list').jqGrid('getRowData');

我只得到第一页的记录。

我需要知道有什么方法可以让我获取所有包含可见列的数据,而不管分页。

getGridParamdata 的用法是正确的。如果您需要从项目或数组中删除一些属性,那么您应该对数组进行深度复制(通过使用 $.extend(true, {}, data))并从数组的每个项目中删除不需要的属性。

或者,您可以在新数组中复制所有具有非隐藏列的属性。代码可能如下所示:

// get the reference to all parameters of the grid
var p = $("#listTableSupply").jqGrid("getGridParam");

// save the list of all non-hidden columns as properties of helper object
var colNames = {}, i, cm;
for (i = 0; i < p.colModel.length; i++) {
    cm = p.colModel[i];
    if (cm.hidden !== true) {
        colNames[cm.name] = true;
    }
}
// We have now colNames object with properties,
// which correspond to non-hidden columns of the grid

// Make copy of p.data including only non-hidden columns
var newData = new Array(p.data.length), prop, newItem, item;
for (i = 0; i < p.data.length; i++) {
    item = p.data[i];
    newItem = {};
    for (prop in item) {
        if (item.hasOwnProperty(prop) && colNames[prop]) {
            // fill only properties of non-hidden columns
            newItem[prop] = item[prop];
        }
    }
    newData[i] = newItem;
}

我没有测试上面的代码,但我希望它用你需要的数据填充 newData 数组。

更新: 我为您创建了 the demo,它演示了 lastSelectedData 而非 data 的用法。它在生成的数组 newData 中填充网格的筛选项,仅包括可见列。您可以过滤数据,然后单击按钮 "Show non-hidden fields of filtered and sorted data"。该演示填充 newData 数组并显示它。我在 click 处理程序中使用了以下代码:

var p = $grid.jqGrid("getGridParam"), filteredData = p.lastSelectedData,
    idName = p.localReader.id, i, cm, prop, newItem, item,
    colNames = {}, newData;
if (p.lastSelectedData.length > 0) {
    for (i = 0; i < p.colModel.length; i++) {
        cm = p.colModel[i];
        if (cm.hidden !== true && $.inArray(cm.name, ["rn", "cb", "subgrid"]) < 0) {
            colNames[cm.name] = true;
        }
    }
    colNames[idName] = true;
    newData = new Array(p.lastSelectedData.length);
    for (i = 0; i < p.lastSelectedData.length; i++) {
        item = p.lastSelectedData[i];
        newItem = {};
        for (prop in item) {
            if (item.hasOwnProperty(prop) && colNames[prop]) {
                // fill only properties of non-hidden columns
                newItem[prop] = item[prop];
            }
        }
        newData[i] = newItem;
    }
    alert(JSON.stringify(newData));
}