javascript 如何将附加变量传递给 jqGrid 格式化程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8494782/
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
How to pass additional variables to jqGrid formatter?
提问by stawek
I'm trying to create some kind of reusable formatter for jqGrid column, I would like to create custom formatter where I'm able to pass additional data , something similar to this code:
我正在尝试为 jqGrid 列创建某种可重用的格式化程序,我想创建自定义格式化程序,我可以在其中传递其他数据,类似于以下代码:
function imageLinkFormatter(cellval,options,rowObject,icon,link_class,link_action){
var img='<span class="ui-icon '+icon+' icon"><span/>';
var link='<a href="#'+link_action+'/id/'+rowObject.id+'" class="'+link_class+'" rel="'+rowObject.id+'">'+img+'</a>';
return link;
}
回答by Oleg
It's probably a misunderstanding. The interface of the custom formatteris defined by jqGrid. To have additional parameters in the custom formatter you have to modify the source code of jqGrid.
这大概是个误会。自定义格式化程序的接口由 jqGrid 定义。要在自定义格式化程序中添加其他参数,您必须修改 jqGrid 的源代码。
The good news is that you don't really need to extend the standardcustom formatter. Instead of that you want probably just share the code. So you can define the common code as the function like
好消息是您并不真的需要扩展标准的自定义格式化程序。而不是您想要的只是共享代码。所以你可以将公共代码定义为函数
function imageLinkFormatter(cellval, options, rowObject, icon, link_class, link_action) {
var img = '<span class="ui-icon ' + icon + ' icon"><span/>';
var link = '<a href="#' + link_action + '/id/' + rowObject.id + '" class="' +
link_class + '" rel="' + rowObject.id + '">' + img + '</a>';
return link;
}
and call the function from the custom formatter of the different columns of the grid with additional parameters.
并从带有附加参数的网格不同列的自定义格式化程序中调用该函数。
colModal: [
{name: 'col1', formatter: function (cellvalue, options, rowObject) {
return imageLinkFormatter(cellvalue, options, rowObject,
'ui-icon-pencil', 'edit-link-class', 'Edit');
}},
{name: 'col2', formatter: function (cellvalue, options, rowObject) {
return imageLinkFormatter(cellvalue, options, rowObject,
'ui-icon-plus', 'add-link-class', 'Add');
}},
{name: 'col2', formatter: function (cellvalue, options, rowObject) {
return imageLinkFormatter(cellvalue, options, rowObject,
'ui-icon-trash', 'del-link-class', 'Delete');
}},
...
]
Is it what you want?
是你想要的吗?
回答by Andrew Veresov
define formatoptions in the column definition
在列定义中定义格式选项
colModal: [
{name: 'col1',
formatter: imageLinkFormatter,
formatoptions: {
icon: 'ui-icon-pencil',
link_class: 'edit-link-class',
action: 'Edit'
}},
{name: 'col2', formatter: imageLinkFormatter, formatoptions: {icon: 'ui-icon-plus', link_class: 'add-link-class', action: 'Add'}},
{name: 'col3', formatter: imageLinkFormatter, formatoptions: {icon: 'ui-icon-trash', link_class: 'del-link-class', action: 'Add'}}
...
]
and then you can access it inside custom formatter
然后您可以在自定义格式化程序中访问它
function imageLinkFormatter(cellval, options, rowObject) {
var img = '<span class="ui-icon ' + options.colModel.formatoptions.icon + ' icon"><span/>';
var link = '<a href="#' + options.colModel.formatoptions.action + '/id/' + rowObject.id + '" class="' +
options.colModel.formatoptions.link_class + '" rel="' + rowObject.id + '">' + img + '</a>';
return link;
}