需要在 EXTJs 时间字段中将小时数显示为 07:00、15:00、23:00(8 小时增量)

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 小时增量),但是当我使用以下代码时,它没有给出想要的输出,如果我遗漏了什么,有人可以帮助我吗?

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%'
    }]
});

提前致谢。

你在这里遇到了一个小问题,因为时间选择器没有按照你试图让它工作的方式工作。 我会把它分解,所以你有一个想法:

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%'

        }]
    });

Fiddle: https://fiddle.sencha.com/#fiddle/i3e