【问题标题】:Extjs filtering of a combobox in a grid editor网格编辑器中组合框的 Extjs 过滤
【发布时间】:2016-09-30 11:24:11
【问题描述】:

我有一个带有组合框编辑器的网格。我想根据行记录中的值过滤组合框存储。

我现在得到的是:

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
}},

这是可行的,但有一个问题:当下拉菜单显示在字段顶部时,其余选项显示在远离字段的位置,仍为已删除选项分配空间。

我尝试根据以下任何事件过滤商店:

  • 网格:编辑前
  • 组合:渲染前
  • 组合:渲染
  • 组合:后渲染

在每种情况下,结果如下:商店被正确过滤,但在组合框展开时,过滤器被清除。

我想了解会发生什么。为什么组合框总是在显示选项列表之前清除其商店的过滤器?

任何人都可以提供幕后的见解吗?或者告诉我我在图片中缺少什么?

【问题讨论】:

  • 你可以试试clearFilterOnBlur:false 这样过滤器就不会被清除了。
  • @alexander 谢谢,我会试试的。我看到了这个配置,但我认为它只会在模糊组合时改变行为。
  • @alexander 不,不幸的是这没有帮助。 clearFilterOnBlur 仅适用于键入时使用的过滤器。

标签: extjs filter combobox extjs4.2


【解决方案1】:

你可以这样做:
小提琴链接: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()
        });
    }
});

【讨论】:

  • 您的小提琴不适用于 ExtJs 4.2。看起来 Extjs 6 不再有我遇到的问题。这意味着我真的应该努力升级。
  • 在 Extjs 4.2 中,网格面板的小部件列不起作用。你需要更新你的 extjs 版本。
  • 我会更新,但这不是一个直接的选择。我认为这需要一周时间,因为该应用程序非常重要,并且实现了 Ext 4.2 不原生但现在在 Ext 6 中原生的功能。
  • 我现在了解您的解决方案与我的需求之间的区别。我需要一个组合框作为celleditor,您提出了一个组合框作为小部件列。这只会引出另一个问题:widgetcolumn 中的组合框是否充当编辑器?据我了解,widgetcolumn 组合框的选定值未绑定到行记录的值。这是正确的,还是我错过了什么?
【解决方案2】:

我终于想通了。问题不在于 ExtJs 正在清除组合框商店中的过滤器。问题来自对过滤器工作的误解。事实上filterBy 只是暂时过滤存储,但不会将该过滤器保存在存储中。

另一方面,addFilter({filterFn: ...}) 将过滤器添加到商店的 filters 集合中。

使用addFilter 添加到商店的过滤器在呈现该组合框后仍将应用,使用filterFn 应用的过滤器是不稳定的。

现在我可以在网格的beforeedit 事件上添加过滤器,并在editcanceledit 上删除它。

最喜欢的

我有一个带有组合框编辑器的网格。我想根据行记录中的值过滤组合框存储。

我现在得到的是:

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

控制器:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多