ExtJS - 将日期值发布为 Unix 时间戳
我使用 editorgrid
来编辑 JsonStore
中的元素。 JsonStore
使用 HttpProxy
来更新后端数据库。
我的问题是后端 API 期望 fromTs
和 toTs
为 Unix 时间戳,但是当更新记录时,生成的 http post 包含格式如下的日期:Wed Oct 20 00:00:00 UTC+0200 2010
我在 API 文档中搜索了控制帖子格式的参数,但没有找到任何内容。有没有一种简单的方法可以做到这一点?
myJsonStore = new Ext.data.JsonStore({
autoLoad: true,
autoSave: true,
proxy: new Ext.data.HttpProxy({
api: {
create: '/create/',
read: '/read/',
update: '/update/',
destroy:'/destroy/'
}
}),
writer: new Ext.data.JsonWriter({
encode: true,
writeAllFields: true
}),
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'fromTs', type: 'date', dateFormat:'timestamp'},
{name: 'toTs', type: 'date', dateFormat:'timestamp'}
]
});
editorgrid
配置如下:
{
xtype: 'editorgrid',
clicksToEdit: 1,
columns: [
{header: "Id", dataIndex: 'id', editable: false},
{header: "From", dataIndex: 'fromTs', editor: new Ext.form.DateField({format: 'd.m.Y', startDay: 1}), xtype: 'datecolumn', format: 'd.m.Y'},
{header: "To", dataIndex: 'toTs', editor: new Ext.form.DateField({format: 'd.m.Y', startDay: 1}), xtype: 'datecolumn', format: 'd.m.Y'}
],
store: myJsonStore
}
I use an editorgrid
to edit elements from a JsonStore
. The JsonStore
uses a HttpProxy
to update the backend database.
My problem is that the backend API expects fromTs
and toTs
to be Unix timestamps, but when a record is updated, the resulting http post contains a date formatted like this: Wed Oct 20 00:00:00 UTC+0200 2010
I've searched the API documentation for a parameter to control the post format, but I've not been able to find anything. Is there a simple way to do this?
myJsonStore = new Ext.data.JsonStore({
autoLoad: true,
autoSave: true,
proxy: new Ext.data.HttpProxy({
api: {
create: '/create/',
read: '/read/',
update: '/update/',
destroy:'/destroy/'
}
}),
writer: new Ext.data.JsonWriter({
encode: true,
writeAllFields: true
}),
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'fromTs', type: 'date', dateFormat:'timestamp'},
{name: 'toTs', type: 'date', dateFormat:'timestamp'}
]
});
The editorgrid
is configured like this:
{
xtype: 'editorgrid',
clicksToEdit: 1,
columns: [
{header: "Id", dataIndex: 'id', editable: false},
{header: "From", dataIndex: 'fromTs', editor: new Ext.form.DateField({format: 'd.m.Y', startDay: 1}), xtype: 'datecolumn', format: 'd.m.Y'},
{header: "To", dataIndex: 'toTs', editor: new Ext.form.DateField({format: 'd.m.Y', startDay: 1}), xtype: 'datecolumn', format: 'd.m.Y'}
],
store: myJsonStore
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这个案例很旧,但我找到了这个问题的解决方案,但我从未在这里发布过。
我向代理的 beforewrite 事件添加了一个侦听器,并在那里操纵了 post 参数
I know this case is old, but I found a solution to this problem that I never came around to post here.
I added a listener to the proxy's beforewrite event, and manipulated the post params there
您也许可以挂钩
EditorGridPanel
的validateedit
事件或afteredit
事件,并使用以下命令将用户输入的值转换回时间戳一个日期解析方法。我猜测EditorGridPanel
正在逐字更新存储中的记录,而不将它们转换回时间戳,因此您必须手动执行此操作。所以我想也许是这样的:You might be able to hook into the
validateedit
event orafteredit
event of yourEditorGridPanel
and convert the user-entered-value back into a timestamp using a Date parsing method. I'm guessing thatEditorGridPanel
is updating the records in the store verbatim without converting them back into timestamps, so you have to do that manually. So I'm thinking maybe something like:问题是 JsonWriter 不尊重 JsonReader 中使用的 dateFormat 字段。这篇文章描述了这个问题以及一些解决它的代码。您可以在此处找到它。
The problem is that the JsonWriter does not respect the dateFormat field used in your JsonReader. This post describes the issue along with some code that will address it. You can find it here.