Have Custom formatter for date field in JQGrid

2019-05-19 00:50发布

I want to diplay date on grid as N/A incase it is NULL in the database, or else in normal date format. At present I have this code:

name: 'Handshake_Actual_Date', index: 'Handshake_Actual_Date', search: false, 
editable:true, align: "left", width: 75, formatter: "date", formatoptions: 
{newformat:"m/d/Y" },
editoptions: {
size: 20,
dataInit: function (el) {
$(el).datepicker({ dateFormat: 'mm/d/yy' });
},
defaultValue: function () {
var currentTime = new Date();
var month = parseInt(currentTime.getMonth() + 1);
month = month <= 9 ? "0" + month : month;
var day = currentTime.getDate();
day = day <= 9 ? "0" + day : day;
var year = currentTime.getFullYear();
return day + "/" + month + "/" + year;
}
}

It works perfectly well but I want a custom formatter like below :

function myFormatter (cellvalue, options, rowObject) {
if(cellvalue==null)
{
return "N/A";
}
else
{
return $.fn.fmatter.date(cellvalue, options, rowObject);
}
}

but it is not working..! for an example you can see the code here : http://jsfiddle.net/35Larwva/

1条回答
We Are One
2楼-- · 2019-05-19 01:26

Try to replace

$.fn.fmatter.date(cellvalue, options, rowObject)

to

$.fn.fmatter.call(this, "date", cellvalue, options, rowObject);

Moreover I recommend you to use more correct JavaScript code: cellvalue==NULL should be replaced to cellvalue === null, you should use ; at the end of statements (after return "N/A" for example) and it's better always use {} for if and else statements.

UPDATED: I fixed your jqfiddle demo so that it work now: http://jsfiddle.net/OlegKi/35Larwva/2/

查看更多
登录 后发表回答