jqGrid('getGridParam','colNames')异常行为 [英] jqGrid('getGridParam','colNames') odd behavior

查看:95
本文介绍了jqGrid('getGridParam','colNames')异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此函数返回网格的列名可以正常工作.拼接返回的数组时会出现问题.

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

当我多次使用此功能时,问题就来了(将其导出到excel).下次我导出时,getGridParam函数实际上返回的是拼接的列名数组,而不是实际的列名数组.好像是通过引用或其他方式传递.

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

columnTitles2的值作为拼接数组返回.这可能是完全愚蠢的,但是我在这里想念的是什么?

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

推荐答案

方法getGridParam返回jqGrid使用的内部参数的引用引用.如果使用数组或对象(例如colNamescolNames),则应格外小心.您需要根据自己的目的修改数组 ,但是您不想更改jqGrid中的值,则应先复制副本,然后修改副本:

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

我用slice制作了jqGrid使用的内部colNames的副本.

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

这篇关于jqGrid('getGridParam','colNames')异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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