2013-04-07 116 views
0

我正在尝试动态绑定jqGrid列模型的格式器。我按照如下动态构建colModel阵列。如何动态绑定jqGrid colModel格式器

ColModel:[{name:Id,width:50,formatter:customerLinkFormatter}] 

如下

$.extend($.fn.fmatter, { 
customerLinkFormatter: function (cellvalue, options, rowdata) { 
    return '<a href="CustomerEdit.aspx?id=' + rowdata[options.colModel.name] + '"> ' + cellvalue + '</a>'; 
} 
我已经扩展格式化

});

但是,没有链接显示为Id列。请帮我弄清楚。

下面是代码的一部分

$(document).ready(function() { 
     "use strict"; 
     $.ajax({ 
      type: "POST", 
      url: "../Hdlr.ashx?", 
      datatype: "json", 
      success: function (msg) { 
       jqcolNames = msg.ColNames, 
       jqcolModel = msg.ColModel, 

       PopulateGrid(); 
      }, 
      error: function (msg) { 
       alert(' error ' + msg.responseText); 
      } 
     }); 
    }); 

    function PopulateGrid() { 
     $('#list').jqGrid({ 
      url: "../Hdlr.ashx?", 
      colNames: jqcolNames, 
      colModel: jqcolModel, 
      jsonReader: { 
       cell: "", 
       id: "0", 
       repeatitems: false 
      }, 
      rowNum: 10, 
      rowList: [10, 20, 30], 
      pager: "#pager", 
      rownumbers: true, 
      viewrecords: true, 
      search: false, 
      caption: "Grid Information" 
     }).jqGrid("navGrid", "#pager", { edit: false, add: false, del: false, search: false }); 
    } 

回答

0

尝试定义formatter就像定义一个函数:

function customerLinkFormatter(cellvalue, options, rowdata) { 
       return '<a href="CustomerEdit.aspx?id=' + rowdata.name + '"> ' + cellvalue + '</a>'; 
      }; 
+0

这就是我之前所做的。数据库中的格式化程序字段直接具有该功能。它没有工作或者 – user1077595 2013-04-08 12:43:26

0

如果扩展$.fn.fmatter那么你应该使用字符串“customerLinkFormatter”,而不是直接使用功能customerLinkFormatter

colModel:[{name: "Id", width: 50, formatter: "customerLinkFormatter"}] 

如果您之前没有使用相应的字符串值定义Id变量,那么name:Id的用法也是错误的。

你写了关于动态绑定jqGrid的格式化器。您只需更改 colModel内的财产。可以使用setColProp方法来检验。请参阅heresetColProp的使用示例。

+0

感谢您的答复。当我扩展格式化程序时,我仍然必须绑定colModel的格式化程序。 – user1077595 2013-04-08 12:41:05

+0

@ user1077595:对不起,我不明白你的问题。关于'$ .extend($ .fn.fmatter,{customerLinkFormatter:function(...){...}});'你定义了'$ .fn.fmatter.customerLinkFormatter'函数,它将被调用格式化使用'formatter:“customerLinkFormatter”'定义的列上的所有单元格。因此,您的自定义格式化程序与jqGrid的任何其他[预定义格式程序](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter#predefined_format_types)的工作方式相同。 – Oleg 2013-04-08 12:47:16

+0

我从数据库中获取colModel如下 “ColModel”:[{“name”:“CUSTOMER_ID”,“width”:“60”,“formatter”:“customerLinkFormatter”},{“name”:“说明“,”width“:”50“,”formatter“:null}]但它不起作用 – user1077595 2013-04-08 13:22:36