简体   繁体   English

Extjs4网格编辑器远程组合框displayValue

[英]Extjs4 grid editor remote combobox displayValue

I have a grid with some columns to be edited. 我有一个网格,其中包含一些要编辑的列。 One of the columns should be edited through a combobox. 其中一列应通过组合框进行编辑。 The combobox store is remote and is of a key value pair type: 组合框存储是远程的,并且是键值对类型:

['id', 'title']

The combobox valueField is id and the displayValue is title. 组合框valueField为id,displayValue为title。 When editing a cell my combobox loads the store, the displayValue is selected and the valueField is returned to the grid editor. 编辑单元格时,我的组合框加载了商店,选择了displayValue,并将valueField返回到网格编辑器。 So the cell is then filled with the valueField. 然后用valueField填充单元格。

My question is: how do I get the cell to render the displayValue? 我的问题是:如何让单元格呈现displayValue? Just selecting it from the store is not good enough since the render happens before the store is loaded. 只是从商店中选择它是不够好的,因为渲染发生在加载商店之前。 My code for now (that works with local stores only): 我现在的代码(仅适用于本地商店):

{
    header: 'Column title',
    dataIndex: 'id',
    displayField: 'title',
    editor: {
        xtype: 'combo',
        valueField: 'id',
        store: 'myStore',
        displayField: 'title'
    },
    renderer: function(value) {
        //How do I find the editors combobox store?
        store = new myStore();
        index = store.findExact('id', value);
        if (index != -1) {
            rs = store.getAt(index).data;
            return rs.title;
        }
        return value;
    }
}

This is how I did it: 我就是这样做的:

I have 2 stores, storeA for the grid and storeB for the value to be selected (as described above). 我有2个商店,storeA用于网格,storeB用于要选择的值(如上所述)。 Both stores have an ID from storeB. 两个商店都有来自storeB的ID。

I ended up adding a field to storeA - the title for storeB ID. 我最终向storeA添加了一个字段 - storeB ID的标题。 And in the grid i have this title as a hidden column. 在网格中,我将此标题作为隐藏列。 And in the combobox editor column i use this renderer: 在组合框编辑器列中,我使用此渲染器:

  renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
       //Title is the title for the storeB ID
       return store.data.items[rowIndex].data.title;
  }

On the grid I have a listener for the edit event to update the column containing the title with the title from the combobox: 在网格上我有一个edit事件的监听器来更新包含标题的列,其中包含组合框中的标题:

  grid.on('edit', function(editor, e, eOpts ) {
       grid.store.data.items[e.rowIdx].set('title', editor.editors.items[0].field.rawValue)
  })

Hope this helps someone else! 希望这有助于其他人!

In your model definitions, what is the type of 'id'. 在您的模型定义中,'id'的类型是什么。 If you define as 'int', you have to: 如果定义为'int',则必须:

index = store.findExact('id', parseInt(value));

Here's the answer, bring the combo store into the scope of your renderer: 这是答案,将组合商店带入渲染器的范围:

http://jsfiddle.net/WRXcw/3/ http://jsfiddle.net/WRXcw/3/

Ext.define('GridModel', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int'
    },{
        name: 'type_id',
        type: 'int'
    }]
});

Ext.define('ComboModel', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int'
    },{
        name: 'label',
        type: 'string'
    }],
    statics: {
        TYPE_OPTION_ONE:   1,
        TYPE_OPTION_TWO:   2,
        TYPE_OPTION_THREE: 3,
        TYPE_OPTION_FOUR:  4
    }
});

var comboStore = Ext.create('Ext.data.Store', {
    model: 'ComboModel',
    data: [{
        id: 1,
        label: '1st Option'
    },{
        id: 2,
        label: '2nd Option'
    },{
        id: 3,
        label: '3rd Option'
    }]
});

var gridStore = Ext.create('Ext.data.Store', {
    model: 'GridModel',
    data: [{
        id: 1,
        type_id: ComboModel.TYPE_OPTION_ONE
    },{
        id: 2,
        type_id: ComboModel.TYPE_OPTION_TWO
    },{
        id: 3,
        type_id: ComboModel.TYPE_OPTION_THREE
    },{
        id: 4,
        type_id: ComboModel.TYPE_OPTION_TWO
    }]
});

Ext.widget('grid', {
    title: 'Rendering Combos',
    width: 650,
    height: 500,
    renderTo: 'ct',
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ],
    store: gridStore,
    forceFit: true,
    columns: [{
        dataIndex: 'id',
        header: 'ID'
    },{
        dataIndex: 'type_id',
        header: 'Type',
        editor: {
            xtype: 'combobox',
            displayField: 'label',
            valueField: 'id',
            queryMode: 'local',
            store: comboStore,
            allowBlank: true
        },
        renderer: function(value) {
            var rec = comboStore.getById(value);

            if (rec)
            {
                return rec.get('label');
            }

            return '—';
        }
    }]
});

Value is not the only parameter passed in to the renderer see docs: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.column.Column-cfg-renderer 值不是传递给渲染器的唯一参数,请参阅docs: http//docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.column.Column-cfg-renderer

value : Object value :对象

 The data value for the current cell **metaData** : Object A collection of metadata about the current cell; 

can be used or modified by the renderer. 可以由渲染器使用或修改。 Recognized properties are: tdCls, tdAttr, and style. 已识别的属性包括:tdCls,tdAttr和style。

 **record** : Ext.data.Model The record for the current row **rowIndex** : Number The index of the current row **colIndex** : Number The index of the current column **store** : Ext.data.Store The data store **view** : Ext.view.View The current view 

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

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