动态列的JQGrid:cellattr不起作用 [英] JQGrid with dynamic column: cellattr does not work
问题描述
我已经用这种方法(ajax调用)构建了JQGrid表的动态列:
I have built my JQGrid table with dynamic column in this way (ajax call) :
$.ajax(
{
type: "POST",
url: "Scripts/GetSummaryInformation.php",
data: { domain: "<?php echo $domain; ?>", etc. },
dataType: "json",
success: function(result)
{
var colD = result.gridModel;
var colN = result.colNames;
var colM = result.colModel;
var colSpan = result.colSpan;
jQuery("#FinalRatingList<?php echo $domain; ?><?php echo $companyID; ?>").jqGrid({
jsonReader : { repeatitems: false, root:"dataset", cell: "cell", id: "0" },
url: 'Scripts/GetSummaryInformation.php',
datatype: 'json',
mtype: 'POST',
postData : { domain: "<?php echo $domain; ?>", etc.},
datastr : colD,
colNames:colN,
colModel :colM,
height: 'auto',
pager: jQuery('#FinalRatingListPager'),
caption:"Final Rating",
loadComplete: function(data){
console.info("FinalRatingList Success");
notyMsg ("loadComplete", "info");
},
...
它工作正常:)
当我在PHP文件中创建一个列,就像使用 cellattr
When I create a column in the PHP file like using the cellattr
$response->colModel[$colMod++] = array ('name'=>'DIR'.$projectName, 'index'=>'DIR'.$projectName, 'width'=>'40', "cellattr"=>"rowSetting" );
根本没有任何效果。
即使我把代码直接放在cellattr中,就像这样
there is absolutely no effect at all. Even when I put code directly in the cellattr like this
"cellattr"=>"function( rowId, value, rowObject, colModel, arrData) { return ' style=\"background:orange\"'}" );
有没有人面对或知道这个问题的解决方案?
Does anyone have faced or know the solution to this problem?
提前感谢您的协作
更新
谢谢奥列格它对于cellattr和模板非常有用(这是一个非常好的建议)。
Thanks Oleg. It worked greatly for cellattr and for template (which is a really good advice).
对于那些对这些感兴趣的人来说,代码是:
For those who are interested here is the code:
var rowSetting = function (rowId, val, rawObject, cm) {
return 'style="background-color: orange;';
};
var cellattrMapping = {
"rowTemplate": rowTemplate
};
var rowTemplate = {
width:120,
cellattr: rowSetting
};
AJAX CALL
success: function(result)
{
...
for (i=0; i<colM.length; i++) {
cm = colM[i];
if (cm.hasOwnProperty("template") && cellattrMapping.hasOwnProperty(cm.template))
{
cm.template = cellattrMapping[cm.template];
}
推荐答案
看起来你的样子看起来像将 cellattr
设置为 tring value cellattr:rowSetting
而不是将其初始化为函数的指针。问题是您可以以JSON格式发送的数据有一些限制。它支持字符串,数字,布尔,数组和对象,但您可以将函数作为JSON数据的一部分发送。
It seems that what you do looks like you set cellattr
to string value cellattr: "rowSetting"
instead of initialysing it to the pointer of function. The problem is there are some restrictions in the data which you can send as JSON. It supports strings, numbers, boolean, arrays and objects, but you can's send functions as part of JSON data.
可以为问题提出许多解决方法。例如,您可以使用您使用的一些 cellattr
函数创建JavaScript代码。您可以将所有 cellattr
函数放在一个对象中:您的自定义 cellattr
字符串到函数映射。在成功回调之前,您可以检查
result.colModel
cellattr
属性。如果您发现 cellattr
属性,您应该将字符串值替换为相应的函数引用。
One can suggest many workarounds for the problem. For example you can create JavaScript code with some cellattr
functions which you use. You can place all the cellattr
functions which you need in one object: your custom cellattr
"string to functions mapping". Inside of success
callback you can examine result.colModel
items for cellattr
property. If you found cellattr
property you should just replace the string value to the corresponding function reference.
在旧答案中,我描述了更详细的非常接近的解决方案。您可以对 cellattr
使用相同的方法。
In the old answer I described more detailed very close solution. You can use the same approach for cellattr
.
在我看来,更好的方法是使用 template
属性 colModel
更加活跃(请参阅旧答案)。您将具有与以前相同的问题,但您可以提供您在不同网格中使用的常用模板,并将第一个字符串值发布为 template
属性 colModel
。然后,您可以将该值替换为具有所有必需实现细节的JavaScript对象。使用列模板的主要优点是在多个网格上共享通用代码。 colModel
中的信息变得更小,更易读。模板可以轻松修改。
More better way in my opinion would be to use template
attribute of colModel
more active (see the old answer). You will have the same problem as before, but you could provide common templates which you use in different grids and post first string values as the value of template
attribute of colModel
. Then you could replace the value to JavaScript object which hold all required implementation details. The main advantage of usage column templates is sharing common code over multiple grids. The information in colModel
become smaller and more readable. The templates could be easy modified.
这篇关于动态列的JQGrid:cellattr不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!