2010-05-21 92 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

相关问题