PDA

View Full Version : dynamically changedatefiled display



jmoraes
8 Dec 2009, 8:56 PM
Hi

how can i change dynamically the datefield format? Ex: Y/m/d < -> Y/m

i tried



var dt = new Date(myDatefield.getValue()); // gets old selected date
myDatefield.format = 'Y/m';
myDatefield.setValue(dt);

and



var dt = new Date(myDatefield.getValue()); // gets old selected date
myDatefield.format = 'Y/m/d';
myDatefield.setValue(dt);

But, getValue always gets the first value defined when the component is rendered when i switch between 'Y/m/d' and 'Y/m'

Anyone can help?

Regards
Jarbas

mystix
8 Dec 2009, 11:25 PM
use this override:


Ext.override(Ext.form.DateField, {
switchFormat: function(nf) {
var dt = this.getValue(); // get underlying js Date object
this.format = nf; // set new format

if (Ext.isDate(dt)) {
this.setRawValue(dt.format(nf)); // set this DateField's display value
}
}
});



then


myDatefield.switchFormat('Y/m');
myDatefield.switchFormat('Y/m/d');




p.s.


But, getValue always gets the first value defined when the component is rendered when i switch between 'Y/m/d' and 'Y/m'

that's not true. getValue() always returns the current value of the DateField.

jmoraes
9 Dec 2009, 2:53 AM
thanks!

but about your p.s. What am I doing wrong?

Pick a new date from the datefield and check/uncheck "use day"



var myDateField = new Ext.form.DateField({
fieldLabel: 'date',
format: 'Y/m/d',
value: new Date()
});

var check = new Ext.form.Checkbox({
fieldLabel: 'use day',
checked: true,
listeners: {
check: {
fn: function(el, value) {

console.log('before switchFormat: '+myDateField.getValue().format('Y/m/d'))
if (!value) {
myDateField.switchFormat('Y/m')
} else {
myDateField.switchFormat('Y/m/d')
}
console.log('after switchFormat: '+myDateField.getValue().format('Y/m/d'))
}
}
}
});

var form = new Ext.form.FormPanel({
renderTo: Ext.getBody(),
items: [myDateField, check]
})


Regards

mystix
9 Dec 2009, 3:02 AM
i changed my suggested code to avoid mucking with the actual stored value.

try the updated code instead.