【问题标题】:Ext Js combobox value after onchange is value instead of labelonchange 后的 Ext Js 组合框值是值而不是标签
【发布时间】:2013-02-01 02:54:45
【问题描述】:

只是对应该很容易解决的事情感到沮丧,但我的头脑太简单了,看不到它:)

我有一个网格,其中 1 列是一个组合框。这件事工作得很好,并且通过我的 ajax 请求发送了正确的值,但是在我编辑了网格行之后,组合框消失了,并且出现的值不是标签,而是值。

editor: new Ext.form.field.ComboBox({
            typeAhead: true,
            lazyRender: true,
            store: new Ext.data.ArrayStore({
                fields: ['contact', 'contactLabel'],
                data: [
                    [1,'Jan'],
                    [2,'Jeroen'],
                    [3,'Mattijs'],
                    [4,'Sven'],
                    [5,'Thomas'],
                    [6,'Yoran']
                ]
            }),
            valueField: 'contact',
            displayField: 'contactLabel',
            hiddenName: 'contact'
        })

所以发生的情况是,当我将组合框更改为“Thomas”时,单元格的值变为“5”,而不是“Thomas”。我认为定义值/显示字段会有所不同,但事实并非如此。

谁知道答案?

【问题讨论】:

    标签: extjs combobox grid


    【解决方案1】:

    我不太确定我是否正确。如果是这样,您将需要一个渲染器。我想下面的示例代码应该会告诉你是否是这种情况。

    // 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 edit combo
    var combo = new Ext.form.ComboBox({
      store: store,
      valueField: "value",
      displayField: "text"
    });
    

    查看这个完整的工作示例(cellEditing + rowEditing)JSFiddle ()

    这是完整的代码

    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone', 'id'],
        data:{'items':[
            {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224","id": 0},
            {"name":"Bart", "email":"bart@simpsons.com", "phone":"555-222-1234","id": 1},
            {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244","id": 2},
            {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254","id": 3}
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
        // the combo store
    var store = new Ext.data.SimpleStore({
      fields: [ "value", "text" ],
      data: [
        [ 0, "Option 0" ],
        [ 1, "Option 1" ],
        [ 2, "Option 2" ],
        [ 3, "Option 3" ]
      ]
    });
    
    // 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 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: 'cell'
    });
    // 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: 'rowmodel',
        plugins: [
            Ext.create('Ext.grid.plugin.RowEditing', {
                clicksToEdit: 1
            })
        ],
        height: 200,
        width: 400,
        renderTo: 'row'
    });
    

    html

    <div id="cell"></div>
    <div id="row"></div>
    

    【讨论】:

    • 我一定在这里遗漏了一些东西。我得到: TypeError: rec is undefined return (rec === null ? '' : rec.get(combo.displayField));我正在使用 rowEditing 插件顺便说一句。不知道这会不会有什么不同?
    • @DinandDerksen 好吧,不。那不应该有任何区别。 JSFiddle 当前不可用,因此我无法提供带有 rowEditing 插件的示例。尝试将rec === null ?这一行改为rec ?,注意boxRender必须知道编辑器使用的combo实例。
    • 非常感谢,sra!我已经让它完全按照我的意愿工作了。我唯一需要更改的是存储数组中的键必须标记为字符串,而不是整数。不知何故,当编辑器被激活时,我得到了“3”,而不是“选项 3”。无论如何...它通过 ajax 发送正确的值,并在保存前后的组合框中显示正确的值。非常感谢!
    • 请记住,此解决方案仅在整个商店已从数据库加载时才有效。或者,至少是与 ID 匹配的记录。例如,employee 组合框指向一家拥有 10,000 名员工(使用分页)的商店,很有可能找不到记录。我目前正在研究一个更好的解决方案,因为我们的组合有很多记录,我们不能为每个网格行加载。
    【解决方案2】:

    试试:

    data: [
        {contact: 1, contactLabel: 'Jan'},
        ...
    ]
    

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2018-12-07
      • 2018-10-27
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多