代码之家  ›  专栏  ›  技术社区  ›  Lorenz Meyer

ExtJs-在列标题中使用搜索字段过滤网格

  •  9
  • Lorenz Meyer  · 技术社区  · 10 年前

    在ExtJ中,有许多选项可以过滤网格。文档中有两个很好的示例,如 this question .

    1. Remote filtering
    2. Local filtering

    但是,将过滤器隐藏在默认下拉菜单中 Ext.ux.grid.FiltersFeature 我觉得很尴尬。一个符合人体工程学的好选择是在列标题中创建搜索字段,如 his question .

    如何做到这一点?

    1 回复  |  直到 7 年前
        1
  •  24
  •   Community kfsone    7 年前

    在对稀疏的文档进行了大量研究之后,由于SO中的大量问题和答案,我想出了一个简单的类,它添加了这个功能并允许配置。

    它看起来像这样:

    Search filter fields in column header

    将此字段添加到网格中,如下所示:

    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 与此相关,否则该列不能 flex 预计起飞时间。

    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())
            }
        }
    })