ExtJS (3x) dateedit popup with resset/clear button.

Ok dear friends, all know that extjs is very complex but also very useful and sophisticated library. Any change in it — is problem. As example one of mine clients want and clear up date field, which can’t be edited manually. I.e. — your have popup, and they was in need of having one more button at the calendar popup.

There are few possibilities to add this button. Most simplest  — us not change popup :) but make this button inline in edit field. This was made by author of plugin here.

In same topic, i commented out how to make an Reset Button in popup.

Basicly:

  • overwrites Ext.DatePicker.prototype — i.e. i am adding there property, to activate reset ( showReset : false)
  • than am overwriting also onRender func() in same component. This is most interesting — am adding place holder for second button & am hiding it, if no need in it. Same style as extjs was writen …
  • Am overwriting Ext.menu.DateMenu.prototype — this one i used to activate popups from dateedit fields. So i was need add here showReset: false property and change init method, for use this, when initialising datepicker.
  • Than am overwriting Ext.form.DateField.prototype — to send property (also added) to the  «Ext.apply(this.menu.picker, { «
  • Than am adding new component, which have reset buttons in popup & inside field. This is more usable for myne application
    Ext.ux.ResetDateField = Ext.extend(Ext.form.DateField, { isStart: true, showReset: …

I know that all — is to complex & maybe are methods to make this simpler. Maybe somebody can help me optimize this code?

 

PS. Code

Ok, here is code, which makes this reset button in popup … also with with reset button inside field (last future can be commented if any need)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Ext.apply(Ext.DatePicker.prototype, {
 
    resetText : 'Reset',
    showReset : false,
    resetTip : '',
 
    selectReset : function()
    {
        if(this.resetBtn && !this.resetBtn.disabled)
        {
            this.setValue(new Date().clearTime());
            this.fireEvent('select', this, 'Selected');
        }
    },
 
 
 
 
    onRender : function(container, position){
 
 
 
        var m = [
             '<table cellspacing="0">',
                '<tr><td class="x-date-left"><a href="#" title="', this.prevText ,'"> </a></td><td class="x-date-middle" align="center"></td><td class="x-date-right"><a href="#" title="', this.nextText ,'"> </a></td></tr>',
                '<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'],
                dn = Ext.DatePicker.prototype.dayNames,
                i;
 
        for(i = 0; i < 7; i++){
            var d = this.startDay+i;
            if(d > 6){
                d = d-7;
            }
            m.push('<th><span>', dn[d].substr(0,1), '</span></th>');
        }
        m[m.length] = '</tr></thead><tbody><tr>';
        for(i = 0; i < 42; i++) {
            if(i % 7 === 0 && i !== 0){
                m[m.length] = '</tr><tr>';
            }
            m[m.length] = '<td><a href="#" hidefocus="on" class="x-date-date" tabIndex="1"><em><span></span></em></a></td>';
        }
        m.push('</tr></tbody></table></td></tr>',
 
                this.showToday || this.showReset ? '<tr><td colspan="3" class="x-date-bottom" align="center"><div style="overflow:auto;width:78px;text-align:center;">' : "",
                this.showToday ? '<div class="x-date-bottom1" style="float:left;overflow:auto;"></div>' : '',
                this.showReset ? '<div class="x-date-bottom2" style="float:left;overflow:auto;"></div>' : '',
                this.showToday || this.showReset ? '</div></td></tr>' : "",
 
                '</table><div class="x-date-mp"></div>');
 
        var el = document.createElement('div');
 
 
 
        el.className = 'x-date-picker';
        el.innerHTML = m.join('');
 
        container.dom.insertBefore(el, position);
 
        this.el = Ext.get(el);
        this.eventEl = Ext.get(el.firstChild);
 
        this.prevRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-left a'), {
            handler: this.showPrevMonth,
            scope: this,
            preventDefault:true,
            stopDefault:true
        });
 
        this.nextRepeater = new Ext.util.ClickRepeater(this.el.child('td.x-date-right a'), {
            handler: this.showNextMonth,
            scope: this,
            preventDefault:true,
            stopDefault:true
        });
 
        this.monthPicker = this.el.down('div.x-date-mp');
        this.monthPicker.enableDisplayMode('block');
 
        this.keyNav = new Ext.KeyNav(this.eventEl, {
            'left' : function(e){
                if(e.ctrlKey){
                    this.showPrevMonth();
                }else{
                    this.update(this.activeDate.add('d', -1));
                }
            },
 
            'right' : function(e){
                if(e.ctrlKey){
                    this.showNextMonth();
                }else{
                    this.update(this.activeDate.add('d', 1));
                }
            },
 
            'up' : function(e){
                if(e.ctrlKey){
                    this.showNextYear();
                }else{
                    this.update(this.activeDate.add('d', -7));
                }
            },
 
            'down' : function(e){
                if(e.ctrlKey){
                    this.showPrevYear();
                }else{
                    this.update(this.activeDate.add('d', 7));
                }
            },
 
            'pageUp' : function(e){
                this.showNextMonth();
            },
 
            'pageDown' : function(e){
                this.showPrevMonth();
            },
 
            'enter' : function(e){
                e.stopPropagation();
                return true;
            },
 
            scope : this
        });
 
        this.el.unselectable();
 
        this.cells = this.el.select('table.x-date-inner tbody td');
        this.textNodes = this.el.query('table.x-date-inner tbody span');
 
        this.mbtn = new Ext.Button({
            text: ' ',
            tooltip: this.monthYearText,
            renderTo: this.el.child('td.x-date-middle', true)
        });
        this.mbtn.el.child('em').addClass('x-btn-arrow');
 
        if(this.showToday){
            this.todayKeyListener = this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectToday,  this);
            var today = (new Date()).dateFormat(this.format);
            this.todayBtn = new Ext.Button({
                renderTo: this.el.child('div.x-date-bottom1', true),
                text: String.format(this.todayText, today),
                tooltip: String.format(this.todayTip, today),
                handler: this.selectToday,
                scope: this
            });
        }
 
 
        if(this.showReset){
            //this.resetKeyListener = this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectReset,  this);
            //var today = (new Date()).dateFormat(this.format);
            this.resetBtn = new Ext.Button({
                renderTo: this.el.child('div.x-date-bottom2', true),
                text: this.resetText,
                tooltip: this.resetTip,
                handler: this.selectReset,
                scope: this
            });
        }
 
 
 
 
        this.mon(this.eventEl, 'mousewheel', this.handleMouseWheel, this);
        this.mon(this.eventEl, 'click', this.handleDateClick,  this, {delegate: 'a.x-date-date'});
        this.mon(this.mbtn, 'click', this.showMonthPicker, this);
        this.onEnable(true);
 
        $.log( "Rendered" ); // do not run
 
    }
 
 
});
 
// DATE MENU REWRITE
 
Ext.apply(Ext.menu.DateMenu.prototype, {
 
    showReset: false, //new property - to control all
 
    initComponent : function(){
        this.on('beforeshow', this.onBeforeShow, this);
        if(this.strict = (Ext.isIE7 && Ext.isStrict)){
            this.on('show', this.onShow, this, {single: true, delay: 20});
        }
        Ext.apply(this, {
            plain: true,
            showSeparator: false,
            items: this.picker = new Ext.DatePicker(Ext.applyIf({
                internalRender: this.strict || !Ext.isIE,
                ctCls: 'x-menu-date-item',
                id: this.pickerId
            }, this.initialConfig))
        });
        this.picker.showReset = this.showReset; //<---- here init picker as wee need
        this.picker.purgeListeners();
        Ext.menu.DateMenu.superclass.initComponent.call(this);
 
        this.relayEvents(this.picker, ['select']);
        this.on('show', this.picker.focus, this.picker);
        this.on('select', this.menuHide, this);
        if(this.handler){
            this.on('select', this.handler, this.scope || this);
        }
    }
 
});
 
 
//----------------------
Ext.apply(Ext.form.DateField.prototype, {
 
    showReset: false,
 
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        if(this.menu == null){
            this.menu = new Ext.menu.DateMenu({
                hideOnClick: false,
                focusOnSelect: false,
                showReset:true
            });
        }
        this.onFocus();
        Ext.apply(this.menu.picker,  {
            minDate : this.minValue,
            maxDate : this.maxValue,
            disabledDatesRE : this.disabledDatesRE,
            disabledDatesText : this.disabledDatesText,
            disabledDays : this.disabledDays,
            disabledDaysText : this.disabledDaysText,
            format : this.format,
            showToday : this.showToday,
            startDay: this.startDay,
            showReset: this.showReset,
            minText : String.format(this.minText, this.formatDate(this.minValue)),
            maxText : String.format(this.maxText, this.formatDate(this.maxValue))
        });
        this.menu.picker.setValue(this.getValue() || new Date());
        this.menu.show(this.el, "tl-bl?");
        this.menuEvents('on');
    }
 
});
 
 
 
 
 
/* reset datefield component */
 
Ext.ux.ResetDateField = Ext.extend(Ext.form.DateField, {
 
    isStart: true,
    showReset: true,
 
 
 
//    yearsToGet: 5,
    reset_trg_class: 'x-form-clear-trigger',
 
    initComponent: function() {
        this.triggerConfig = { 
            tag:'span', cls:'x-form-twin-triggers', cn:[ 
            {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger x-form-date-trigger"},
            {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.reset_trg_class}
        ]}; 
        Ext.ux.ResetDateField.superclass.initComponent.call(this);
    },
 
 
    getTrigger: Ext.form.TwinTriggerField.prototype.getTrigger, 
    initTrigger: Ext.form.TwinTriggerField.prototype.initTrigger,
    onTrigger2Click: function(e) 
    { 
        this.reset( );
//        this.setValue( '' );
//        store.commit();
    },
    onTrigger1Click: Ext.form.DateField.prototype.onTriggerClick, 
    trigger1Class: Ext.form.DateField.prototype.triggerClass,
 
// main so
});


Now, when is need of use field with reset:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{                         
      header : "Sent Date",
      dataIndex : 'sent_date',
      sortable : true,
      width: 80,
      renderer: function(value, metaData, r) { 
          metaData.attr = rowcolor(r.data.status);
          return value;
      },
 
//      editor : new Ext.form.DateField({ /*Ext.ux.form.Custom*/
      editor : new Ext.ux.ResetDateField({ /*Ext.ux.form.Custom*/
 
          allowBlank: true,
          format: 'm/d/Y',
          width : 120,
          enableKeyEvents: true,
 
 
          listeners: {
                  'keydown' : function (field_, e_  )  {
                    field_.onTriggerClick();
                    e_.stopEvent();
                    return false;
              },
                  'focus' : function (field_  )  {
                    field_.onTriggerClick();
                    e_.stopEvent();
                    return false;
              }
 
          }
 
 
      })
  }

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *