Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS 4, customize boolean column value of Ext.grid.Panel

Good day, i have a grid with boolean column:

 var grid = Ext.create('Ext.grid.Panel', {
      ...
      columns: [{
           dataIndex: 'visibleForUser',
           text: 'Visible',
           editor: {
              xtype: 'checkboxfield',
              inputValue: 1 // <-- this option has no effect
           }
      },
      ...

Grid's store is remote via JSON proxy. When i save or update row, the resulting JSON look like:

{... visibleForUser: false, ... }

As you see, ExtJS serializes checkbox value as true or false JSON terms. I need to customize this and serialize to, say, 1 and 0, any suggestion how to accomplish this ? Thank you.

like image 770
Dfr Avatar asked Jan 16 '23 18:01

Dfr


2 Answers

I've just changed my checkboxes system-wide to always act/respond to 0 and 1:

Ext.onReady(function(){
    // Set the values of checkboxes to 1 (true) or 0 (false), 
    // so they work with json and SQL's BOOL field type
    Ext.override(Ext.form.field.Checkbox, { 
        inputValue: '1',
        uncheckedValue: '0'
    });
});

But you can just add this configs per checkbox.

like image 91
Izhaki Avatar answered Jan 23 '23 13:01

Izhaki


Ext JS 4.1.1 has a new serialize config on record fields. When a writer is preparing the record data, the serialize method is called to produce the output value instead of just taking the actual field value. So you could do something like this:

fields: [{
    name: "visibleForUser",
    type: "boolean",
    serialize: function(v){
        return v ? 1 : 0;
    }
    /* other fields */
}]

I try to avoid overriding default component behavior whenever possible. As I mentioned, this only works in 4.1.1 (it was introduced in 4.1.0 but I believe it was broken). So if you're using an earlier version, one of the other answers would suit you better.

like image 24
Eric Avatar answered Jan 23 '23 13:01

Eric