2010-05-21 102 views
2

對於我的jqGrid中的其中一列,我提供了一個自定義格式化函數。我提供了一些特殊情況,但如果不滿足這些條件,我想使用內置的日期格式化工具方法。我似乎沒有得到$ .extend()的正確組合來創建方法所期望的選項。jqGrid自定義格式化程序

我colModel此列:

{ name:'expires', 
    index:'7', 
    width:90, 
    align:"right", 
    resizable: false, 
    formatter: expireFormat, 
    formatoptions: {srcformat:"l, F d, Y g:i:s A",newformat:"n/j/Y"} 
}, 

和我想要做的

function expireFormat(cellValue, opts, rowObject) { 
    if (cellValue == null || cellValue == 1451520000) { 
     // a specific date that should show as blank 
     return ''; 
    } else { 
     // here is where I'd like to just call the $.fmatter.util.DateFormat 
     var dt = new Date(cellValue * 1000); 
     var op = $.extend({},opts.date); 
     if(!isUndefined(opts.colModel.formatoptions)) { 
      op = $.extend({},op,opts.colModel.formatoptions); 
     } 
     return $.fmatter.util.DateFormat(op.srcformat,dt,op.newformat,op); 
    } 
} 

一個例子(異常被拋出在DateFormat的方法,看起來膽量就像它試圖讀取傳入的選項的掩碼屬性一樣)

編輯:

$ .extend將所有需要的東西從i18n庫設置的全局屬性中獲取,$ .jgrid.formatter.date

var op = $.extend({}, $.jgrid.formatter.date); 
if(!isUndefined(opts.colModel.formatoptions)) { 
    op = $.extend({}, op, opts.colModel.formatoptions); 
} 
return $.fmatter.util.DateFormat(op.srcformat,dt.toLocaleString(),op.newformat,op); 

回答

4

在jqGrid的源代碼,不同的選項都傳遞給格式化,當它是一個內置的功能,當使用自定義格式與:

formatter = function (rowId, cellval , colpos, rwdat, _act){ 
     var cm = ts.p.colModel[colpos],v; 
     if(typeof cm.formatter !== 'undefined') { 
      var opts= {rowId: rowId, colModel:cm, gid:ts.p.id }; 
      if($.isFunction(cm.formatter)) { 
       v = cm.formatter.call(ts,cellval,opts,rwdat,_act); 
      } else if($.fmatter){ 
       v = $.fn.fmatter(cm.formatter, cellval,opts, rwdat, _act); 
      } else { 
       v = cellVal(cellval); 
      } 
     } else { 
      v = cellVal(cellval); 
     } 
     return v; 
    }, 

所以基本上這是怎麼回事的是,當使用內置格式化程序時,cm.formatter作爲參數傳遞。我需要確認這一點,但根據您收到的錯誤,這似乎是來自grid.locale-en.js(或您使用的任何i18n文件版本)的formatter選項的副本。因此,在內部調用時,格式化程序將包含其他選項,如masks - 這是您的代碼無法執行的選項。

作爲預防措施,我會嘗試將masks添加到您的op變量中。如果這能解決您的問題,那就太棒了,否則請繼續添加其他缺少的選項回到您的代碼中,直到它工作。

這有幫助嗎?

+0

是的,似乎我不得不從i18n格式化選項延伸,發現正確的組合。謝謝! – 2010-05-21 18:13:39

+0

不客氣,很高興幫助:) – 2010-05-21 18:37:14

相關問題