jqGrid custom formatter
For one of the columns in my jqGrid, I'm providing a custom formatter function. I'm providing some special cases, but if those conditions aren't met, I'd like to resort to using the built-in date formatter utility method. It doesn't seem that I'm getting the right combination of $.extend() to create the options that method is expecting.
My colModel for this column:
{ 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"}
},
And an example of what I'm trying to do
function expireFormat(cellValue, opts, rowObject) {
if (cellValue == null || cellValue == 1451520000) {
// a specific date that should show as blank
return '';
} else {
// here is where开发者_StackOverflow中文版 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);
}
}
(An exception is being thrown in the guts of that DateFormat method, looks like where it's trying to read into a masks property of the options that get passed in)
EDIT:
The $.extend that put everything in the place it needed was getting it from that global property where the i18n library set it, $.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);
In the jqGrid source code, different options are passed to the formatter when it is a built-in function versus when a custom formatter is used:
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;
},
So basically what's going on is that when a built-in formatter is used, cm.formatter
is passed as an argument. I need to confirm this, but based upon the error you are receiving, this appears to be a copy of the formatter
options from grid.locale-en.js (or whatever version of the i18n file you are using). So when called internally the formatter would contain additional options such as masks
- which is the one your code is failing on.
As a preventative measure, I would try adding masks
to your op
variable. If that solves your problem then great, otherwise keep adding other missing options back into your code until it works.
Does that help?
精彩评论