【问题标题】:Sencha Filter Grid column煎茶过滤器网格列
【发布时间】:2023-03-17 23:19:01
【问题描述】:

我正在尝试找出如何过滤列,以便找到特定的楼层值(或例如所有值为“2”的楼层)并通过单击特定按钮按升序/降序对它们进行排序。 我写了这段代码,但它不起作用,因为当我在过滤器中写一个值时,他没有给我任何行(如果我尝试使用“type:'string'”):

{
   text: "Floor",
   dataIndex: 'attributes',
   filter:{
       type: 'number',
       value : 'floor'
   },
   renderer: function (value) {
      return value['floor'];
   },

}

我应该如何更改我的代码以使其正常工作?

【问题讨论】:

    标签: extjs filter filtering


    【解决方案1】:

    您需要将dataIndex 更改为非嵌套列。

    您可以在模型中添加逻辑字段,例如:

     {
        name: "floor", "mapping": "attributes.floor"
    }
    

    在列中:

    {
       text: "Floor",
       dataIndex: 'floor',
       filter:{
           type: 'number'
       }
    }
    

    已编辑 - 查看小提琴:https://fiddle.sencha.com/#view/editor&fiddle/30gr

    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
            Ext.Msg.alert('Fiddle', 'Welcome to Sencha Fiddle!');
    
            var store = Ext.create("Ext.data.Store", {
                fields: [{
                    "name": "name",
                    "type": "string"
                }, {
                    "name": "floor",
                    "mapping": function (data) {
                        console.log(data);
                        if (data && data.attributes && Ext.isNumber(data.attributes.floor)) {
                            return data.attributes.floor;
                        }
                        return null
                    }
                }],
                data: [{
                    "name": "A1",
                    "attributes": {
                        "floor": 1
                    }
                }, {
                    "name": "B1",
                    "attributes": {
                        "floor": 1
                    }
                }, {
                    "name": "A2",
                    "attributes": {
                        "floor": 2
                    }
                }, {
                    "name": "A3",
                    "attributes": {
                        "floor": 3
                    }
                }, {
                    "name": "ANU",
                    "attributes": {
                        "floor": null
                    }
                }, {
                    "name": "AN"
                }]
            });
    
            Ext.create("Ext.grid.Panel", {
                renderTo: Ext.getBody(),
                width: 400,
                height: 500,
                store: store,
                columns: [{
                    "dataIndex": "name",
                    "text": "Name"
                }, {
                    "dataIndex": "floor",
                    "text": "Floor"
                }]
            })
        }
    });
    

    【讨论】:

    【解决方案2】:

    我建议在数据模型中计算 floor 的值,而不是使用列渲染器。这样您就可以对该值进行过滤和排序。

    您需要在模型上添加计算字段

    fields: [{
        name: 'attributes'
    }, {
        name: 'floor',
        calculate: function(data) {
            return data.attributes && Number(data.attributes.floor);
        }
    }]
    

    那么你的专栏就变成了

    {
        text: "Floor",
        dataIndex: 'floor',
        filter:{
            type: 'number',
            value : 'floor'
        }
    }
    

    【讨论】:

    • 您的字段示例不起作用。 Field floor depends on undefined field attributes
    • 你错了@norbeq。我的代码工作正常,也可以排序:fiddle.sencha.com/#view/editor&fiddle/30h3
    • 在我的情况下,解决方案在中间:为了使其可正确排序,我必须像这样编辑我的代码:name: "floor", calculate: function (data) { if(Number(data.attributes.floor)){ return Number(data.attributes.floor) }else{ return null; } }
    • 看看在没有通过地板的情况下做了什么:fiddle.sencha.com/#view/editor&fiddle/30h8 呈现为 0 - 所以很糟糕。
    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2012-06-22
    • 2011-06-11
    • 2023-01-13
    • 2011-12-25
    相关资源
    最近更新 更多