简体   繁体   English

ExtJs-网格编辑器中用于远程存储组合框的渲染器

[英]ExtJs - Renderer for remote store combobox in grid editor

I know this has been asked somewhere again and again but I can't find a solid answer as standard for this kind of problem. 我知道这个问题一再被问到,但是我找不到一个可靠的答案作为此类问题的标准。

As many have done before I have an editor inside a grid that is a combobox that uses remote store from a database. 正如许多人所做的一样,在网格内部有一个编辑器是一个组合框,它使用数据库中的远程存储。 Now suppose that there is a table in the database with 50,000 records. 现在假设数据库中有一个包含50,000条记录的表。 The combobox only loads the first 15. When I select a record between these 15 everything displays fine. 组合框仅加载前15个。当我在这15个之间选择一条记录时,一切都很好。 To render the displayField instead the valueField , I use the following function 为了渲染displayField而不是valueField ,我使用以下函数

  renderCombobox : function(value, metaData, record, rowIndex, colIndex, store, view, isNewRow){      
      var me      = this,
          columns = me.columns,
          editor, editorStore, editorIndex, displayField, display, rec;

    for(var i=0,j=columns.length;i<j;i++){          
      if(columns[i].getEditor){
          editor = columns[i].getEditor(record);
          if(editor.xtype === 'combobox'){
              editorStore   = editor.getStore().load();
              editorIndex   = editorStore.findExact(editor.valueField, value);
              displayField = editor.displayField;
          }
      }
    }      
    if(editorIndex != -1){
        rec = editorStore.getAt(editorIndex);
        if(rec && rec.get(displayField)){
            display = rec.get(displayField);
        }               
    }      
    return display;        
}

The problem is the following scenario. 问题是以下情况。

If I type ahead, I can find a record that is not between these 15 records. 如果我键入,我会发现一条记录不在这15条记录之间。 For example the record 42,300. 例如记录42,300。 I select it and for the moment everything is OK. 我选择它,目前一切正常。 Now If I click on another field on another editor in the grid (ie datefield) the renderer function for the combobox returns undefined as it tries to find the record with value of the record 42,300 that does not exist in the store. 现在,如果我单击网格中另一个编辑器(即日期字段)上的另一个字段,则组合框的渲染器函数将返回undefined因为它试图查找具有存储中不存在的记录42300的值的记录。 Debugger says that the store contains again only the first 15 records. 调试器说该存储区再次仅包含前15条记录。

Is there any configuration I miss? 我想念任何配置吗? The store needs a limitation. 商店需要限制。 I can't bring 50,000 records from the database at once. 我无法一次从数据库中提取50,000条记录。

我认为该示例完全可以满足您的要求: http : //extjs.eu/ext-examples/#combo-in-grid

The awesome Saki's article gave me the clue to solve this task. Saki出色的文章为我提供了解决此任务的线索。

You don't need any renderers, just add an additional store with all key-value pairs for your combo and add 'edit' event to the grid. 您不需要任何渲染器,只需为组合添加一个包含所有键值对的附加商店,然后将“ edit”事件添加到网格即可。

Here it is. 这里是。

Your additional remote store : 您的其他远程存储:

Ext.define('crm.store.BillAdTarifsStore', {
    extend      : 'Ext.data.Store',
    storeId     : 'BillAdTarifsStore',
    requires    : 'crm.model.BillAdTarifsModel',
    model       : 'crm.model.BillAdTarifsModel',
    autoLoad    : true 
});

it's model: 它的模型:

Ext.define('crm.model.BillAdTarifsModel', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id',        type: 'int'},
        { name: 'VarName',   type: 'string' }
    ],
    proxy   : {
        type    : 'ajax',
        url     : AjaxUrl,
        reader  : {
            type            : 'json',
            idProperty      : 'id',
            root            : 'data',
            totalProperty   : 'total',
            successProperty : 'success',
            messageProperty : 'message'
        },
        extraParams  : {
            Action   : 'DataRequest',
            DataType : 'GetBillAdTarifs'
        }
    }
});

Grid configuration: 网格配置:

plugins: [
    Ext.create('Ext.grid.plugin.CellEditing', {
        clicksToEdit: 1
    })
],
listeners: {
    // this event will change valueField in cell to the displayField (after choosing combo item)
    edit: function(e, eOpts) {
        // eOpts.rowIdx - is the index of row with clicked combo
        var SelectedComboId = Number(Ext.getCmp('ObjectsGrid').getStore().getAt( eOpts.rowIdx ).get('YourGridColumnName'));
        console.log( 'SelectedComboId: '+SelectedComboId );

        if(Number(SelectedComboId) > 0) {
            // ComboItemObj - get field name by index from additional store
            var ComboItemObj = Ext.data.StoreManager.lookup('BillAdTarifsStore').getById(SelectedComboId);
            if ( typeof ComboItemObj.data.id !== null && Number(ComboItemObj.data.id)>0 ) {
                // real combo item is chosen, set field name
                    Ext.getCmp('ObjectsGrid').getStore().getAt(eOpts.rowIdx).set('TrfCianBig',ComboItemObj.data.VarName);
            }
        }
    }

Column configuration can look like this: 列配置如下所示:

editor  : new Ext.form.field.ComboBox({
    triggerAction : 'all',
    forceSelection: false,
    editable    : false,
    allowBlank  : true,
    displayField: 'VarName',
    valueField  : 'id',
    store       : Ext.data.StoreManager.lookup('BillAdTarifsComboStore'),
        listeners: {
                 'change': function(comp, newValue, oldValue, eOpts) {
                               // Selected combo id value
                               var ComboValue  = comp.getValue();
                                // save data by some Ext.Ajax.request
                                },
                 click: {
                            // you can preload combo store with specifig parameters
                            element: 'el', 
                            fn  : function (store, operation, eOpts) {
                                .....
                                ComboStore.load({
                                        params: {
                                            SomeId : SomeParam
                                        }
                                    });

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

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