2013-04-07 40 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