Have Custom formatter for date field in JQGrid

我们两清 提交于 2019-12-07 22:56:33

问题


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:


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/



来源:https://stackoverflow.com/questions/25683851/have-custom-formatter-for-date-field-in-jqgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!