extjs的日期范围选择器?

mfpqipee  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(193)

有没有适合ExtJ的日期范围选择器?
大概是这样的:是吗?

wnrlj8wa

wnrlj8wa1#

我花了大约2个小时就完成了这个程序,所以我确信还有一些挥之不去的bug和需要改进的地方,但它至少让你开始使用一个自定义的日期范围组件。下面是我的Fiddle。代码:

Ext.define('DatePickerEnhanced', {
    extend: 'Ext.picker.Date',
    alias: 'widget.datePickerEnhanced',

    config: {
        startDate: null,
        endDate: null
    },

    isDateRange: true,
    isDragging: false,

    initComponent: function () {
        this.callParent()
        this.on({
            mouseover: {
                element: 'el',
                fn: 'onMouseOverEnhanced',
                delegate: 'td.x-datepicker-cell',
                scope: this
            }
        });
    },

    handleDateClick: function (e, t) {
        this.callParent(arguments)
        if (this.isDateRange) {
            if (this.isDragging) {
                this.isDragging = false;
                var startCell = this.getCellByValue(this.startCell)
                if (startCell) {
                    Ext.get(startCell).addCls(this.selectedCls)
                }
                this.setEndDate(new Date(this.getCellDateValue(this.activeCell)))
                this.updateDateRange(this.startCell, this.endCell);
            } else {
                this.setStartDate(new Date(this.getCellDateValue(this.activeCell)))
                this.isDragging = true;
                this.updateDateRange(this.getCellDateValue(), -1);
            }
        }
    },

    getCellByValue: function (value) {
        var cells = this.cells.elements;
        for (var i = 0; i < cells.length; i++) {
            var cell = cells[i]
            if (cell.firstChild.dateValue === value) {
                return cell;
            }
        }
    },

    onMouseOverEnhanced: function (e, target, eOpts) {
        if (this.isDragging) {
            this.updateDateRange(this.getCellDateValue(), this.getCellDateValue(target));
        }
    },

    updateDateRange: function (startValue, endValue) {
        var cells = this.cells.elements;
        var selectedCls = this.selectedCls;
        for (var i = 0; i < cells.length; i++) {
            var cell = Ext.fly(cells[i])
            var dateValue = this.getCellDateValue(cells[i]);
            if (dateValue !== startValue && (dateValue < startValue || dateValue > endValue)) {
                cell.removeCls(selectedCls)
            } else {
                cell.addCls(selectedCls)
            }
        }
    },

    getCellDateValue: function (cell) {
        return cell && cell.firstChild.dateValue || this.startCell;
    },

    getDateRange: function () {
        return {
            start: this.getStartDate(),
            end: this.getEndDate()
        }
    },

    /**
     * Update the contents of the picker for a new month
     * @private
     * @param {Date} date The new date
     */
    fullUpdate: function (date) {
        var me = this;
        me.callParent(arguments);
        Ext.asap(function () {
            if (me.isDateRange && me.endCell) {
                me.updateDateRange(me.startCell, me.endCell)
            }
        })
    },

    updateStartDate: function (value) {
        this.startCell = value.getTime()
        this.publishState('startDate', value);
    },

    updateEndDate: function (value) {
        this.endCell = value.getTime()
        this.publishState('endDate', value);
    }
});

Ext.create('Ext.container.Viewport', {
    viewModel: {
        data: {
            theStart: Ext.Date.add(new Date(), Ext.Date.DAY, 2),
            theEnd: Ext.Date.add(new Date(), Ext.Date.DAY, 5)
        }
    },
    items: [{
        xtype: 'datePickerEnhanced',
        minDate: new Date(),
        bind: {
            startDate: '{theStart}',
            endDate: '{theEnd}'
        }
    }, {
        xtype: 'displayfield',
        fieldLabel: 'Start',
        bind: {
            value: '{theStart:date("m/d/Y")}'
        }
    }, {
        xtype: 'displayfield',
        fieldLabel: 'End',
        bind: {
            value: '{theEnd:date("m/d/Y")}'
        }
    }]
});

相关问题