如何过滤ExtJs GridPanel/ExtJs Store?

3
我是一名新手,正在学习ExtJs。我有一个绑定了数据存储的GridPanel。我有一个checkboxgroup,其中包含GridPanel行的可能值。我想使用checkboxgroup值来过滤GridPanel。
以下是代码 -
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);
                        }
                    }
                }
            }             
        }            
]});

我哪里出了问题?

PS:我正在使用3.4版本

1个回答

10

getValue()方法有点棘手,返回的对象结构因结果集而异,这导致了您代码中的问题。不过getChecked()方法更加直接,我将在解决方案中使用它。 然后,由于在这种情况下更有用,我们使用filterBy。 这里是解决方案(注释内联):

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'));                                         
        });
    }
}

问题已解决。测试并工作正常 :)

更新 以上代码适用于 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;                                         
        });
    }
}

可选项 (额外改进,仅适用于ExtJs 4.x)
然而,检查您的应用程序,我认为可以进行以下改进:

  • 根据存储数据动态创建过滤复选框
  • 将“全部”复选框与其他复选框同步(即选择“全部”时,选择所有其他复选框)

这是包含改进的代码:

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'));                                         
                    });
                }                                
            }
        });
    });
}

这也经过测试并且正常工作:)。如果您需要,我可以传递一个包含运行代码的jsfiddle链接(只需告诉我)。

来自玻利维亚拉巴斯的问候


感谢您的努力。我正在使用3.4版本,似乎getChecked不受支持。我收到了Uncaught TypeError: Object [object Object] has no method 'getChecked'的错误提示。 - Sandy
我有一个更新。实际上,在“Leftpanel”的“items”中还有另一个“checkboxgroup”,用于列出可能的ID。我需要同时使用这两个“checkboxgroup”来过滤“Gridpanel”。 - Sandy
1
我为第一个代码片段添加了更新,现在它适用于3.4版本:)。请看一下。 - Edgar Villegas Alvarado
1
谢谢 :). 对于另一个复选框组,标准将是相同的(添加监听器)。但也许使用相同的复选框组会更容易,只需添加具有不同“name”属性或类似内容的项目即可。 - Edgar Villegas Alvarado
1
好的,这个想法是将所有的筛选条件都合并到filterBy方法中,这样你最终会得到类似于:return ... && record.get('date') < checkboxDate; - Edgar Villegas Alvarado
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接