在JQGrid中具有用于日期字段的自定义格式化程序 [英] Have Custom formatter for date field in JQGrid
问题描述
我想在网格上将日期显示为N/A,以防数据库中的日期为NULL或以正常日期格式显示. 目前,我有以下代码:
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);
}
}
但是它不起作用..! 例如,您可以在此处查看代码: http://jsfiddle.net/35Larwva/
but it is not working..! for an example you can see the code here : http://jsfiddle.net/35Larwva/
推荐答案
尝试替换
$.fn.fmatter.date(cellvalue, options, rowObject)
到
$.fn.fmatter.call(this, "date", cellvalue, options, rowObject);
此外,我建议您使用更正确的JavaScript代码:cellvalue==NULL
应该替换为cellvalue === null
,在语句末尾(例如,在return "N/A"
之后)应该使用;
,最好总是使用{}
用于if
和else
语句.
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.
已更新:我修复了您的jqfiddle演示,使其现在可以正常工作: http ://jsfiddle.net/OlegKi/35Larwva/2/
UPDATED: I fixed your jqfiddle demo so that it work now: http://jsfiddle.net/OlegKi/35Larwva/2/
这篇关于在JQGrid中具有用于日期字段的自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!