2011-12-13 11 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; 
} 
関連する問題