onchange后的Ext Js组合框值是值而不是标签

时间:2013-01-31 13:38:14

标签: extjs combobox grid

只是对一些应该是一个简单的解决方案感到沮丧,但我太简单了,不能看到它:)

我有一个网格,其中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'
        })

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

任何知道答案的人?

2 个答案:

答案 0 :(得分:5)

我不确定我是否帮助你。如果是这样,你需要一个渲染器。我想在代码剪切下面的例子应该会告诉你是否意味着这样的情况。

// 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>

答案 1 :(得分:0)

尝试:

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