javascript jqgrid 自定义格式化程序:自定义格式化程序始终返回网格的最后一行。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9456029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jqgrid custom formatter: The custom formatter always returns the last row of the grid. Why?
提问by Larry
UpdatedI have problems to point data with the custom formatter.
更新我在使用自定义格式化程序指向数据时遇到问题。
I am using jqgrid's custom formatter.
我正在使用 jqgrid 的自定义格式化程序。
function myformatter ( cellvalue, options, rowObject )
{
....
Now, my custom formatter seems to point always on the last row of the grid. In fact, if I get rowObject[0]
, for example, I have the value of the [column 0, last row] of my grid. Why?
现在,我的自定义格式化程序似乎总是指向grid 的最后一行。事实上,rowObject[0]
例如,如果我得到,我就有了网格的 [column 0, last row] 的值。为什么?
The grid's data is correctly compiled and I already checked Json object content.
网格的数据已正确编译,我已经检查了 Json 对象内容。
Here's my custom formatter:
这是我的自定义格式化程序:
......
{ name: 'act', index: 'Detail', width: 50, sortable: false, search: false,
formatter: function (cellvalue, options, rowObject) {
i = options.rowId;
var tst = '<a class="nau" name="nau" onClick="alert(i);return false;" href="#"></a>';
var det = '<a class="det" name="det" onClick="alert(this.name);return false;" href="#"></a>';
return tst + det;
}
}
....
Update
更新
I noticed that the formatter works fine if I return the string I want directly (for example return rowObject[0]
works fine), while I have problems when I use variables. Moreover, if I try to do onclick=alert(rowObject[0])
I get an exception saying rowObject
does not exists. I think this is the problem: if I set t = rowObject[0]
, then the formatter use t
as static variable instead of updating it for each row. The same if I set i = options.rowId
, where i
remains static...WHY? What I should do?
我注意到如果我直接返回我想要的字符串(例如return rowObject[0]
工作正常),格式化程序工作正常,而我在使用 variables 时遇到问题。此外,如果我尝试这样做,onclick=alert(rowObject[0])
我会得到一个异常rowObject
,说不存在。我认为这是问题所在:如果我设置了t = rowObject[0]
,则格式化程序将t
用作静态变量,而不是为每一行更新它。如果我设置相同i = options.rowId
,哪里i
保持静态......为什么?我该做什么?
回答by Larry
I succeed to get it work...I must say that I feel a little embarrassed ... it was a stupid mistake. I hope we can still help some inexperienced like me, anyway. I did not put variables outside of quotes:
我成功地让它工作......我必须说我感到有点尴尬......这是一个愚蠢的错误。无论如何,我希望我们仍然可以帮助一些像我这样没有经验的人。我没有将变量放在引号之外:
......
{ name: 'act', index: 'Detail', width: 50, sortable: false, search: false,
formatter: function (cellvalue, options, rowObject) {
i = options.rowId;
var tst = '<a class="nau" name="nau" onClick="alert('+i+');return false;" href="#"></a>';
var det = '<a class="det" name="det" onClick="alert(this.name);return false;" href="#"></a>';
return tst + det;
}
}
....
I quote the precious help from @Oleg: "The code in onclick
will be executed separately so you have to use values of the variables and not the names. For example 'onclick="alert(rowObject[0]);return false;"'
will produce error because global array rowObject is not exist. You have to change the code to use 'onclick="alert(' + rowObject[0] + ');return false;"'
which will place the value of rowObject[0]
in the code."
我引用了@Oleg 的宝贵帮助:“中的代码onclick
将单独执行,因此您必须使用变量的值而不是名称。例如,'onclick="alert(rowObject[0]);return false;"'
由于全局数组 rowObject 不存在,会产生错误。您必须更改代码使用'onclick="alert(' + rowObject[0] + ');return false;"'
哪个将rowObject[0]
在代码中放置的值。”
回答by Oleg
I suppose that you have some problems in fillingof the grid. If options.rowId
is the same for all rows then you fill the grid with the wrong data where the id
is always 1.
我想您在填充网格时遇到了一些问题。如果options.rowId
所有行都相同,那么你用错误的数据填充网格,其中id
总是 1。
If you will don't localize the wrong place in your code you should include the code and the test data which you use.
如果您不想在代码中定位错误的位置,则应该包含您使用的代码和测试数据。
Moreover you should use onclickinstead of onClick
. Your current code can work now, but it will be not more work it your would change the DOCTYPE.
此外,您应该使用onclick而不是onClick
. 您当前的代码现在可以工作了,但是如果您更改 DOCTYPE,就不会再工作了。