开发者

Displaying JSON serialized date in ext js grid

The server returns date in JSON as below,

{
    "LastUpdated": "\/Date(1310117748850)\/"
}

I'm using ExtJs grid and the date is not showing up. How I can display it in M/dd/yyyy format?

this.store = new Ex开发者_运维问答t.data.JsonStore({
    autoLoad: {
        params: {
            start: 0,
            limit: 10
        }
    },
    autoDestroy: true,
    url: '/home/jobs',
    idProperty: 'Name',
    fields: ['Name',
             'Description',
             'Type',
             'Group',
             'Data',
             'Schedule.Name',
             'Schedule.Description', {
                 name: 'LastUpdated',
                 type: 'date'
             },
             'Schedule.Expression',
             'Status'],
    root: 'data',
    sortInfo: {
        field: 'Name',
        direction: 'ASC'
    }
});   

In Grid colModel:

{
    header: 'Last Updated',
    dataIndex: 'LastUpdated',
    width: 80,
    renderer: Ext.util.Format.dateRenderer('m/d/Y')
},


//Grid:
columns: [                    
    {header: 'Last Updated', dataIndex: 'LastUpdated', renderer: Ext.util.Format.dateRenderer('m/d/Y')}                        
]

//store:
{
  field:'LastUpdated',
  type:'date',
  convert:function(v,j){
     return new Date(v.replace(/\/Date((\d+))\//, '$1'));
  }
}


In the column section, you could create you own custom date renderer:

columns: [                    
    {header: 'Last Updated', dataIndex: 'LastUpdated', renderer: dateRenderer}                        
]

And then create a function that processes your date:

function dateRenderer(value, id, r) {
    var yourDate = r.data['uploadDate'];

    // some js stuff here to strip out just the epoch time

    var datevar = new Date(yourDateEpoch);
    return Ext.Date.format(datevar, 'm/d/Y')
}


For date field you need also specify dateFormat. E.g.:

{name: 'LastUpdated', type: 'date', dateFormat: 'Y-m-d'}

But in your case you receive date in other format...


The only thing you need to do is to have the following in your model

{name: 'LastUpdated', type: Ext.data.Types.DATE, dateFormat: 'time' }

It tells ExtJS that the data coming into the model for this field is of form epoch time.


Use

return new Date(parseInt(v.replace('/Date(', '')));

full example below,

columns: [                    
    {
        header: 'Last Updated',
        dataIndex: 'LastUpdated',
        renderer: Ext.util.Format.dateRenderer('m/d/Y')
    }
]

//store:
{
    field:'LastUpdated',
    type:'date',
    convert:function(v, j) {
        return new Date(parseInt(v.replace('/Date(', '')));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜