需要在EXTJs时间段显示时间为07:00,15:00,23:00(8小时递增)

时间:2015-02-11 18:40:35

标签: javascript 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 个答案:

答案 0 :(得分: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