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

2024-03-06

在 ExtJs 中,有很多过滤网格的选项。文档中有两个很好的示例,如中引用的这个问题 https://stackoverflow.com/questions/11518853/extjs-4-grid-filtering.

  1. 远程过滤 http://docs.sencha.com/extjs/4.2.2/#!/example/grid/infinite-scroll-with-filter.html
  2. 本地过滤 http://docs.sencha.com/extjs/4.2.2/#!/example/grid-filtering/grid-filter-local.html

但是,将过滤器隐藏在默认下拉菜单中Ext.ux.grid.FiltersFeature http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.ux.grid.FiltersFeature对我来说看起来真的很尴尬。一个很好的符合人体工程学的选择是在列标题中创建搜索字段,就像 @Ctacus 所示他的问题 https://stackoverflow.com/questions/21877638/extjs-4-2-how-to-set-grid-filter-height-when-header-text-is-wrapped.

如何才能实现这一目标?


经过对稀疏文档的大量研究,并感谢 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 http://docs.sencha.com/extjs/4.2.2/#!/api/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,以下错误及其解决方法 https://stackoverflow.com/q/42064892/1951708与此相关,否则该列不能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())
        }
    }
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 如何将背景颜色(或自定义 css 类)应用于网格中的列 - ExtJs 4?

    看起来应该很简单 但我根本无法完成此任务 我什至不需要动态完成它 例如 假设我有一个简单的 2 列网格设置 如下所示 columns header USER dataIndex firstName width 70 cls red head
  • ExtJS GridPanel 中的垂直滚动条

    我正在开发一个项目 其中页面上有一个 GridPanel 该面板可以显示任意数量的行 并且我设置了 autoHeight 属性 这会导致 GridPanel 扩展以适合行数 我现在想要一个水平滚动条 因为在某些分辨率下 并非所有列都会显示
  • Extjs Restful Store,批量发送请求?

    我创建了一个带有商店配置的网格组件 如下所示 Create the store config store new Ext data Store restful true autoSave false batch true writer ne
  • Extjs 4.0.7,编辑器网格 - 如何获取更新的单元格值?

    我需要在控制器中获取 检索 更新的单元格值 MVC 所以我尝试了这个 var modified this getItemGrid getStore getUpdatedRecords console log modified return
  • 突出显示 extjs4 折线图的一部分

    在 extjs 4 1 1a 中 下面的代码是折线图的工作示例 现在我需要在给定的最小和最大时间戳上突出显示该图表的一部分 xtype chart store ChartData height 100 width 100 legend po
  • Webkit 是否有 CSS3 网格布局的有效实现?

    CSS 网格布局 编辑草案 2011 年 11 月 21 日 http dev w3 org csswg css3 grid align 我正在制作一个原型 该原型将在选定的设备和浏览器上向客户展示 目前我并不担心跨浏览器兼容性 IE10开
  • GridSplitter 从右侧调整大小 - 奇怪的行为

    使用 Kaxaml 从左侧调整大小可以按预期工作
  • 最小的 ExtJS 包是什么?

    有谁知道 Ext JS 2 2 所需的最少文件吗 我知道 ExtJS 网站有一个功能 build http extjs com products extjs build ExtJS ext js 的小版本 作为 ext all js 的替代
  • ExtJS 4:克隆商店

    我正在尝试找出如何克隆Ext data Store不保留旧的参考 让我用一些代码更好地解释一下 这是源商店 var source Ext create Ext data Store fields name age data name foo
  • 过滤数组以获取唯一字段值

    我知道有很多方法可以过滤数组中的唯一值 但是如何过滤数组中具有给定字段的唯一值的对象呢 例如我有 obj1 obj2 obj3 其中每个对象具有以下形式 firstName lastName 如何过滤数组以最终得到一个最终数组 其中所有对象
  • 选择月份或年份时,ExtJS 5 xtype 日期字段不起作用

    当单击下拉菜单选择个别月份 年份时 对话框消失 就像我试图单击一样 fiddle https fiddle sencha com fiddle 9m6 https fiddle sencha com fiddle 9m6 Ext onRea
  • 过滤器返回 true 或 false

    我正在使用过滤器在 data it 返回对象中查找 id 它返回的对象不是 true 或 false 如果我怎样才能返回 true 或 falseval recoredId valueId var hasMatch data filter
  • 如何向 Ext.tree.TreePanel 添加复选框?

    我创建了这个简单的树 var children text My Layers children new Ext tree TreeNode text test1 leaf true new Ext tree TreeNode text te
  • ExtJS:简单表单忽略 formBind

    我有一个小问题让我发疯了好几天 我有一个表单面板 Ext define EC view PasswordPanel extend Ext form Panel alias widget pwdpanel bodyPadding 15 ini
  • Auth0 isAuthenticated() 始终为 false

    我正在使用 Extjs 并且我用过本教程 https github com auth0 samples auth0 javascript samples tree master 01 Login设置应用程序和 auth0 这是登录代码 us
  • django-过滤器和聚合函数

    这是一个特定于应用程序的问题 Django 过滤器 https github com alex django filter 这里给没用过的人简单说明一下 f ProductFilter request GET queryset Produc
  • 按值数组过滤对象数组中的嵌套数组

    考虑以下对象数组 guid j5Dc9Z courses id 1 name foo guid a5gdfS courses id 2 name bar
  • 如何组合过滤条件

    过滤器类函数接受一个条件 a gt Bool 并在过滤时应用它 当您有多个条件时 使用过滤器的最佳方法是什么 使用了应用函数 liftA2 而不是 liftM2 因为出于某种原因我不明白 liftM2 在纯代码中如何工作 liftM2 组合
  • 在shiny中过滤传单地图数据

    我在用传单地图设置这个闪亮的东西时遇到了麻烦 我的原帖 https stackoverflow com questions 50111566 applying leaflet map bounds to filter data within
  • 为什么 dplyr filter() 不能在函数内工作(即使用变量作为列名)?

    使用 dplyr 函数对数据进行过滤 分组和变异的函数 基本管道序列在函数之外工作得很好 这就是我使用真实列名称的地方 将其放入一个函数中 其中列名称是一个变量 并且某些函数可以工作 但有些函数则不能 尤其是 dplyr filter 例如

随机推荐