调用WCF返回的Json数据,时间字段的返回格式是 "open_date":"\/Date(976723200000+0800)\/"
我的javascript是这样写的
//创建js下department对象,和WCF端的department对象对应
var department = Ext.data.Record.create([
{ name: 'dept_id', mapping: 'dept_id' }, //name:js下对象属性名称 ,mapping:对应服务端属性名称
{name: 'dept_code' },
{ name: 'chi_name' },
{ name: 'eng_name' },
{ name: 'phone_no' },
{ name: 'fax_no' },
{ name: 'status' },
{ name: 'eng_addr_1' },
{ name: 'eng_addr_2' },
{ name: 'eng_addr_3' },
{ name: 'eng_addr_4' },
{ name: 'chi_addr_1' },
{ name: 'chi_addr_2' },
{ name: 'hot_line' },
{ name: 'grade' },
{ name: 'dept_head_id' },
{ name: 'dept_head_name' },
{ name: 'dept_head_phone' },
{ name: 'dept_head_phone_2' },
{ name: 'dist_mgr_id' },
{ name: 'open_date', type: 'date', dateFormat: 'n/j h:ia' },
{ name: 'close_date', type: 'date', dateFormat: 'n/j h:ia' },
{ name: 'update_dept' },
{ name: 'update_user' },
{ name: 'update_date' },
{ name: 'dist_mgr_tel' },
{ name: 'direct_head' },
{ name: 'direct_tel' },
{ name: 'direct_tel_2' },
{ name: 'dp_op_date' },
{ name: 'dept_type' },
{ name: 'email_addr' },
{ name: 'service_area' },
{ name: 'clerk_id' },
{ name: 'senior_clerk_id' },
{ name: 'mr_co_id' },
{ name: 'dept_order' },
{ name: 'seat_count' },
{ name: 'market_type' },
{ name: 'org_code' },
{ name: 'region_id' },
{ name: 'dept_dist_id' },
{ name: 'remark' },
{ name: 'alt_chi_name' }
]);
var reader = new Ext.data.JsonReader(
readerOption,
department
);
//用代理和读取器创建用于ExtJs的数据存储器
store = new Ext.data.Store({
proxy: proxy,
reader: reader,
remoteSort: true
});
//列七
var cm7 = {
header: "开始时间",
width: 100,
dataIndex: 'open_date',
renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
};
//列八
var cm8 = {
header: "关闭时间",
width: 100,
dataIndex: 'close_date',
renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
};
在园子里阿不的一篇文章的回复里有看到阿不说这样处理的:
Ext.util.Format.date = function(v, format, f2, f, t) {
if (!v) {
return "";
}
if (!(v instanceof Date)) {
vDate = v.replace(/\/Date\(([0-9]+)\)\//ig, '$1');
;
if (vDate != '') {
v = new Date(parseInt(vDate));
}
else {
v = new Date(Date.parse(v));
}
}
return v.dateFormat("Y-m-d");
}
你得为EXT自己写个renderer专门处理WCF返回的Date格式,具体的做法是:
1、把"\/Date(976723200000+0800)\/"中的976723200000提取出来,这一步无论是正则还是substring都能完成
2、把976723200000变成整型,用parseInt来完成
3、把976723200000交给Date类型的构造函数,即var date = new Date(976723200000);
4、现在已经有一个Date变量了,格式化成字符串的话应该没问题了吧?