简体   繁体   English

如何按嵌套在json中的列对OpenUI5 Grid进行过滤/排序?

[英]How to filter/sort OpenUI5 Grid by column, which is nested in the json?

I'm now testing the capabilities of this grid and I'm having a look at this example at the moment. 我现在正在测试此网格的功能,目前正在看这个示例 Last week, I tried some basic loading of data, returned from the controller of the MVC app that I'm working on. 上周,我尝试了一些基本的数据加载,这些数据是从我正在处理的MVC应用程序的控制器返回的。 It returns json, which I then give to the grid to be displayed. 它返回json,然后将其提供给要显示的网格。 The data, that I want to show, is stored in multiple tables. 我要显示的数据存储在多个表中。 For now, I load data from only two of them for simplicity, because I'm only testing the capabilities of the grid - will it suit our needs. 现在,为了简化起见,我仅从其中的两个加载数据,因为我仅测试网格的功能-它将满足我们的需求。

The data, which arrives at the grid (in js), looks something like this: 到达网格(在js中)的数据看起来像这样:

{
    Cars: [
        {
            car_Number: '123',
            car_Color: 'red',
            car_Owner: Owner: {
                owner_ID: '234',
                owner_Name: 'John'
            },
            car_DateBought: '/Date(1450648800000)/'
        },
        {
            car_Number: '456',
            car_Color: 'yellow',
            car_Owner: Owner: {
                owner_ID: '345',
                owner_Name: 'Peter'
            },
            car_DateBought: '/Date(1450648800000)/'
        },
        {
            car_Number: '789',
            car_Color: 'green',
            car_Owner: Owner: {
                owner_ID: '567',
                owner_Name: 'Michael'
            },
            car_DateBought: '/Date(1450648800000)/'
        }
    ]
}

Here is some sample code of what I have done so far: 这是到目前为止我所做的一些示例代码:

$.ajax({
    type: 'post',
    url: BASE_HREF + 'OpenUI5/GetAllCars',
    success: function (result) {
        var dataForGrid = result['rows'];
        debugger;

        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData(dataForGrid);

        var oTable = new sap.ui.table.Table({
            selectionMode: sap.ui.table.SelectionMode.Multi,
            selectionBehavior: sap.ui.table.SelectionBehavior.Row,
            visibleRowCountMode: sap.ui.table.VisibleRowCountMode.Auto,
            minAutoRowCount: 10,
            //visibleRowCount: 10,
            showNoData: false
        });

        // define the Table columns and the binding values
        oTable.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({
                text: "ID of car"
            }),
            template: new sap.ui.commons.TextView({ text: "{car_Number}" }),
            sortProperty: "car_Number", // https://sapui5.netweaver.ondemand.com/sdk/test-resources/sap/ui/table/demokit/Table.html#__2
            filterProperty: "car_Number"
        }));

        oTable.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({ text: "Color of car" }),
            template: new sap.ui.commons.TextView({ text: "{car_Color}" }),
            sortProperty: "car_Color",
            filterProperty: "car_Color"
        }));

        oTable.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({ text: "Car Owner ID" }),
            template: new sap.ui.commons.TextView({
            // does not work like this -> text: "{Owner.owner_ID}"
                text: {
                    path: 'Owner',
                    formatter: function (owner) {
                        return owner !== null ? owner['owner_ID'] : '';
                    }
                }
            }),
            sortProperty: "Owner.owner_ID", // these two don't work
            filterProperty: "Owner.owner_ID"
        }));

        oTable.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({ text: "Car Owner Name" }),
            template: new sap.ui.commons.TextView({
            // does not work like this -> text: "{Owner.owner_Name}"
                text: {
                    path: 'Owner',
                    formatter: function (owner) {
                        return owner !== null ? owner['Name'] : '';
                    }
                }
            }),
            sortProperty: "Owner.owner_Name", // these two don't work
            filterProperty: "Owner.owner_Name"
        }));

        var dateType = new sap.ui.model.type.Date({ // http://stackoverflow.com/questions/22765286/how-to-use-a-table-column-filter-with-formatted-columns
            pattern: "dd-MM-yyyy"
        });
        oTable.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({ text: "Date bought" }),
            template: new sap.ui.commons.TextView({
                text: {
                    path: 'car_DateBought',
                    formatter: dateFormatterBG
                }
            }),
            sortProperty: "car_DateBought",
            filterProperty: "car_DateBought",
            filterType: dateType
        }));

        oTable.setModel(oModel);
        oTable.bindRows("/");
        oTable.placeAt("testTable", "only");
    },
    error: function (xhr, status, errorThrown) {
        console.log("XHR:");
        console.log(xhr);
        console.log("Status:");
        console.log(status);
        console.log("ErrorThrown:");
        console.log(errorThrown);
    }
});

My problems: 我的问题:

  1. I cannot sort or filter the list of cars by owner_ID or owner_Name. 我无法按owner_ID或owner_Name排序或过滤汽车列表。 How should I do the filtering and sorting? 我应该如何进行过滤和排序? Should it be done with the help of a formatter function in some way, or...? 是否应该通过格式化程序功能以某种方式完成,或者...?

  2. I can sort by car_DateBought, but I cannot filter the cars by this field. 我可以按car_DateBought进行排序,但是无法按此字段过滤汽车。 First, I tried setting filterType: dateType, then I tried setting it to filterType: dateFormatterBG(it turns out, that dateType does exactly the same thing as my own dateFormatterBG does, btw). 首先,我尝试设置filterType:dateType,然后尝试将其设置为filterType:dateFormatterBG(事实证明,dateType与我自己的dateFormatterBG所做的事情完全一样,顺便说一句)。

     function dateFormatterBG(cellvalue, options, rowObject) { var formatedDate = ''; if ((cellvalue != undefined)) { var date = new Date(parseInt(cellvalue.substr(6))); var month = '' + (date.getMonth() + 1); var day = '' + date.getDate(); var year = date.getFullYear(); if (month.length < 2) { month = '0' + month; } if (day.length < 2) { day = '0' + day; } formatedDate = [day, month, year].join('-'); } return formatedDate; } 

Anyway, as I said, I tried both, but it doesn't work. 无论如何,就像我说的,我都尝试了两次,但是都行不通。 When I click on the header of a column like in the example in the first link, I don't get any sort of a datepicker. 当我单击第一个链接中的示例中的列标题时,没有任何日期选择器。 How can I tell OpenUI5, that this column needs to be filtered by date and it should provide the user with a datepicker, when he/she clicks on the 'Filter' input field at the bottom of the dropdown menu? 我如何告诉OpenUI5,当他/她单击下拉菜单底部的“过滤器”输入字段时,该列需要按日期过滤,并且应该为用户提供日期选择器? When I try to write the date in the filter field like '07-11-2016' (the way it is formatted), I get an empty table/grid. 当我尝试在“ 07-11-2016”(格式设置)之类的过滤器字段中写入日期时,我得到了一个空表/网格。 If I try to enter the huge number from field car_DateBought in the json object, all available rows in the table stay the same and when I reclick on the header, the filter field at the bottom of the dropdown menu appears with error-state. 如果我尝试在json对象中从字段car_DateBought输入巨大的数字,则表中所有可用的行均保持不变,并且当我重新单击标题时,下拉菜单底部的过滤器字段会显示错误状态。

Thank you in advance for your help and pieces of advice! 预先感谢您的帮助和建议!

Edit: This is just sample, dummy data. 编辑:这只是样本虚拟数据。 I try to load the real data and I see, that in the table I've got a couple of rows with date, which is today (07-11-2016, or 11/7/2016 if you prefer). 我尝试加载实际数据,然后看到表中有几行带有日期的日期,即今天(2016年7月11日,或者您愿意的话,2016年11月7日)。 That's why getting an empty table after trying to filter means it's not working correctly. 这就是为什么在尝试过滤后得到一个空表意味着它不能正常工作的原因。

Sorting: in a sample I am looking at the following appears in the controller: 排序:在一个示例中,我正在查看以下内容出现在控制器中:

    onInit : function () {
        this._IDSorter = new sap.ui.model.Sorter("my_id", false);
    },
    ....

Then in the view there is a button defined in the header column as 然后,在视图中,标题列中定义了一个按钮,如下所示:

        <Column>
            <Label text="Project"/>
            <Button icon="sap-icon://sort" press="onSortID" />
        </Column>

And back in the controller there is a further function: 在控制器中,还有另一个功能:

    onSortID:  function(){
      this._IDSorter.bDescending = !this._IDSorter.bDescending;
      this.byId("table").getBinding("items").sort(this._IDSorter);
    },

I read this collectively as defining a sorter in the onInit(), then toggle/reversing it in the click event of the sort button in the column header via the onSortId() function. 我在将其定义为onInit()时集体阅读,然后通过onSortId()函数在列标题中排序按钮的click事件中切换/反转它。 The OpenUI5 API doc re sorters indicates there are more parameters in the sorter constructor for initial sort direction and sorting function. OpenUI5 API文档重新排序器指示排序器构造函数中还有更多参数可用于初始排序方向和排序功能。

Following this pattern, for your needs to sort on the owner_ID or owner_Name, I assume you could set up a sorter as 按照这种模式,为了满足您对owner_ID或owner_Name进行排序的需要,我假设您可以将排序器设置为

this._OwnerIDSorter = new sap.ui.model.Sorter("car_Owner/owner_ID", false);
this._OwnerNameSorter = new sap.ui.model.Sorter("car_Owner/owner_Name", false);

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

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