//格式化时间
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, // month
"d+": this.getDate(), // day
"h+": this.getHours(), // hour
"m+": this.getMinutes(), // minute
"s+": this.getSeconds(), // second
"q+": Math.floor((this.getMonth()+3)/3), // quarter
"S": this.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return format;
}
//返回格式化后的时间
function formatDatebox(value) {
if (value == null || value == '') {
return '';
}
var dt;
if (value instanceof Date) {
dt = value;
} else {
dt = new Date(value);
}
return dt.format("yyyy-MM-dd"); //扩展的Date的format方法(上述插件实现)
}
以上代码是把后台传过来的Date对象进行格式化,直接把formatter指定为formatDatebox即可
//扩展editor编辑器
$.extend($.fn.datagrid.defaults.editors, {
datetimebox: {// datetimebox就是你要自定义editor的名称
init: function (container, options) {
var input = $('<input class="easyuidatetimebox">').appendTo(container);
return input.datetimebox({
formatter: function (date) {
return new Date(date).format("yyyy-MM-dd");
}
});
},
getValue: function (target) {
return $(target).parent().find('input.combo-text').val();
},
setValue: function (target, value) {
$(target).datetimebox("setValue", formatDatebox(value));
},
resize: function (target, width) {
var input = $(target);
if ($.boxModel == true) {
input.width(width - (input.outerWidth() - input.width()));
} else {
input.width(width);
}
}
}
});
以上代码扩展了datetimebox编辑器,只在在需要的单元格里指定编辑器为“datetimebox”即可,如
{
field:'bigendate' ,
title:'开始时间' ,
width: 150 ,
align:'center' ,
formatter: formatDatebox,
editor:{
type:'datetimebox' ,
options:{
editable:true
}
}
},
特别要注意的一点是:“
datetimebox”编辑器里的“setValue”方法里的“value”的格式一定要和“binendate”里的格式一样