简体   繁体   English

ExtJS 4.2网格单元格编辑如何将列值与组合显示值绑定

[英]ExtJS 4.2 grid cellediting how to bind column value with combo display value

I have a grid with cellediting plugin. 我有一个带有cellediting插件的网格。

One of my columns is a int field which represents a value in a combo store when editing the cell. 我的一列是int字段,它表示编辑单元格时组合存储中的值。

I want to know how can I have this column to show the displayfield instead of the value before I edit the cell. 我想知道在编辑单元格之前如何使此列显示显示字段而不是值。

Here are the images for reference: 以下是供参考的图像:

在此处输入图片说明

The values 2,0,0 etc are my "accesstype" field that is an int. 值2,0,0等是我的“ int”字段。

If I click the cell to edit that why I get: 如果我单击该单元格来编辑,我为什么会得到:

在此处输入图片说明

And If I select a value then instead of showing the text I get again the int value. 而且,如果我选择一个值,则不再显示文本,而是再次得到int值。

在此处输入图片说明

How can I show the display field instead of the value field? 如何显示显示字段而不是值字段?

For the ease of access here's the answer from the other question : 为了便于访问,这是另一个问题的答案:

You can set a default value for the combo. 您可以设置组合的默认值。 That should then get that rendered at startup. 然后应该在启动时将其渲染。

Use cell renderer to render the displayField of the combo into your grid. 使用单元格渲染器将组合的displayField渲染到网格中。 Following a working example that can be poster in one of the API code boxes 遵循一个可以在其中一个API代码框中张贴的工作示例

Working JSFiddle 工作中的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 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 combo store
var store = new Ext.data.SimpleStore({
  fields: [ "value", "text" ],
  data: [
    [ 1, "Option 1" ],
    [ 2, "Option 2" ]
  ]
});
// 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: Ext.getBody()
});

只需删除该组合的配置属性“ valueField”,从下拉列表中选择值后,它将显示displayField值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM