In extjs, sometimes you need to select a date range, which needs to be automatically judged. The selected start date cannot be greater than the end date, or the end date cannot be less than the start date. The implemented code is as follows
Rendering:
As you can see from the picture above, when a start time is selected, the selection range of the end time will be automatically limited to realize the linkage of the two date selectors.
The code is as follows:
First define the linkage processing function:
Ext.apply(Ext.form.field.VTypes, { daterange: function (val, field) { var date = field.parseDate(val); if (!date) { return false; } if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) { var start = field.up('grid').down('#' + field.startDateField); start.setMaxValue(date); start.validate(); this.dateRangeMax = date; } else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) { var end = field.up('grid').down('#' + field.endDateField); end.setMinValue(date); end.validate(); this.dateRangeMin = date; } return true; }, daterangeText: '开始日期必须小于结束日期' }); Ext.tip.QuickTipManager.init();
Add to items in tbar, bbar or form:
{ xtype: 'datefield', fieldLabel: '时间范围 开始', name: 'startdt', id: 'startdt', vtype: 'daterange', endDateField: 'enddt', format: 'Y-m-d', width: 220, labelWidth: 90, msgTarget: 'side', autoFitErrors: false }, { xtype: 'datefield', fieldLabel: '结束', name: 'enddt', id: 'enddt', vtype: 'daterange', startDateField: 'startdt', format: 'Y-m-d', width: 170, labelWidth: 40, msgTarget: 'side', autoFitErrors: false }, { xtype: 'button', text: '查询', iconCls: 'fljs', handler: function () { ...
The above effect can be achieved. This code is copied and run in extjs4.1.1