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

Lor*_*yer 8 grid extjs filter extjs4 extjs4.2

在ExtJs中,有许多选项可以过滤网格.文档中有两个很好的例子,比如这个问题中提到的.

  1. 远程过滤
  2. 本地过滤

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

怎么能实现这一目标?

Lor*_*yer 23

经过稀疏文档的大量研究,并且由于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
    }]
},
Run Code Online (Sandbox Code Playgroud)

以下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())
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

对于ExtJs 6.2.0,以下错误及其解决方法与此相关,否则无法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())
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

  • 对于ExtJS 5.1:http://dev.sencha.com/ext/5.1.0/examples/kitchensink/#grid-filtering不是那么直观,但它有效并且是正式的. (2认同)