简体   繁体   English

在网格编辑器中对组合框进行Extjs过滤

[英]Extjs filtering of a combobox in a grid editor

I have a grid with a combobox editor. 我有一个带有组合框编辑器的网格。 I want to filter the combobox store according to a value in the row's record. 我想根据行记录中的值过滤组合框存储。

What I got now is this: 我现在得到的是:

editor: {
    xtype: 'combo',
    forceSelection: true,
    store: 'ship.Transportmodes',
    displayField: 'name',
    valueField: 'id',
    queryMode: 'local',
    listeners: {
        expand: function(combo){
            var shipper = combo.up('grid').editingPlugin.activeRecord.get('shipper');
            combo.store.filterBy(function(record){
                console.log(record.get('shippers').indexOf(shipper))
                return record.get('shippers').indexOf(shipper) != -1
            })
        }
    },
    editable: false
}},

This is working, but there is a problem: when the dropdown is showing on the top of the field, the remaining options appear far from the field, the space for the removed options is still allotted. 这是可行的,但是存在一个问题:当下拉菜单显示在字段顶部时,其余选项显示在远离字段的位置,但仍为已分配选项分配了空间。 屏幕截图

I tried to filter the store on any of these events: 我尝试对以下任何事件进行过滤:

  • grid : beforeedit 网格:beforeedit
  • combo : beforerender 组合:先渲染
  • combo : render 组合:渲染
  • combo : afterrender 组合:后渲染

In each case the result is the following: The store gets filtered correctly, but at the time the combobox is expanded, the filter is cleared. 在每种情况下,结果如下:正确过滤了存储,但是在组合框展开时,清除了过滤器。

I would like to understand what happens. 我想了解会发生什么。 Why does a combobox always clear the filters of its store before displaying the list of options ? 为什么组合框在显示选项列表之前总是清除其商店的过滤器?

Can anybody give an insight behind the scenes ? 任何人都可以在幕后提供见解吗? Or tell me what I'm missing in the picture ? 还是告诉我图片中我缺少什么?

You can do like this: 您可以这样:
Fiddle link: https://fiddle.sencha.com/#fiddle/1hle 小提琴链接: https : //fiddle.sencha.com/#fiddle/1hle

Ext.application({
    name : 'Fiddle',

    simpsonsStore : Ext.create('Ext.data.Store', {
        storeId : 'simpsonsStore',
        fields : ['name', 'email'],
        data : [
            {name : 'Lisa',email : 'lisa@simpsons.com',id:1}, 
            {name : 'Bart', email : 'bart@simpsons.com',id:2}, 
            {name : 'Homer', email : 'homer@simpsons.com',id:3},
            {name : 'Marge',email : 'marge@simpsons.com',id:4}]
    }),

    tagfieldstore : Ext.create('Ext.data.Store', {
        fields : ['tag', 'field'],
        data : [
            {aid:1,tag : 'tag1',field : 'lisa record1'}, 
            {aid:1,tag : 'tag3',field : 'lisa record2'}, 
            {aid:3,tag : 'tag1',field : 'homer record3'}, 
            {aid:3,tag : 'tag3',field : 'homer record4'},
            {aid:2,tag : 'tag1',field : 'bart record1'}, 
            {aid:2,tag : 'tag3',field : 'bart record2'}, 
            {aid:4,tag : 'tag1',field : 'marge record3'}, 
            {aid:4,tag : 'tag3',field : 'marge record4'}]
    }),

    launch : function() {

        Ext.create('Ext.grid.Panel', {
            title : 'Simpsons',
            store : this.simpsonsStore,
            columns : [{
                flex :1 ,
                text : 'Name',
                dataIndex : 'name',
            }, {
                text : 'Email',
                dataIndex : 'email',
                flex : 1
            }, {
                xtype : 'widgetcolumn',
                cellWrap : true,
                text : 'Phone',
                flex : 1,
                widget : {
                    xtype: 'combo',
                    store : this.tagfieldstore,
                    valueField : 'tag',
                    displayField : 'field',
                    growMax : true, 
                    listeners:{
                       expand:function(combo){
                           combo.store.clearFilter();
                          var id = combo.$widgetRecord.data.id;
                           combo.store.filterBy(function(rec){
                                return rec.data.aid == id;
                          });
                       }
                    }

                }
            }],
            flex : 1,
            renderTo : Ext.getBody()
        });
    }
});

Finally I figured it out. 终于我明白了。 The problem was not that ExtJs is clearing filters on a combobox' store. 问题不在于ExtJ正在清除组合框存储中的筛选器。 The problem came from a missunderstanding of the working of filters. 问题来自对过滤器工作的误解。 In fact filterBy filters the store only temporarily, but does not save that filter in the store. 实际上, filterBy只是临时过滤商店,但不会将该过滤器保存在商店中。

On the other hand, addFilter({filterFn: ...}) adds the filter to the store's filters collection. 另一方面, addFilter({filterFn: ...})将过滤器添加到商店的filters集合中。

Filters that are added on a store with addFilter will still apply after rendering that combobox, filters that are applied with filterFn are volatile. 呈现该组合框后,使用addFilter在商店中添加的过滤器仍然适用,使用filterFn过滤器是易失性的。

Now I can add the filter on grid's beforeedit event, and remove it on edit and canceledit . 现在,我可以在网格的beforeedit事件上添加过滤器,并在editcanceledit beforeeditcanceledit

favorite 喜爱

I have a grid with a combobox editor. 我有一个带有组合框编辑器的网格。 I want to filter the combobox store according to a value in the row's record. 我想根据行记录中的值过滤组合框存储。

What I got now is this: 我现在得到的是:

editor: {
    xtype: 'combo',
    forceSelection: true,
    store: 'ship.Transportmodes',
    displayField: 'name',
    valueField: 'id',
    queryMode: 'local',
    // no listener here
    editable: false
}

Controller: 控制器:

onBeforeedit: function(edit, e){
    this.getStore('mystore').addFilter(
        {id: 'shipperFilter', 
        filterFn: function(record){
            return e.record.get('shippers').indexOf(this.shipper) != -1
        }
    })
}

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

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