【问题标题】:ExtJs - Filter a grid with a search field in the column headerExtJs - 在列标题中使用搜索字段过滤网格
【发布时间】:2014-02-25 12:54:55
【问题描述】:

在 ExtJs 中,有许多过滤网格的选项。文档中有两个很好的示例,如 this question 中引用的。

  1. Remote filtering
  2. Local filtering

但是,将过滤器隐藏在Ext.ux.grid.FiltersFeature 的默认下拉菜单中对我来说真的很尴尬。一个符合人体工程学的良好选择是在列标题中创建搜索字段,例如 @Ctacus 在 his question 中显示的内容。

如何做到这一点?

【问题讨论】:

    标签: extjs filter grid extjs4 extjs4.2


    【解决方案1】:

    经过对稀疏文档的大量研究,并且感谢 SO 中的大量问题和答案,我想出了一个简单的类,它添加了此功能并允许进行配置。

    看起来像这样:

    您在网格中添加此字段,如下所示:

    Ext.define('Sandbox.view.OwnersGrid', {
        extend: 'Ext.grid.Panel',
        requires: ['Sandbox.view.SearchTrigger'],
        alias: 'widget.ownersGrid',
        store: 'Owners',
        columns: [{
            dataIndex: 'id',
            width: 50,
            text: 'ID'
        }, {
            dataIndex: 'name',
            text: 'Name',
        items:[{
            xtype: 'searchtrigger',
            autoSearch: true
        }]
    },
    

    以下configs 是可能的,并且可以像Ext.util.Filter 的文档中描述的那样工作:

    • anyMatch
    • caseSensitive
    • exactMatch
    • operator
    • 另外你可以使用autoSearch。如果为 true,过滤器会在您键入时搜索,如果为 false 或未设置,则必须单击搜索图标以应用过滤器。

    ExtJs 5 / 6 来源:

    Ext.define('Sandbox.view.SearchTrigger', {
        extend: 'Ext.form.field.Text',
        alias: 'widget.searchtrigger',
        triggers:{
            search: {
                cls: 'x-form-search-trigger',
                handler: function() {
                    this.setFilter(this.up().dataIndex, this.getValue())
                }
            },
            clear: {
                cls: 'x-form-clear-trigger',
                handler: function() {
                    this.setValue('')
                    if(!this.autoSearch) this.setFilter(this.up().dataIndex, '')
                }
            }
        },
        setFilter: function(filterId, value){
            var store = this.up('grid').getStore();
            if(value){
                store.removeFilter(filterId, false)
                var filter = {id: filterId, property: filterId, value: value};
                if(this.anyMatch) filter.anyMatch = this.anyMatch
                if(this.caseSensitive) filter.caseSensitive = this.caseSensitive
                if(this.exactMatch) filter.exactMatch = this.exactMatch
                if(this.operator) filter.operator = this.operator
                console.log(this.anyMatch, filter)
                store.addFilter(filter)
            } else {
                store.filters.removeAtKey(filterId)
                store.reload()
            }
        },
        listeners: {
            render: function(){
                var me = this;
                me.ownerCt.on('resize', function(){
                    me.setWidth(this.getEl().getWidth())
                })
            },
            change: function() {
                if(this.autoSearch) this.setFilter(this.up().dataIndex, this.getValue())
            }
        }
    })
    

    对于 ExtJs 6.2.0,following bug and its workaround 与此相关,否则该列不能为 flexed。

    ExtJs 4 来源:

    Ext.define('Sandbox.view.SearchTrigger', {
        extend: 'Ext.form.field.Trigger',
        alias: 'widget.searchtrigger',
        triggerCls: 'x-form-clear-trigger',
        trigger2Cls: 'x-form-search-trigger',
        onTriggerClick: function() {
            this.setValue('')
            this.setFilter(this.up().dataIndex, '')
        },
        onTrigger2Click: function() {
            this.setFilter(this.up().dataIndex, this.getValue())
        },
        setFilter: function(filterId, value){
            var store = this.up('grid').getStore();
            if(value){
                store.removeFilter(filterId, false)
                var filter = {id: filterId, property: filterId, value: value};
                if(this.anyMatch) filter.anyMatch = this.anyMatch
                if(this.caseSensitive) filter.caseSensitive = this.caseSensitive
                if(this.exactMatch) filter.exactMatch = this.exactMatch
                if(this.operator) filter.operator = this.operator
                console.log(this.anyMatch, filter)
                store.addFilter(filter)
            } else {
                store.filters.removeAtKey(filterId)
                store.reload()
            }
        },
        listeners: {
            render: function(){
                var me = this;
                me.ownerCt.on('resize', function(){
                    me.setWidth(this.getEl().getWidth())
                })
            },
            change: function() {
                if(this.autoSearch) this.setFilter(this.up().dataIndex, this.getValue())
            }
        }
    })
    

    【讨论】:

    • 我能够在我的 ExtJS Grid 中实现您的代码。问题之一是有时用户无法使用鼠标单击在列中的 searchTrigger 字段之间切换。 'Tab' 键也可用于在 searchTriggers 之间切换。您是否遇到同样的问题?
    • 我发现在某些情况下,我无法通过鼠标单击访问 serach 字段,但我没有进一步查看,并且我没有识别出该问题何时发生的任何模式。
    • 对于那些使用 extjs5 的人,triggerCls 和 trigger2Cls 应该替换为 Ext.form.field.Text 触发器,请参阅此处docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/…
    • 对于 ExtJS 5.1:dev.sencha.com/ext/5.1.0/examples/kitchensink/#grid-filtering 不是很直观,但它可以工作并且是官方的。
    • 不适合我。 ExtJS 6.0.2。没有错误,但没有显示搜索字段。
    猜你喜欢
    • 2018-03-19
    • 2013-02-13
    • 1970-01-01
    • 2014-11-18
    • 2014-08-03
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多