ExtJS 4.2 Date組件擴展:添加清除按鈕


ExtJS中除了提供豐富的組件外,我們還可以擴展他的組件。

在這里,我們將在Date日期組件上添加一個【清除】按鈕,用於此組件已選中值的清除。

目錄

1. Date組件介紹

2. 主要代碼說明

3. 代碼與在線演示

 

1. Date組件介紹

這里的Date組件全稱為 Ext.form.field.Date,為form表單一個組件。

查看Ext.form.field.Date的源代碼的得知需要 Ext.picker.Date

Ext.picker.Date是一個日期選擇器,包含了日期選中、渲染布局等等,也正是我們需要修改的文件。

 

2. 主要代碼說明

Date組件原先就包括了一個【今天】按鈕,可根據此按鈕在Ext.picker.Date的創建方式來創建一個【清除】按鈕。

2.1 創建一個js文件,用以覆蓋Ext.picker.Date

Ext.define('Js.ux.DatePicker', {
    override: 'Ext.picker.Date'
    ...
}

 

2.2 renderTpl屬性

說明:renderTpl表示一個組件的渲染模板,在【今天】按鈕后面渲染【清除】按鈕。

 

2.3 beforeRender方法

說明:在此方法中初始化【清除】按鈕。

beforeRender: function () {
    var me = this;
    me.callParent();
    // 創建按鈕 
    me.clearBtn = new Ext.button.Button({
        ownerCt: me,
        ownerLayout: me.getComponentLayout(),
        text: '清除',
        tooltip: '清除日期',
        tooltipType: 'title',
        handler: me.selectClear,
        scope: me
    });
}

  

2.4 selectClear方法

說明:此方法表示點擊【清除】按鈕執行的內容。

selectClear: function () {
    var me = this,
        btn = me.clearBtn,
        handler = me.handler;

    if (btn && !btn.disabled) {
        me.setValue('');
        me.fireEvent('select', me, me.value);
        if (handler) {
            handler.call(me.scope || me, me, me.value);
        }
        me.onSelect();
    }
    return me;
}

  

3. 代碼與在線演示

3.1 完整代碼

在ExtJS文件后面引入此文件即可:

/*!
* 在原有的Date組件上添加【清除】按鈕
*/
Ext.define('Js.ux.DatePicker', {
    override: 'Ext.picker.Date',
    renderTpl: [
        '<div id="{id}-innerEl" role="grid">',
            '<div role="presentation" class="{baseCls}-header">',
                 // the href attribute is required for the :hover selector to work in IE6/7/quirks
                '<a id="{id}-prevEl" class="{baseCls}-prev {baseCls}-arrow" href="#" role="button" title="{prevText}" hidefocus="on" ></a>',
                '<div class="{baseCls}-month" id="{id}-middleBtnEl">{%this.renderMonthBtn(values, out)%}</div>',
                 // the href attribute is required for the :hover selector to work in IE6/7/quirks
                '<a id="{id}-nextEl" class="{baseCls}-next {baseCls}-arrow" href="#" role="button" title="{nextText}" hidefocus="on" ></a>',
            '</div>',
            '<table id="{id}-eventEl" class="{baseCls}-inner" cellspacing="0" role="grid">',
                '<thead role="presentation"><tr role="row">',
                    '<tpl for="dayNames">',
                        '<th role="columnheader" class="{parent.baseCls}-column-header" title="{.}">',
                            '<div class="{parent.baseCls}-column-header-inner">{.:this.firstInitial}</div>',
                        '</th>',
                    '</tpl>',
                '</tr></thead>',
                '<tbody role="presentation"><tr role="row">',
                    '<tpl for="days">',
                        '{#:this.isEndOfWeek}',
                        '<td role="gridcell" id="{[Ext.id()]}">',
                            // the href attribute is required for the :hover selector to work in IE6/7/quirks
                            '<a role="presentation" hidefocus="on" class="{parent.baseCls}-date" href="#"></a>',
                        '</td>',
                    '</tpl>',
                '</tr></tbody>',
            '</table>',
            '<tpl if="showToday">',
                '<div id="{id}-footerEl" role="presentation" class="{baseCls}-footer">',
                    '{%this.renderTodayBtn(values, out)%}',
                    '{%this.renderClearBtn(values, out)%}',
                    '</div>',
            '</tpl>',
        '</div>',
        {
            firstInitial: function (value) {
                return Ext.picker.Date.prototype.getDayInitial(value);
            },
            isEndOfWeek: function (value) {
                // convert from 1 based index to 0 based
                // by decrementing value once.
                value--;
                var end = value % 7 === 0 && value !== 0;
                return end ? '</tr><tr role="row">' : '';
            },
            renderTodayBtn: function (values, out) {
                Ext.DomHelper.generateMarkup(values.$comp.todayBtn.getRenderTree(), out);
            },
            renderMonthBtn: function (values, out) {
                Ext.DomHelper.generateMarkup(values.$comp.monthBtn.getRenderTree(), out);
            },
            renderClearBtn: function (values, out) { // 清除按鈕
                Ext.DomHelper.generateMarkup(values.$comp.clearBtn.getRenderTree(), out);
            }
        }
    ],

    beforeRender: function () {
        var me = this;
        me.callParent();
        // 創建按鈕 
        me.clearBtn = new Ext.button.Button({
            ownerCt: me,
            ownerLayout: me.getComponentLayout(),
            text: '清除',
            tooltip: '清除日期',
            tooltipType: 'title',
            handler: me.selectClear,
            scope: me
        });
    },

    // Do the job of a container layout at this point even though we are not a Container.
    // TODO: Refactor as a Container.
    finishRenderChildren: function () {
        var me = this;
        me.callParent();
        me.clearBtn.finishRender();
    },

    /**
    * Sets the value of the date field
    * @param {Date} value The date to set
    * @return {Ext.picker.Date} this
    */
    setValue: function (value) {
        if (value == '') {
            this.value = value;
        } else {
            this.value = Ext.Date.clearTime(value, true);
            return this.update(this.value);
        }
    },

    /**
    * 清除按鈕點擊
    */
    selectClear: function () {
        var me = this,
            btn = me.clearBtn,
            handler = me.handler;

        if (btn && !btn.disabled) {
            me.setValue('');
            me.fireEvent('select', me, me.value);
            if (handler) {
                handler.call(me.scope || me, me, me.value);
            }
            me.onSelect();
        }
        return me;
    },

    beforeDestroy: function () {
        var me = this;
        if (me.rendered) {
            Ext.destroy(
                me.clearBtn
            );
        }
        me.callParent();
    },
});

 

3.2 運行圖

 

3.3 在線演示

在線演示http://www.akmsg.com/ExtJS/index.html#App.Demo.DateExtendTab

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM