【问题标题】:Need to show hours as 07:00, 15:00, 23:00 (8 hrs increment) in EXTJs timefield需要在 EXTJs 时间域中将小时显示为 07:00、15:00、23:00(8 小时增量)
【发布时间】:2015-04-12 06:32:28
【问题描述】:

我需要在 EXTJs 时间域中将小时显示为 07:00、15:00、23:00(8 小时增量),但是当我使用以下代码时,它没有给出所需的输出,有人可以帮助我吗,如果我遗漏了什么。

Ext.create('Ext.form.Panel', {
    title: 'Time Card',
    width: 300,
    bodyPadding: 10,
    renderTo: Ext.getBody(),
    items: [{
        xtype: 'timefield',
        name: 'in',
        fieldLabel: 'Time In',
        format : 'H:i',
        minValue: '07:00',
        maxValue: '23:00',
        increment: 480,
        anchor: '100%'
    }]
});

提前致谢。

【问题讨论】:

    标签: javascript extjs timefield


    【解决方案1】:

    您在这里遇到了一个小问题,因为时间选择器无法按照您尝试使其工作的方式工作。 我会分解它,让你有一个想法:

    1) 获取 24 小时内的所有时间(从 00 到 23)。

    2) 每increment 分钟将使用一种方法将时间从00 推到23。 例如(如果你设置increment: 60,那么你会得到00:00, 01:00, 02:00)

    3) 此数据集将返回到您的时间字段,并使用您的 minValue 和 MaxValue 对其应用过滤器

    因此,使用 480 的设置,您将获得: 00:00, 08:00, 16:00。

    然后应用过滤器(minValue: 07:00maxValue: 23:00)将删除第一个过滤器,留下 08:00 和 16:00。

    基本上你想要的是第一个间隔是 7 小时,然后是第一个循环后 8 小时,所以你需要像这样应用覆盖:

    **** 我没有彻底测试过这个,所以如果你想在不同的时间间隔使用它,你可能要小心。 ***

        Ext.define('Ext.picker.override.Time',{
            override: 'Ext.picker.Time',
            statics: {
                createStore: function(format, increment, startHour) {
                    var dateUtil = Ext.Date,
                        clearTime = dateUtil.clearTime,
                        initDate = this.prototype.initDate,
                        times = [],
                        min = clearTime(new Date(initDate[0], initDate[1], initDate[2])),
                        startHour = startHour || min,
                        max = dateUtil.add(clearTime(new Date(initDate[0], initDate[1], initDate[2])), 'mi', (24 * 60) - 1),
                        firstIncrement = startHour.getHours() * 60,
                        count = 0;
    
                    while(min <= max){
                        times.push({
                            disp: dateUtil.dateFormat(min, format),
                            date: min
                        });
                        min = dateUtil.add(min, 'mi', (count++ == 0 ? firstIncrement : increment));
                    }
    
                    return new Ext.data.Store({
                        model: Ext.picker.Time.prototype.modelType,
                        data: times
                    });
                }
            }
        });
        Ext.define('Ext.form.field.CustomTime',{
            extend: 'Ext.form.field.Time',
            alias: 'widget.customtimefield',
            initComponent: function() {
                var me = this,
                    min = me.minValue,
                    max = me.maxValue;
    
                if (min) {
                    me.setMinValue(min);
                }
                if (max) {
                    me.setMaxValue(max);
                }
                me.displayTpl = new Ext.XTemplate(
                    '<tpl for=".">' +
                    '{[typeof values === "string" ? values : this.formatDate(values["' + me.displayField + '"])]}' +
                    '<tpl if="xindex < xcount">' + me.delimiter + '</tpl>' +
                    '</tpl>', {
                        formatDate: me.formatDate.bind(me)
                    });
    
                // Create a store of times.
                me.store = Ext.picker.Time.createStore(me.format, me.increment, me.minValue);
    
                me.superclass.superclass.initComponent.call(this);
    
                // Ensure time constraints are applied to the store.
                // TimePicker does this on create.
                me.getPicker();
            } 
        });
    
        Ext.create('Ext.form.Panel', {
            title: 'Time Card',
            width: 300,
            bodyPadding: 10,
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'customtimefield',
                name: 'in',
                fieldLabel: 'Time In',
                format : 'H:i',
                minValue: '07:00',
                maxValue: '23:00',
                increment: 480,
                anchor: '100%'
    
            }]
        });
    

    小提琴:https://fiddle.sencha.com/#fiddle/i3e

    【讨论】:

    • 非常感谢 Guilherme 的回复,我会检查并通知您。
    • Guilherme,当我选择像 15:00 这样的时间时,它不会填充到字段中。
    • Oops..sometimes ExtJS 在您这样覆盖 initComponent 时表现不佳,因此我替换了从 field.Time 创建扩展类的代码并更新了小提琴。这应该可以为您解决问题。
    • 太棒了 Guilherme,非常感谢... :)
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2013-07-23
    相关资源
    最近更新 更多