1

我正在使用 ExtJS 4.2。我正在使用下面的代码在 ExtJS 网格中呈现组合框。这是我在 Grid 内的 Combobox 上的第一次尝试,我的最终目标是除此之外的几个级别。但我被困在第一步,即添加一个组合框并在网格的组合框中显示一个 Json 结果。下面是我的代码:

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
    });
});

我从后端返回的 JSON 对象是: {"sucess":true,"secureUsers":[{"username":"admin","id":1},{"username":"super","id":2 },{"用户名":"用户","id":3}]}

结果只是一个带有两个标题 ID 和用户名的 Grid 以及一一列出的记录。但是,我没有在每一行的用户名上看到任何组合框。即使我单击,它们也不会变成组合框(我读到这是某处的行为)。此外,我也没有在调试器工具上看到任何运行时错误。

没有组合

你能告诉我我哪里错了吗?是因为我对 Grid 和组合使用相同的 userStore 吗?

4

2 回答 2

2

您需要将CellEditing插件添加到您的网格配置中:

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
        })
    ]
});
于 2013-06-09T22:01:00.473 回答
0

(西班牙语)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')]
    });
});
于 2013-11-25T06:31:35.403 回答