ExtJS - 将日期值发布为 Unix 时间戳

发布于 2024-09-28 06:21:58 字数 1359 浏览 2 评论 0原文

我使用 editorgrid 来编辑 JsonStore 中的元素。 JsonStore 使用 HttpProxy 来更新后端数据库。

我的问题是后端 API 期望 fromTstoTs 为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筑梦 2024-10-05 06:21:58

我知道这个案例很旧,但我找到了这个问题的解决方案,但我从未在这里发布过。

我向代理的 beforewrite 事件添加了一个侦听器,并在那里操纵了 post 参数

proxy: new Ext.data.HttpProxy({
    api: {
        create: '/create/',
        read:   '/read/',
        update: '/update/',
        destroy:'/destroy/'
    },
    listeners: {
        beforewrite: function(proxy, action, record, params) {
            var fromTs = record.data.fromTs;
            var toTs = record.data.toTs;

            if(record.data.fromTs) record.data.fromTs = fromTs.format('U');
            if(record.data.toTs) record.data.toTs = toTs.format('U');

            // Update record to be sent
            // root = store.reader.root
            params.root = Ext.util.JSON.encode(record.data);

            // Restore record
            record.data.fromTs = fromTs;
            record.data.toTs = toTs;
        }
    }
})

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

proxy: new Ext.data.HttpProxy({
    api: {
        create: '/create/',
        read:   '/read/',
        update: '/update/',
        destroy:'/destroy/'
    },
    listeners: {
        beforewrite: function(proxy, action, record, params) {
            var fromTs = record.data.fromTs;
            var toTs = record.data.toTs;

            if(record.data.fromTs) record.data.fromTs = fromTs.format('U');
            if(record.data.toTs) record.data.toTs = toTs.format('U');

            // Update record to be sent
            // root = store.reader.root
            params.root = Ext.util.JSON.encode(record.data);

            // Restore record
            record.data.fromTs = fromTs;
            record.data.toTs = toTs;
        }
    }
})
老娘不死你永远是小三 2024-10-05 06:21:58

您也许可以挂钩 EditorGridPanelvalidateedit 事件或 afteredit 事件,并使用以下命令将用户输入的值转换回时间戳一个日期解析方法。我猜测 EditorGridPanel 正在逐字更新存储中的记录,而不将它们转换回时间戳,因此您必须手动执行此操作。所以我想也许是这样的:

grid.on('validateedit', function(event) {
  if (isDateColumn(column)) {
    event.record.data[event.field] = dateToTimestamp(event.value);
  }
}

You might be able to hook into the validateedit event or afteredit event of your EditorGridPanel and convert the user-entered-value back into a timestamp using a Date parsing method. I'm guessing that EditorGridPanel 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:

grid.on('validateedit', function(event) {
  if (isDateColumn(column)) {
    event.record.data[event.field] = dateToTimestamp(event.value);
  }
}
甩你一脸翔 2024-10-05 06:21:58

问题是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文