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

标签 extjs filter grid extjs4 extjs4.2

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

  • Remote filtering
  • Local filtering

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

    如何实现呢?

    最佳答案

    经过对稀疏文档的大量研究,并感谢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与此相关,否则无法对该列进行flex ed。

    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-使用列标题中的搜索字段过滤网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22014855/

    相关文章:

    grid - 如何使用Matplotlib在对数-对数图的所有刻度上显示对数间隔的网格线?

    javascript - d3.js - 未捕获的类型错误 : Cannot read property 'data' of undefined

    javascript - 使用 Javascript 提交表单

    javascript - 使用 React Native 进行基于标签的过滤

    java - Grid Vaadin - 数据显示在 grid-scroller 下方

    java - 网格模拟 : how to send message among peer nodes through a router?

    extjs - 如何将GeoExt3添加到Extjs 6 MVVM体系结构?

    javascript - 在 extjs4 中如何在另一个函数中调用函数?

    Javascript 将参数传递给过滤器函数?

    filter - 结合 PowerBI DAX 过滤器和 SELECTCOLUMN