ExtJS DateField - initialising date

Zaje picture Zaje · Apr 20, 2012 · Viewed 18.8k times · Source

I have an ExtJS Date field. During some operation in the application, min value and max value get assigned to the date field. The min value and max value are 4 years prior to the current dat, but when the date picker opens up, it opens to the disabled current dates. The user has to manually scroll back 4 years to select a date. Anyways I can update the datepicker to open up by showing the date between the min value and max value ?

Adding code:

cmpDt.setMinValue(new Date(2000, 0, 1));
cmpDt.setMaxValue(new Date(2004, 0, 1));

this sets the min and max value. I cant use setValue() because it inialises/changes the textfield. I want the textfield to get value only on selection from the datepicker.

Thanks

Answer

Stefan Gehrig picture Stefan Gehrig · Apr 20, 2012

You have to set an initial value using the value property of the Ext.form.field.DateView:

{
    ...,
    minValue: new Date(2000, 0, 1),
    maxValue: new Date(2004, 11, 31),
    value: new Date(2002, 5, 15),
    ...
}

EDIT after more info from the OP:

You may override the onExpand method that initializes the value on the picker. The original one looks like (given that you use ExtJS 4 - but 3 should not be that much different):

...,
onExpand: function() {
    var value = this.getValue();
    this.picker.setValue(Ext.isDate(value) ? value : new Date());
},
...

You could override the method to read:

...,
onExpand: function() {
    var value = this.getValue(),
        myDefaultDate = /* do some processing to determine the default date*/;
    this.picker.setValue(Ext.isDate(value) ? value : myDefaultDate);
},
... 

Just add the override to the initial form field configuration.