【问题标题】:jqGrid custom formatterjqGrid 自定义格式化程序
【发布时间】:2010-05-21 13:53:57
【问题描述】:

对于我的 jqGrid 中的一列,我提供了一个自定义格式化程序函数。我提供了一些特殊情况,但如果不满足这些条件,我想求助于使用内置的日期格式化程序实用方法。我似乎没有得到正确的 $.extend() 组合来创建该方法所期望的选项。

本专栏的我的 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);

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

    在 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 的formatter 选项的副本(或您正在使用的任何版本的 i18n 文件)。因此,当在内部调用时,格式化程序将包含其他选项,例如 masks - 这是您的代码失败的选项。

    作为预防措施,我会尝试将masks 添加到您的op 变量中。如果这样可以解决您的问题,那就太好了,否则请继续将其他缺少的选项添加回您的代码中,直到它起作用为止。

    这有帮助吗?

    【讨论】:

    • 是的,看来我必须扩展 i18n 格式化程序选项,找到正确的组合。谢谢!
    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多