Pages

Thursday, April 14, 2011

ExtJS -=- Converting a timestamp value to a valid date when loading a FormPanel (DateField)

I've got a problem today. I was trying  to load a record that contains a timestamp field that comes from a Rails application (with PostgreSQL DB).

The FormPanel refuse to load the date value in my DateField component. I've discovered that it was because the DateField did not recognize the date because it was a timestamp.

In order to be able to set the date's value, we need to format the timestamp as a date when loading the FormPanel.

Here's how to do this.

Generated JSON used to load the FormPanel
{
     success:true, 
     data: {
          start_date: \"Tue Apr 12 20:06:11 UTC 2011\", 
          id: 4, 
          app_id: 2, 
          version: \"1.2.3\", 
          message: \"test msg\", 
          active: true
     }
}


Add this code to your FormPanel
form.getForm().on({
    actioncomplete: function(form, action) {
        if (action.type === 'load') {
            if (action.result.success) {
                var date = new Date(action.result.data.start_date);

                form.setValues({
                    start_date: date.format('Y-m-d')
                });
            }
        }
    }
});

When loading, the value of the date will be converted from a timestamp to a valid date and the DateField will recognize the value.

No comments:

Post a Comment