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