rajugaadu
rajugaadu

Reputation: 714

Combobox not rendering in ExtJS Grid

I am using ExtJS 4.2. I am using the below code to render a combobox in a ExtJS Grid. This is my first attempt on a Combobox inside a Grid and my end target is a few levels beyond this. But I am stuck at the first step i.e to add a combobox and display a Json result in the comboboxes of the Grid. Below is my code:

Ext.onReady(function() {

    Ext.require([ 'Ext.data.*', 'Ext.grid.*' ]);


    // ************************** Define Data Models ************************ //

    Ext.define('SecureUser', {
        extend : 'Ext.data.Model',
        fields : [ 'id', 'username' ]
    });


    // ************************** Define Data Stores ************************ //

    //The Store contains the AjaxProxy as an inline configuration
    var userStore = Ext.create('Ext.data.Store', {
        autoLoad : true,
        model : 'SecureUser',
        proxy : {
            type : 'ajax',
            api: {
                read: 'secureUserSecureRole/listJSON',
            },
            reader : {
                type : 'json',
                successProperty: 'success',
                root : 'secureUsers',
                messageProperty: 'message'
            },
            writer : {
                type : 'json',
                encode: 'true',
                root: 'secureUsers'                 
            }
        }
    });


    //renderer needed to display correct field when not editing combo (see API)
    Ext.util.Format.comboRenderer = function(combo) {
        return function(value) {
            var record = combo.findRecord(combo.valueField, value);
            return record ? record.get(combo.displayField)
                    : combo.valueNotFoundText;
        }
    }

    var combo = new Ext.form.ComboBox({
        typeAhead: true,
        triggerAction: 'all',
        mode: 'remote',
        store: userStore,
        valueField: 'username',
        displayField: 'username'
    });

    // Grid-columns with meta-data from backend.
    var recipeColumns = [ {
        header : "ID",
        width : 40,
        sortable : true,
        dataIndex : 'id'
    },{
        header : 'User Name',
        width : 130,
        dataIndex : 'username',
        editor : combo,
        renderer: Ext.util.Format.comboRenderer(combo)
    }];

    // create youbrew.recipe.Grid instance (@see recipeGrid.js)
    var userGrid = Ext.create('Ext.grid.Panel', {
        renderTo : document.body,
        store: userStore,
        width : 200,
        height : 300,
        clicksToEdit : 'auto',
        columns : recipeColumns
    });
});

My JSON object returned from backend is: {"sucess":true,"secureUsers":[{"username":"admin","id":1},{"username":"super","id":2},{"username":"user","id":3}]}

The result is just a Grid with two headers ID and User Name and the records listed one by one. But, I don't see any comboboxes on the username(s) in each row. Even when I click, they don't turn into comboboxes ( I read this is the behavior somewhere ). Also, I don't see any runtime errors on the debugger tool too.

No Combo

Can you tell me where am I going wrong? Is it because I am using the same userStore for both Grid and the combo as well?

Upvotes: 1

Views: 3807

Answers (2)

Luis Castrillo
Luis Castrillo

Reputation: 1

(Spanish) A mi me funciono, de la siguiente forma:

Ext.onReady(function() {

    Ext.require([ 'Ext.data.*', 'Ext.grid.*' ]);


    // ************************** Define Data Models ************************ //

    Ext.define('SecureUser', {
        extend : 'Ext.data.Model',
        fields : [ 'id', 'username' ]
    });


    // ************************** Define Data Stores ************************ //

    //The Store contains the AjaxProxy as an inline configuration
    var userStore = Ext.create('Ext.data.Store', {
        autoLoad : true,
        model : 'SecureUser',
        proxy : {
            type : 'ajax',
            api: {
                read: 'secureUserSecureRole/listJSON',
            },
            reader : {
                type : 'json',
                successProperty: 'success',
                root : 'secureUsers',
                messageProperty: 'message'
            },
            writer : {
                type : 'json',
                encode: 'true',
                root: 'secureUsers'                 
            }
        }
    });

    var combo = new Ext.form.ComboBox({
        typeAhead: true,
        triggerAction: 'all',
        mode: 'remote',
        store: userStore,
        valueField: 'username',
        displayField: 'username'
    });

    var comboRenderer = function(value, p, record) {
        var record = combo.findRecord(combo.valueField, value);
        return record ? record.get(combo.displayField) : value;
    }

    // Grid-columns with meta-data from backend.
    var recipeColumns = [ {
        header : "ID",
        width : 40,
        sortable : true,
        dataIndex : 'id'
    },{
        header : 'User Name',
        width : 130,
        dataIndex : 'username',
        editor : combo,
        renderer: comboRenderer
    }];

    // create youbrew.recipe.Grid instance (@see recipeGrid.js)
    var userGrid = Ext.create('Ext.grid.Panel', {
        renderTo : document.body,
        store: userStore,
        width : 200,
        height : 300,
        clicksToEdit : 'auto',
        columns : recipeColumns,
        plugins: [Ext.create('Ext.grid.plugin.CellEditing')]
    });
});

Upvotes: 0

Ye Liu
Ye Liu

Reputation: 8976

You need to add CellEditing plugin to your grid config:

var userGrid = Ext.create('Ext.grid.Panel', {
    renderTo : document.body,
    store: userStore,
    width : 200,
    height : 300,
    columns : recipeColumns,
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ]
});

Upvotes: 2

Related Questions