DateField怎么获得json里的时间数据

DateField怎么获得json里的时间数据

问题描述:

我用json中数据自动填充到FormPanel表单中,
但是在DateField里没有显示数据。
DateField怎么获得json里的时间?
[b]问题补充:[/b]
其他Field都可以得到数据,只是DataField里没有
以下是我得到的json,应该如何格式化?
{success:true,data:{"cbcontent":"内容5",
"cbendtime":{"date":24,"day":6,"hours":0,"minutes":0,"month":0,"seconds":0,"time":1232726400000,"timezoneOffset":-480,"year":109},
"cbid":10,"cbistop":0,"cbmsgtypeid":14,"cbposter":"","cbposterid":1,"cbposttime":{"date":13,"day":2,"hours":10,"minutes":10,"month":0,"seconds":9,"time":1231812609000,"timezoneOffset":-480,"year":109},"cbsource":1,
"cbstarttime":{"date":4,"day":0,"hours":0,"minutes":0,"month":0,"seconds":0,"time":1230998400000,"timezoneOffset":-480,"year":109},"cbtitle":"标题5","cbtoplevel":3}}
[b]问题补充:[/b]
我刚接触这个,还是不明白,代码如下,id是GRID中一行数据的ID,
功能是通过点击修改按钮,弹出修改页,修改页的Field中要填充修改前的数据。
[code="java"]function updateBulletin(id) {
var sysMsgForm = new Ext.FormPanel({
labelAlign : 'top',
frame : true,
bodyStyle : 'padding:5px 5px 0',
width : 600,
items : [{ //第一行
xtype : 'textfield',
fieldLabel : '标题',
name : 'cbtitle',
maxLength : 25,
allowBlank : false,
blankText : '标题不能为空',
anchor : '100%'
}, { //第二行
layout : 'column',
items : [{
columnWidth : .5,
layout : 'form',
items : [new Ext.form.DateField({
fieldLabel : '生效日期',
name : 'cbstarttime',
format : 'Y-m-d',
allowBlank : false,
anchor : '95%'
})

                                    ]
                                }, {
                                    columnWidth : .5,
                                    layout : 'form',
                                    items : [new Ext.form.DateField({ // DateField
                                        fieldLabel : '失效日期',
                                        name : 'cbendtime',
                                        format : 'Y-m-d',
                                        allowBlank : false,
                                        anchor : '95%'
                                    })]
                                }]
                    }, {                         //第三行
                        xtype : 'textarea',
                        fieldLabel : '内容',
                        name : 'cbcontent',
                        maxLength : 500,
                        allowBlank : false,
                        blankText : '内容不能为空',
                        width : 150,
                        anchor : '100%'
                    }],
            buttons : [{
                text : '修改',
                handler : function() {
                    if (sysMsgForm.form.isValid()) {
                        Ext.MessageBox.show({
                                    msg : '保存中...',
                                    progressText : 'Saving...',
                                    width : 300,
                                    wait : true,
                                    waitConfig : {
                                        interval : 200
                                    },
                                    icon : 'download',
                                    animEl : 'saving'
                                });
                        sysMsgForm.form.doAction('submit', {
                                    url : 'publishedBulletin.do',
                                    method : 'post',
                                    success : function(form, action) {
                                        postWindow.close();
                                        if (action.result.msg == 'ok') {
                                            Ext.MessageBox.hide();
                                            Ext.Msg.alert('提示', '修改成功!');
                                            ds.reload();
                                        } else {
                                            Ext.Msg.alert('失败', action.result.msg);
                                        }
                                    },
                                    failure : function() {
                                        postWindow.close();
                                        Ext.Msg.alert('错误', '网络连接有误');
                                    }
                                });
                    }
                }
            }, {
                text : '关闭',
                handler : function() {
                    postWindow.close();
                }
            }]
        });

var postWindow = new Ext.Window({
            title : '发布公告',
            width : 626,
            height : 400,
            collapsible : true,
            maximizable : true,
            plain : true,
            bodyStyle : 'padding:5px;',
            modal : true,
            items : sysMsgForm
        });
postWindow.show();

sysMsgForm.form.load({
            url : 'updateBulletin.do?cbid=' + id,
            waitMsg : '正在载入数据...'

        });

} [/code]

你想实现这样的效果,给你点代码看看,这是我做的,你不要用form.load直接采用我写的做,一定能出来,[color=red]可以直接用GRID中的数据,这时候,你传入的ID就不能用了,需要传入行ID就是rowIndex
function updateBulletin([color=blue]rowIndex[/color]) {[/color] var record=null;
if(rowIndex>=0&&store!=null){ record = store.getAt(rowIndex);
}
//是不是从列表中读取出来的
var sysMsgForm = new Ext.FormPanel({

labelAlign : 'top',

frame : true,

bodyStyle : 'padding:5px 5px 0',

width : 600,

items : [{ //第一行

xtype : 'textfield',

fieldLabel : '标题',

name : 'cbtitle',

maxLength : 25,

allowBlank : false,

blankText : '标题不能为空',
[color=red]value:record.get('对应的字段名称');[/color] anchor : '100%'

}, { //第二行

layout : 'column',

items : [{

columnWidth : .5,

layout : 'form',

items : [new Ext.form.DateField({

fieldLabel : '生效日期',

name : 'cbstarttime',

format : 'Y-m-d',

allowBlank : false,
[color=red]value:new Date(record.get('对应的时间字段名称').time).format("yyyy-MM-dd"),[/color]
anchor : '95%'

})

]

剩下的你知道该怎么做了吧

后台返回的json数据到前台后就会成为一个字符串了,你需要把这个字符串格式化一下,再扔给DateField。

Date.prototype.format =function(format){var o ={"M+" :this.getMonth()+1,"d+" :this.getDate(),"h+" :this.getHours(),"m+" :this.getMinutes(),"s+" :this.getSeconds(),"q+" :Math.floor((this.getMonth()+3)/3),"S" :this.getMilliseconds()};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;}
[color=red]//时间需要格式化一下,因为Date是个对象,所以输出时会输出它的属性[/color]"date":13,
"day":2,
"hours":10,
"minutes":10,
"month":0,
"seconds":9,
"time":1231812609000,
"timezoneOffset":-480,
"year":109
上面的都是属性,你可以查看Date对象
解决方案如下:
value:new Date(record.get('[color=red]cbendtime[/color]').[color=red]time[/color]).format("yyyy-MM-dd")

[code="javascript"]
Ext.override(Ext.form.DateField, {
setValue : function(date){
if(typeof(date) === "string") {
date = new Date(date);
}
Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
}
});
[/code]

这样写能不能解决你的问题?

value:record.get('对应的字段名称'), anchor : '100%'