【问题标题】:Combobox not rendering in ExtJS Grid组合框未在 ExtJS Grid 中呈现
【发布时间】:2013-06-09 21:26:26
【问题描述】:

我正在使用 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},{"username":"用户","id":3}]}

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

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

【问题讨论】:

  • 小提琴对我们来说会更容易
  • 我没有看到您的网格中有 CellEditing 插件,也许这就是原因
  • 嘿乔克斯。感谢您的建议,但当我在这里提到时我没有看到任何这样的需求:all-docs.info/extjs4/docs/api/Ext.form.ComboBox.html
  • 我将尝试将 CellEditing/RowEditing 插件添加到 Grid 并让您知道结果。我刚刚编辑了我的问题,以防更好地了解我的问题。
  • 哦,这就是 Jaux 的伎俩!谢谢,兄弟。你让它工作了 :) 迄今为止任何人在 ExtJS 上最快的“工作”帮助!更多关于你的方式,因为我现在将处理这些组合框和网格以编辑和触发事件。如果我发现我的试验有任何问题,我非常希望有一种方法可以联系或直接联系您!我猜@jaux 提到的作品:)

标签: extjs extjs4 extjs4.2


【解决方案1】:

您需要将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
        })
    ]
});

【讨论】:

    【解决方案2】:

    (西班牙语)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')]
        });
    });
    

    【讨论】:

    • 您的问题是什么?英文的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多