【问题标题】:extjs datefield browser not showing upextjs 日期字段浏览器未显示
【发布时间】:2012-10-22 21:12:01
【问题描述】:

所以我似乎在这方面找不到任何东西,但我遇到了一个奇怪的跨浏览器问题,我似乎无法找出它发生的原因。我有一个应该拉回某个日期的日期字段。在 Chrome 中显示日期,而在 FF 和 IE 中则不显示。

奇怪的是,我查看了从后端返回的数据并且日期在那里,它只是设置输入的值(也不是我们要求提取数据的任何位置。甚至在一个网格)

我是否缺少任何可以使其在 Chrome 中正确显示但在 FF 和 IE 中不显示的内容?也许商店或模型中有什么东西?

我正在使用 ExtJS 4.1.2,以防万一帮助回答问题

视图(网格)

Ext.define('MyApp.view.ContractGrid', {
extend: 'Ext.grid.Panel',
alias: 'widget.ContractGrid',

height: 443,
id: 'contractgrid',
itemId: '',
width: 667,
title: 'Contract Search',
store: 'ContractStore',

initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        columns: [
            {
                xtype: 'gridcolumn',
                dataIndex: 'ContractNumber',
                text: 'Contract #'
            },
            {
                xtype: 'datecolumn',
                hidden: false,
                dataIndex: 'LicenseDate',
                text: 'License Date'
            },
        ],
        viewConfig: {

        },
        selModel: Ext.create('Ext.selection.RowModel', {

        }),
        dockedItems: [
            {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                    {
                        xtype: 'button',
                        text: '+ New Contract',
                        listeners: {
                            click: {
                                fn: me.onButtonClick,
                                scope: me
                            }
                        }
                    }
                ]
            }
        ]
    });

    me.callParent(arguments);
},
});

查看(面板)

Ext.define('MyApp.view.ContractPanel', {
extend: 'Ext.panel.Panel',
alias: 'widget.ContractPanel',

requires: [
    'MyApp.view.ModuleTabs'
],

draggable: false,
height: 804,
id: 'contractpanel',
autoScroll: true,
layout: {
    type: 'absolute'
},
manageHeight: false,

initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        items: [
            {
                xtype: 'form',
                border: 0,
                height: 350,
                itemId: 'ContractForm',
                maxHeight: 400,
                width: 1060,
                layout: {
                    type: 'absolute'
                },
                bodyPadding: 10,
                manageHeight: false,
                items: [
                    {
                        xtype: 'panel',
                        border: 0,
                        height: 310,
                        margin: '',
                        minWidth: 450,
                        padding: '',
                        width: 480,
                        layout: {
                            type: 'absolute'
                        },
                        manageHeight: false,
                        items: [
                            {
                                xtype: 'textfield',
                                x: 0,
                                y: 0,
                                disabled: true,
                                margin: '',
                                padding: 5,
                                width: 236,
                                name: 'id',
                                fieldLabel: 'Contract ID',
                                labelWidth: 110
                            },
                            {
                                xtype: 'textfield',
                                x: 0,
                                y: 30,
                                margin: '',
                                padding: 5,
                                width: 236,
                                inputId: '',
                                name: 'ContractNumber',
                                fieldLabel: 'Contract Number',
                                labelWidth: 110
                            },
                            {
                                xtype: 'datefield',
                                x: 0,
                                y: 190,
                                padding: 5,
                                width: 210,
                                name: 'LicenseDate',
                                fieldLabel: 'License Date',
                                labelWidth: 110,
                                submitFormat: 'Y-d-m'
                            },
                        ]
                    }
                ]
            },
            {
                xtype: 'ModuleTabs',
                id: 'ModuleTabs',
                x: 0,
                y: 360
            }
        ]
    });

    me.callParent(arguments);
});

商店

Ext.define('MyApp.store.ContractStore', {
extend: 'Ext.data.Store',
alias: 'store.ContractStore',

requires: [
    'MyApp.model.ContractModel'
],

constructor: function(cfg) {
    var me = this;
    cfg = cfg || {};
    me.callParent([Ext.apply({
        autoLoad: true,
        autoSync: true,
        remoteFilter: true,
        remoteSort: true,
        storeId: 'contract',
        model: 'MyApp.model.ContractModel',
        buffered: true,
        pageSize: 200,
        listeners: {
            write: {
                fn: me.onStoreWrite,
                scope: me
            }
        }
    }, cfg)]);
}

});

型号

Ext.define('MyApp.model.ContractModel', {
extend: 'Ext.data.Model',
alias: 'model.ContractModel',

uses: [
    'MyApp.model.ModuleModel'
],

idProperty: 'id',

fields: [
    {
        name: 'id',
        type: 'int'
    },
    {
        name: 'ContractNumber',
        type: 'string'
    },
    {
        name: 'LicenseDate',
        type: 'date'
    }
],

hasMany: {
    model: 'MyApp.model.ModuleModel',
    foreignKey: 'contract_id',
    name: 'modules'
},

proxy: {
    type: 'direct',
    api: {
        create: contract.createRecord,
        read: contract.getResults,
        update: contract.updateRecords,
        destroy: contract.destroyRecord
},
    reader: {
        type: 'json',
        root: 'data'
    }
}
});

【问题讨论】:

  • 另外,如果你们想要任何特定的代码,请告诉我。我不知道在这种情况下显示什么是有益的,因为它只是我从资源中提取的标准日期字段......
  • 它是如何拉日期的?它是什么格式的?它是通过模型来的吗?你是如何设置值的?
  • 问题是这发生在超过 1 个地方。我们在使用'Y-d-m'的'submitFormat'作为我们的PHP后端的日期字段输入框中设置值。当我们提取数据时,我们只使用默认格式/altFormat。我们正在通过模型提取数据。当我们得到结果时,我们肯定会从后端返回正确的数据,但由于某种原因,只有 chrome 会渲染细节,而其他的则不会。对于网格,我们只需关联正确的商店,并且(如果我理解正确的话)商店会相应地填充网格。我们还在其他地方使用记录。
  • 仅供参考,上面的意思是“发布一些代码”... ;)
  • 大声笑我想我只是不知道你想要什么部分,因为它发生在多个位置。这是我的第一个 ExtJS 应用程序 =/ 不过我会发布一些内容。让我知道这是否还不够和/或您是否想要更多。

标签: extjs cross-browser extjs4.1 datefield


【解决方案1】:

您的问题是您没有在模型上指定 dateFormat 。由于您不这样做,它会转到本机 JS Date.parse 方法来创建日期。一些浏览器比其他浏览器对它们接受的格式更宽松。

例如,在 FF 中比较这些:

console.log(Date.parse('2012-01-01'));
console.log(Ext.Date.parse('2012-01-01', 'Y-m-d'));

长话短说,在模型上指定一个 dateFormat,这样您就不会让浏览器随心所欲地决定如何解析日期。

来自文档:

当类型为 指定为“日期”。

在序列化日期字段以供以下用户使用时也使用格式字符串 作家。

Ext.Date.parse 函数的格式字符串,如果 Reader 提供的值是 UNIX 时间戳,或“时间”,如果 Reader 提供的值是 javascript 毫秒时间戳。 请参阅 Ext.Date。

值得注意的是,虽然此配置是可选的,但它 将默认使用基本 JavaScript Date 对象的解析函数 如果未指定,而不是 Ext.Date.parse。这可能导致 意外问题,尤其是在时区之间转换时,或 转换未指定时区的日期时。这 本机 Date.parse 的行为是特定于实现的,并且 根据日期字符串的值,它可能会返回 UTC 日期或当地日期。 因此强烈推荐 解析日期时始终指定明确的日期格式。

【讨论】:

  • 我试过了,还是不行。我在 LicenseDate 存在的每个位置都添加了格式“m/d/Y”,但它仍然没有出现在 IE 或 FF 中,现在突然停止在 chrome 中工作了 o_O ......至少它一直在做一些事情.
  • 玩过它,看起来像在模块中添加'dateFormat'是导致它没有出现在任何地方的原因。 (甚至在 chrome 中也没有),而它是事先出现的。
  • 对不起,实际上你上面的东西有帮助。我返回的 JSON 格式为“Y-m-d H:i:s”。所以显然我需要将模型 dateFormat 更改为该格式。我以为模型 dateFormat 是在说要显示的格式,而不是输入的格式。感谢所有帮助。
  • 显然我错过了那部分 =/ 文档有时可能有点难以理解。不过感谢您的帮助 =)
  • 埃文,最后一行加粗。它非常重要,但从来没有人阅读过。
【解决方案2】:

尝试使用日期格式来塑造模型中的日期字段,如下所示:

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

您也可以在网格中使用格式来显示日期字段,如下所示:

xtype:'datecolumn', format:'Y-m-d', dataIndex:'expectedCompleteBy'

XTemplate 也有特殊的日期功能:

{expectedCompleteBy:date("Y-m-d")} 

形式如下:

{  xtype:'datefield',
   fieldLabel:'Need By Date',
   name:'needByDate',
   //submitFormat:'Y-m-d', defaults to format
   format:'Y-m-d'
 }

日期格式 API:http://docs.sencha.com/ext-js/4-1/#!/api/Ext.Date

【讨论】:

  • DmitryB ...这无济于事。这不是格式问题。我可以在 chrome 中看到我们想要的格式,但在其他浏览器中看不到任何内容。我不想改变格式....
  • 另外,正如在另一个答案中发布的那样,不幸的是,添加格式并没有帮助 =/
  • 什么是 licenseDate 在来自服务器的 JSON 有效负载中的样子?
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多