将过滤器添加到Extjs网格

时间:2023-02-01 11:21:51

I want to add some filters to my grid when I clicked on the filter button (see below).The filters must be add tot the grid with the given values from the filterpanel form.

当我点击过滤器按钮(见下文)时,我想在网格中添加一些过滤器。过滤器必须使用filterpanel表单中的给定值添加到网格中。

On this page the textfield will be filled in and when I clicked on the filter button the onFilterClick handler will trigger the FilterController.

在此页面上将填写文本字段,当我单击过滤器按钮时,onFilterClick处理程序将触发FilterController。

Ext.define('path.to.filterPanel', {
extend: 'Ext.form.Panel',

xtype: 'filter',
controller: 'dashboard-filter',

viewModel: { 
    type: 'dashboard-filter' 
},

frame: false,
bodyPadding: 10,
layout: 'form',

// the fields 
items: [{
    xtype: 'textfield',
    name: 'firstName',
    id: 'firstName',
    fieldLabel: 'Firstname'
}, {
    xtype: 'textfield',
    name: 'lastName',
    id: 'lastName',
    fieldLabel: 'Lastname'
}],

 buttons: [
    text   : 'Filter',
    handler: 'onFilterClick' // trigger to the controller
}]
});

When clicked on the Filterbutton the values will be pust to this controller.

单击“过滤器”按钮时,将对该控制器施加压力。

Ext.define('path.to.FilterController', {
extend: 'Ext.app.ViewController',
alias: 'controller.dashboard-filter',

onFilterClick: function(button) {

    var form = button.up('form').getForm();

    if (form.isValid()) {
        // form valid 

        var firstName = Ext.getCmp("firstName").getValue();
        var lastName = Ext.getCmp("lastName").getValue();

        // check if there is some input
        if (!!employeeNumber || !!firstName || !!lastName) {


            // add filters but how??


        } 
    } else {
        // form not valid
        console.log("not valid");
    }
}
});

Finally this is the grid file where the filters must be added.

最后,这是必须添加过滤器的网格文件。

Ext.define('path.to.gridPanel, {
extend: 'Ext.grid.Panel',

requires: [
    'Ext.grid.column.Action',
    'App.store.Employees'
],

controller: 'dashboard-gridVieuw',
xtype: 'gridVieuw',
store: 'Employees',

stateful: true,
collapsible: true,
multiSelect: true,

stateId: 'gridController',

initComponent: function () {
    this.store = new App.store.Employees();
    var me = this;

    me.columns = [{
        header   : 'Firstname',
        flex     : 1,
        sortable : true,
        dataIndex: 'firstName'
    }, {
        header   : 'Lastname',
        flex     : 1,
        sortable : true,
        dataIndex: 'lastName'
    }]

me.callParent();
}
});

I use version 5 of extjs.

我使用的是extjs的第5版。

2 个解决方案

#1


You can use filterBy method to filter the underlying store associated with the grid. Here is an example:

您可以使用filterBy方法过滤与网格关联的基础商店。这是一个例子:

Ext.getStore('myStore').filterBy(function(record, id) {
            if (record.get('firstName') === firstName) {
                return true;
            } else {
                return false;
            }
        });

Here is a fiddle demonstrating a working example of a filter

这是一个演示过滤器工作示例的小提琴

#2


Thanks for answering my question. It works for me. I've added the follow OnClick handler in the controller.

谢谢回答我的问题。这个对我有用。我在控制器中添加了跟随OnClick处理程序。

 onFilterClick: function(button) {

    var form = button.up('form').getForm();

    if (form.isValid()) {

        var fieldOne = Ext.getCmp("fieldOne").getValue();
        var fieldTwo = Ext.getCmp("fieldTwo").getValue();

        // check if there is some input
        if (!!fieldOne || !!fieldTwo) {

            // get store
            var store = Ext.data.StoreManager.lookup('store');

            // check if store not empty
            if(!Ext.isEmpty(store)){
                // clear filters and add a few new filters if params not empty
                store.clearFilter(true);

                if (!Ext.isEmpty(fieldOne)) {
                    store.filter("fieldOne",  fieldOne)
                }

                if (!Ext.isEmpty(fieldTwo)) {
                    store.filter("fieldTwo",  fieldTwo)
                }

                // count the filtered rows
               var count = store.snapshot ? store.snapshot.length : store.getCount();

                if (count == 0) {
                    alert("No data found!");
                    store.clearFilter();    
                }
            } else{
                //Store empty
                console.warn("Store's empty");
            }
        } else {
            // no values 
            alert("No entered data!");
        }
    } else {
        // form not valid
        alert("Form not valid!");
    }
}

#1


You can use filterBy method to filter the underlying store associated with the grid. Here is an example:

您可以使用filterBy方法过滤与网格关联的基础商店。这是一个例子:

Ext.getStore('myStore').filterBy(function(record, id) {
            if (record.get('firstName') === firstName) {
                return true;
            } else {
                return false;
            }
        });

Here is a fiddle demonstrating a working example of a filter

这是一个演示过滤器工作示例的小提琴

#2


Thanks for answering my question. It works for me. I've added the follow OnClick handler in the controller.

谢谢回答我的问题。这个对我有用。我在控制器中添加了跟随OnClick处理程序。

 onFilterClick: function(button) {

    var form = button.up('form').getForm();

    if (form.isValid()) {

        var fieldOne = Ext.getCmp("fieldOne").getValue();
        var fieldTwo = Ext.getCmp("fieldTwo").getValue();

        // check if there is some input
        if (!!fieldOne || !!fieldTwo) {

            // get store
            var store = Ext.data.StoreManager.lookup('store');

            // check if store not empty
            if(!Ext.isEmpty(store)){
                // clear filters and add a few new filters if params not empty
                store.clearFilter(true);

                if (!Ext.isEmpty(fieldOne)) {
                    store.filter("fieldOne",  fieldOne)
                }

                if (!Ext.isEmpty(fieldTwo)) {
                    store.filter("fieldTwo",  fieldTwo)
                }

                // count the filtered rows
               var count = store.snapshot ? store.snapshot.length : store.getCount();

                if (count == 0) {
                    alert("No data found!");
                    store.clearFilter();    
                }
            } else{
                //Store empty
                console.warn("Store's empty");
            }
        } else {
            // no values 
            alert("No entered data!");
        }
    } else {
        // form not valid
        alert("Form not valid!");
    }
}