【问题标题】:how to export all jqgrid data which should have only visible columns irrespective of paging如何导出所有应该只有可见列而不考虑分页的jqgrid数据
【发布时间】:2017-01-03 11:00:02
【问题描述】:

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

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

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

$('#list').jqGrid('getRowData');

我只得到第一页的记录。

我需要知道是否有任何方法可以获取所有带有可见列的数据,而不管分页。

【问题讨论】:

    标签: jquery json jqgrid export free-jqgrid


    【解决方案1】:

    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 网格的过滤项,仅包括可见列。您可以过滤数据,然后单击“显示过滤和排序数据的非隐藏字段”按钮。该演示填充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));
    }
    

    【讨论】:

    • 谢谢奥列格。当我在 jqgrid 中过滤我的数据时它会起作用吗?因为如果完成任何过滤,我只需要显示过滤数据
    • @user1268130:不客气!如果你需要使用过滤数据而不是完整数据,那么你应该从lastSelectedData而不是data获取数据(见the old demothe answerthis old one
    • 谢谢奥列格。现在会检查出来。 :)
    • newdata 提供所有列,我只需要可见列。顺便说一下,它应该是 p.data.length
    • @user1268130:我创建了演示,它演示了您需要什么。请参阅我的回答的已更新部分。
    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多