如何使用动态列绑定为jqgrid添加自定义格式化程序

如何使用动态列绑定为jqgrid添加自定义格式化程序,jqgrid,Jqgrid,这几乎是前一个问题的延续 我正在尝试为如下列设置自定义格式设置程序。但什么也没发生。请帮忙 JSP: 格式化程序的Javascript函数: function CouponFormatter(cellValue, opts, rowObject) { return cellValue + "Testing coupon formatter"; } function InterFinalPriceFormatter(cellValue, opts, rowObject) { retur

这几乎是前一个问题的延续

我正在尝试为如下列设置自定义格式设置程序。但什么也没发生。请帮忙

JSP:

格式化程序的Javascript函数:

  function CouponFormatter(cellValue, opts, rowObject) {
return cellValue + "Testing coupon formatter";
   }

function InterFinalPriceFormatter(cellValue, opts, rowObject) {
return cellValue + "Testing price formatter";
}
如果你使用

"formatter": "InterFinalPriceFormatter"
您没有将“formatter”属性的值设置为函数

解决此问题的一种方法是循环执行
result.colmodelsist
,并验证是否使用了带有字符串值的“formatter”属性,该字符串值在JavaScript中作为函数实现。然后,可以使用相应格式化程序函数的值覆盖该属性

另一种方法是在格式化程序中使用内联函数:

“格式化程序”:“函数(cellValue、opts、rowObject){return cellValue+\“Testing price formatter\”;}”
在这种情况下,代码和网格参数之间没有明确的分离,但您会在网格内收到一些格式化程序的封装

更新:我希望下一个代码片段(未经测试)能够明确我在第一种实现方式下的意思

var函数映射={
//这里我们定义了我们使用的自定义格式化程序的实现
“CouponFormatter”:函数(cellValue、opts、rowObject){
返回cellValue+“测试优惠券格式化程序”;
},
“InterFinalPriceFormatter”:函数(cellValue、opts、rowObject){
返回cellValue+“测试价格格式化程序”;
}
};
$.ajax({
类型:“POST”,
url:“interFinalTbaAction.action”,
数据:“,
数据类型:“json”,
成功:功能(结果){
变量i,cm,colD=result.couponStripList,
colN=result.columnNames,
colM=result.colModelList;

对于(i=0;iI理解内联函数部分,但没有得到第一部分。当我循环遍历
result.colmodelsist
时,我如何替换为格式化程序功能?你能举一些伪代码示例吗?它是否类似于,var InterFinalPriceFormatter=“function(cellValue,opts,rowObject){return cellValue+\”测试价格格式化程序\“;}”@silpa:I更新我的答案。
var InterFinalPriceFormatter=“函数(cellValue,opts,rowObject){return cellValue+\“测试价格格式化程序\“;}”
定义变量
InterFinalPriceFormatter
具有
typeof(InterFinalPriceFormatter)==“string”
var InterFinalPriceFormatter=函数(cellValue,opts,rowObject){return cellValue+\“Testing price formatter\”;};
定义函数和
typeof(InterFinalPriceFormatter)==函数“
”@沃洛加:不客气!这是一个比较老的答案。现在我建议您最好将
$.fn.fmatter
对象扩展到定义的格式化程序,您可以直接将其用作字符串:
格式化程序:“InterFinalPriceFormatter”
。您可以在中找到相应的代码示例和。重要的是,它允许将格式化程序用作字符串
格式化程序:“dynamicLink”
而不是函数
格式化程序:DynamicClinkFunction
@Jesstone:不知道细节就无法回答您的问题。您使用哪种
数据类型
?是否使用
loadonce
(如果
数据类型:“json”
)?哪种格式有jqGrid的输入数据(您使用哪种
jsonReader
)?如何定义
colModel
等等。最好在发布所有详细信息的地方提出新问题。
  function CouponFormatter(cellValue, opts, rowObject) {
return cellValue + "Testing coupon formatter";
   }

function InterFinalPriceFormatter(cellValue, opts, rowObject) {
return cellValue + "Testing price formatter";
}
"formatter": "InterFinalPriceFormatter"