如何导出所有只有可见列的jqgrid数据,无论分页如何 [英] how to export all jqgrid data which should have only visible columns irrespective of paging

查看:286
本文介绍了如何导出所有只有可见列的jqgrid数据,无论分页如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $( #listTableSupply)。jqGrid(getGridParam,data); 

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

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

我只收到第1页的记录。



我需要知道有没有什么办法,我必须得到所有的数据与可见列,无论分页。

解决方案

使用 getGridParam 数据是正确的方式。如果您需要从项目或数组中删除某些属性,那么您应该对数组进行深入复制(通过使用 $。extends(true,{},data) )并从数组的每个项目中删除不需要的属性。



或者,您可以将新隐藏列中的所有属性复制到新数组中。代码可以是以下内容:

  //获取对网格的所有参数的引用
var p = $( #listTableSupply)的jqGrid( getGridParam)。

//将所有非隐藏列的列表保存为辅助对象的属性
var colNames = {},i,cm; (i = 0; i< p.colModel.length; i ++)
{
cm = p.colModel [i];
if(cm.hidden!== true){
colNames [cm.name] = true;
}
}
//我们现在有colNames对象与属性,
//对应于网格的非隐藏列

//复制p.data只包括非隐藏列
var newData = new Array(p.data.length),prop,newItem,item; (i = 0; i< p.data.length; i ++)的
{
item = p.data [i];
newItem = {};
for(prop in item){
if(item.hasOwnProperty(prop)&& colNames [prop]){
//仅填充非隐藏列的属性
newItem [prop] = item [prop];
}
}
newData [i] = newItem;
}

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



更新:我创建了演示,这演示了使用 lastSelectedData 而不是数据。它填充了生成的数组 newData 网格的过滤后的项目,只包括可见的列。您可以过滤数据,然后单击显示过滤和排序数据的非隐藏字段按钮。演示填充 newData 数组并显示。我在中使用以下代码点击处理程序:

  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); (i = 0; i< p.lastSelectedData.length; i ++)
{
item = p.lastSelectedData [i];
newItem = {};
for(prop in item){
if(item.hasOwnProperty(prop)&& colNames [prop]){
//仅填充非隐藏列的属性
newItem [prop] = item [prop];
}
}
newData [i] = newItem;
}
alert(JSON.stringify(newData));
}


I want to know whether there is any method to get all jqGrid data of visible columns irrespective of paging.

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

But it shows all json data which I have passed to jqgrid. As I use paging in jqgrid if I use

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

I get only the records from 1st Page.

I need to know is there any way that I have to get all data with visible columns irrespective of paging.

解决方案

The usage of getGridParam with data is the correct way. If you need to remove some properties from the item or the array then you should make deep copy of the array (by usage of $.extend(true, {}, data)) and remove unneeded properties from every item of the array.

Alternatively you can copy all properties with non-hidden columns in new array. The code could be about the following:

// 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;
}

I don't tested the above code, but I hope it fill newData array with the data, which you need.

UPDATED: I created the demo for you, which demonstrates the usage of lastSelectedData instead of data. It fills in the resulting array newData the filtered items of the grid, including only visible columns. You can filter the data and then click on the button "Show non-hidden fields of filtered and sorted data". The demo fills the newData array and display it. I used the following code inside of click handler:

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数据,无论分页如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆