简体   繁体   English

jqGrid('getGridParam','colNames')异常行为

[英]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). 当我多次使用此功能时,问题就来了(将其导出为ex​​cel)。 The next time I export, the getGridParam function actually returns the spliced array of column names rather than the actual ones. 下次我导出时,getGridParam函数实际上返回的是拼接的列名数组,而不是实际的。 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. columnTitles2的值作为拼接数组返回。 It might be something completely stupid, but what am I missing here? 这可能是完全愚蠢的,但是我在这里想念的是什么?

The method getGridParam returns the reference of internal parameters used by jqGrid. 该方法getGridParam返回由使用的jqGrid内部参数的参考 You should be careful if you work with arrays or objects, colNames or colNames for example. 如果使用数组或对象(例如colNamescolNames ,则应格外小心。 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: 它需要修改的数组你的目的 ,但你不想改变你的jqGrid应首先使阵列的复制值,然后修改副本:

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

I used slice to make the copy of internal colNames used by jqGrid. 我用slice制作了colNames使用的内部colNames的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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