2011-12-13 23 views
7

我試圖建立某種用於jqGrid的列可重複使用的格式化, 我想創建自定義的格式在那裏我能傳遞更多的數據,類似這樣的東西代碼:如何將附加變量傳遞給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; 
    } 

回答

10

這可能一個誤會。 custom formatter的接口由jqGrid定義。要在自定義格式化程序中有其他參數,您必須修改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; 
} 

,並從其他參數網格的不同列的自定義格式化調用該函數。

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'); 
     }}, 
    ... 
] 

這是你想要的嗎?

+0

感謝的答案和解釋,我認爲我必須擴展自定義格式,但是這是完美解決方案問候 – stawek

+0

@stawek:不客氣! – Oleg

6

在列定義

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'}} 
    ... 
] 

定義formatoptions,然後您可以訪問它的自定義格式裏面

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; 
}