简体   繁体   English

如何过滤ExtJs GridPanel / ExtJs商店?

[英]How to filter ExtJs GridPanel/ExtJs Store?

I'm new to ExtJs. 我是ExtJs的新手。 I have a GridPanel which is binded with a data store . 我有一个与data store绑定的GridPanel I have a checkboxgroup , which containts the possible values of the GridPanel row. 我有一个checkboxgroup ,它包含GridPanel行的可能值。 I want to filter the GridPanel with the checkboxgroup values. 我想用checkboxgroup值过滤GridPanel

Here is the code - 这是代码 -

Store1 = new Ext.data.JsonStore({
url: 'CustomerProfiles/GetDetails',
root: 'rows',
fields:['Name','Id']
});

DetailedResults =
                {
                    xtype: 'grid',
                    autoHeight: true,
                    autoWidth: true,
                    autoScroll: true,
                    border: false,
                    trackMouseOver: false,
                    frame: true,
                    store: Store1,
                    columns: [
                        { header: 'Name', dataIndex: 'Name', width: 90 },
                        { header: 'Id', dataIndex: 'Id', width: 50 }
                    ]
                };

Leftpanel = new Ext.Panel({
id: 'Leftpanel',
frame: true,
width: 175,
items: [
        {
            xtype: 'label'
        },
        {
            xtype: 'checkboxgroup',
            columns: 1,
            vertical: true,
            items: [{
                boxLabel: 'ALL',
                name: 'chkName',
                inputValue: 'all'
            }, {
                boxLabel: 'N1',
                name: 'chkName',
                inputValue: 'N1'
            }, {
                boxLabel: 'N2',
                name: 'chkName',
                inputValue: 'N2'
            }, {
                boxLabel: 'N3',
                name: 'chkName',
                inputValue: 'N3'
            }], listeners: {
                change: {
                    fn: function () {
                        Store1.clearFilter();
                        var selectedValue = this.getValue();
                        for (var i = 0; i < selectedValue.length; i++) {
                            Store1.filter('Name', selectedValue[i].inputValue);
                        }
                    }
                }
            }             
        }            
]});

Where I went wrong? 哪里出错了?

PS: I am using 3.4 version PS:我使用的是3.4版本

The getValue() method is a little tricky, the object it returns has variable structure depending on the resultset, that caused the problem in your code. getValue()方法有点棘手,它返回的对象具有可变结构,具体取决于结果集,这会导致代码中出现问题。 However, the getChecked() method is more straightforward, I'll use it in the solution. 但是, getChecked()方法更简单,我将在解决方案中使用它。 Then, we use filterBy as it's more useful in this case. 然后,我们使用filterBy,因为它在这种情况下更有用。
Here you have the solution (comments inline): 在这里你有解决方案(评论内联):

change: {
    fn: function () {
        var checkedBoxes = this.getChecked(), //Array of checked checkboxes
            selectedValues = []; //Array of selected values                                       
        for (var i = 0; i < checkedBoxes.length; i++) {
            selectedValues.push(checkedBoxes[i].inputValue); //Add each inputValue to the array                                       
        }                                    
        var allSelected = Ext.Array.contains(selectedValues, 'all'); //Whether the 'ALL' option was selected
        Store1.filterBy(function(record){
           //If all was selected or if the name is included in the selectedValues, include the item in the filter
           return allSelected || Ext.Array.contains(selectedValues, record.get('Name'));                                         
        });
    }
}

Problem solved. 问题解决了。 Tested and working :) 经过测试和工作:)

UPDATE The above code works on ExtJs >= 4. For Ext 3.4, this is the code: 更新上面的代码适用于ExtJs> = 4. 对于Ext 3.4,这是代码:

change: {
    fn: function () {
        var selectedValues = []; //Array of selected values 
        this.items.each(function(checkbox){
            if(checkbox.checked)
                selectedValues.push(checkbox.inputValue);
        });                                    
        var allSelected = selectedValues.indexOf('all') >= 0; //Whether the 'ALL' option was selected           
        Store1.filterBy(function(record){
           //If all was selected or if the name is included in the selectedValues, include the item in the filter
           return allSelected || selectedValues.indexOf(record.get('Name')) >= 0;                                         
        });
    }
}

OPTIONAL (extra improvements, works only on ExtJs 4.x) 可选(额外改进,仅适用于ExtJs 4.x)
However, checking your app, I think the following improvements could be done: 但是,检查您的应用程序,我认为可以进行以下改进:

  • Create the filter checkboxes dynamically depending on the store data 根据商店数据动态创建过滤器复选框
  • Sync the ALL checkbox with the rest (ie when selecting ALL, select all the other checkboxes) 将ALL复选框与其余复选框同步(即选择ALL时,选中所有其他复选框)

This is the code with the improvements: 这是具有改进的代码:

var Store1 = new Ext.data.JsonStore({
    proxy: {
        type: 'ajax',                
        url: 'CustomerProfiles/GetDetails',
        reader: {                    
            root: 'rows'                    
        }
    },            
    autoLoad: true,                        
    fields: ['Name','Id'],
    listeners: {
            //Each time the store is loaded, we create the checkboxes dynamically, and add the checking logic in each one
        load: function(store, records){
            createCheckboxesFromStore(store);                       
        }
    }
});

var DetailedResults = {
    xtype: 'grid',
    autoHeight: true,
    autoWidth: true,
    autoScroll: true,
    border: false,
    trackMouseOver: false,
    frame: true,
    store: Store1,
    columns: [
        { header: 'Name', dataIndex: 'Name', width: 90 },
        { header: 'Id', dataIndex: 'Id', width: 50 }
    ]
};

var Leftpanel = new Ext.Panel({
    id: 'Leftpanel',
    frame: true,
    width: 175,
    items: [
        {
            xtype: 'label'
        },
        {
            xtype: 'checkboxgroup',
            columns: 1,
            vertical: true,

        }            
]});

function createCheckboxesFromStore(store){
    var checkBoxGroup = Leftpanel.down('checkboxgroup');
    checkBoxGroup.removeAll();
    checkBoxGroup.add({
        itemId: 'allCheckbox',
        boxLabel: 'ALL',
        name: 'chkName',
        inputValue: 'all',
        checked: true,
        listeners: {
             change: function (chbx, newValue) {                                        
                 console.log("Changed ALL to ", newValue);
                 if(newValue){  //If ALL is selected, select every checkbox                                   
                     var allCheckboxes = this.up('checkboxgroup').query("checkbox"); //Array of all checkboxes
                     for (var i = 0; i < allCheckboxes.length; i++) {
                         allCheckboxes[i].setValue(true);                                         
                     }
                 }

             }   
        }
    });

    //Create one checkbox per store item
    store.each(function(record){
        checkBoxGroup.add({
            boxLabel: record.get('Id'),
            name: 'chkName',
            inputValue: record.get('Name'),
            checked: true,
            listeners: {
                change: function (chbx, newValue) {
                    console.log("Changed ", chbx.inputValue, " to ", newValue);
                    var checkboxGroup = this.up('checkboxgroup'),
                        checkedBoxes = checkboxGroup.getChecked(), //Array of checked checkboxes
                        selectedValues = []; //Array of selected values                                       

                    //If we uncheck one, also uncheck the ALL checkbox
                    if(!newValue) checkboxGroup.down("#allCheckbox").setValue(false);

                    for (var i = 0; i < checkedBoxes.length; i++) {
                        selectedValues.push(checkedBoxes[i].inputValue); //Add each inputValue to the array                                       
                    }                                                                        
                    Store1.filterBy(function(record){
                       //If all was selected or if the name is included in the selectedValues, include the item in the filter
                       return Ext.Array.contains(selectedValues, record.get('Name'));                                         
                    });
                }                                
            }
        });
    });
}

This is also tested and working :). 这也是测试和工作:)。 If you need it, I can pass you a jsfiddle link with the code running (just tell me). 如果你需要它,我可以通过jsfiddle链接运行代码(告诉我)。

Cheers, from La Paz, Bolivia 干杯,来自玻利维亚拉巴斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM