简体   繁体   中英

jqGrid('getGridParam','colNames') odd behavior

Using this function to return the column names of the grid works fine. The issue comes when splicing the array that it returns.

The grid includes a checkbox as the first column so I want to remove that from the array. Here is that code.

var columnTitles = $(table).getGridParam('colNames'); 
columnTitles.splice(0,1);

The problem comes when I use this function multiple times (it's exporting to excel). The next time I export, the getGridParam function actually returns the spliced array of column names rather than the actual ones. It's as if it's being passed by reference or something.

Further proof that it's doing that and I don't just have a problem with a global variable or something...if I do the following code:

var columnTitles = $(table).getGridParam('colNames'); 
var columnTitles2 = $(table).getGridParam('colNames'); 
columnTitles.splice(0,1); 
console.log(columnTitles2); 

The value of columnTitles2 comes back as the spliced array. It might be something completely stupid, but what am I missing here?

The method getGridParam returns the reference of internal parameters used by jqGrid. You should be careful if you work with arrays or objects, colNames or colNames for example. It you need to modify the arrays for your purpose , but you don't want to change the values in jqGrid you should first make copy of the arrays and then modifies the copy:

var columnTitles = $(table).jqGrid("getGridParam", "colNames").slice(); 
columnTitles.splice(0,1);

I used slice to make the copy of internal colNames used by jqGrid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM