Search code examples
extjsextjs4extjs4.1extjs4.2extjs-mvc

ExtJS 4.2 grid cellediting how to bind column value with combo display value


I have a grid with cellediting plugin.

One of my columns is a int field which represents a value in a combo store when editing the cell.

I want to know how can I have this column to show the displayfield instead of the value before I edit the cell.

Here are the images for reference:

enter image description here

The values 2,0,0 etc are my "accesstype" field that is an int.

If I click the cell to edit that why I get:

enter image description here

And If I select a value then instead of showing the text I get again the int value.

enter image description here

How can I show the display field instead of the value field?


Solution

  • For the ease of access here's the answer from the other question:

    You can set a default value for the combo. That should then get that rendered at startup.

    Use cell renderer to render the displayField of the combo into your grid. Following a working example that can be poster in one of the API code boxes

    Working JSFiddle

    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone', 'id'],
        data:{'items':[
            {"name":"Lisa", "email":"[email protected]", "phone":"555-111-1224","id": 0},
            {"name":"Bart", "email":"[email protected]", "phone":"555-222-1234","id": 1},
            {"name":"Homer", "email":"[email protected]", "phone":"555-222-1244","id": 2},
            {"name":"Marge", "email":"[email protected]", "phone":"555-222-1254","id": 3}
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    // the renderer. You should define it within a namespace
    var comboBoxRenderer = function(combo) {
      return function(value) {
        var idx = combo.store.find(combo.valueField, value);
        var rec = combo.store.getAt(idx);
        return (rec === null ? '' : rec.get(combo.displayField) );
      };
    }
    // the combo store
    var store = new Ext.data.SimpleStore({
      fields: [ "value", "text" ],
      data: [
        [ 1, "Option 1" ],
        [ 2, "Option 2" ]
      ]
    });
    // the edit combo
    var combo = new Ext.form.ComboBox({
      store: store,
      valueField: "value",
      displayField: "text"
    });
    
    
    // demogrid
    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            {header: 'Name',  dataIndex: 'name', editor: 'textfield'},
            {header: 'Email', dataIndex: 'email', flex:1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {header: 'Phone', dataIndex: 'phone'},
            {header: 'id', dataIndex: 'id', editor: combo, renderer: comboBoxRenderer(combo)}
        ],
        selType: 'cellmodel',
        plugins: [
            Ext.create('Ext.grid.plugin.CellEditing', {
                clicksToEdit: 1
            })
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });