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

來源:互聯網
上載者:User

標籤:

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

 

==================================系列文章==========================================

本篇文章:7.9 ExtJS 4.2 Date組件擴充:添加清除按鈕

Web開發之路系列文章

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.