使用扩展实体的属性过滤控制器

时间:2016-06-24 10:12:58

标签: odata sapui5

在主 - 详细视图中,我有一个工作中心列表作为主要部分,计划日历作为详细部分。

<mvc:View controllerName="sap.ui.demo.wt.controller.Overview"
  xmlns="sap.m"
  xmlns:mvc="sap.ui.core.mvc"
  xmlns:semantic="sap.m.semantic"
  xmlns:unified="sap.ui.unified"
  xmlns:core="sap.ui.core" displayBlock="true"
>
  <Page title="{i18n>overviewPageTitle}">
    <SplitContainer>
      <masterPages>
        <semantic:MasterPage title="{i18n>overviewMasterTitle}">
          <List items="{data>/WorkCenterSet}">
            <ObjectListItem
              title="{data>WorkCntr}"
              type="Active"
              press="onWorkCtrPressed"
            />
          </List>
        </semantic:MasterPage>
      </masterPages>
      <detailPages>
        <semantic:DetailPage title="{i18n>overviewDetailTitle}">
          <VBox>
            <PlanningCalendar id="PC"
              startDate="{/StartDate}"
              rows="{
                path : 'data>/EmployeeSet',
                parameters : {
                  expand : 'OrderOperation, EmployeeWorkCenter'
                }
              }"
            >
              <rows>
                <PlanningCalendarRow
                  title="{data>UserFullname}"
                  appointments="{
                    templateShareable : true,
                    path : 'data>OrderOperation'
                  }"
                >
                  <appointments>
                    <unified:CalendarAppointment
                      startDate="{data>EarlSchedStartdate}"
                      endDate="{data>EarlSchedFindate}"
                      title="{data>Description}"
                    />
                  </appointments>
                </PlanningCalendarRow>
              </rows>
            </PlanningCalendar>
          </VBox>
        </semantic:DetailPage>
      </detailPages>
    </SplitContainer>
  </Page>
</mvc:View>

我想根据员工所属的工作中心过滤计划日历。

这是控制器:

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/ui/model/json/JSONModel",
  "sap/ui/model/odata/v2/ODataModel",
  "sap/ui/model/Filter",
  "sap/ui/model/FilterOperator"
], function(Controller,JSONModel,ODataModel,Filter,FilterOperator) {
  "use strict";
  return Controller.extend("sap.ui.demo.wt.controller.Overview", {
    onInit: function(){
      this.getView().setModel(new JSONModel({
        "StartDate" : new Date("2016-06-12T08:00:00Z")
      }));
    },

    onWorkCtrPressed: function(oEvent) {
      var oContext = oEvent.getSource().getBindingContext("data");
      var sWorkCntr = oContext.getPath().split("'")[1]; //get the work center's name

      //What I tried first
      //var aFilter = [];
      //aFilter.push(new Filter("WorkCntr",FilterOperator.StartsWith,sWorkCntr))
      //this.getView().byId("PC").getBinding("rows").filter(aFilter);
      sap.m.MessageToast.show("Selected Work Center : " + sWorkCntr);
    }
  });
});

我首先尝试的操作不起作用,因为查询在实体集"WorkCntr"中查找属性"EmployeeSet"。但是此属性仅存在于我在视图中展开的实体集"EmployeeWorkCenterSet"中。问题是:我甚至不知道我需要使用的查询......你能帮忙吗?

PS:我的OData服务是版本2.查询类似

EmployeeSet?$expand=EmployeeWorkCenter($filter={any filter})

不起作用。

1 个答案:

答案 0 :(得分:0)

如上所述in this answer,您可以将导航属性名称添加到Filter路径,如下所示:

new Filter({
  path: "EmployeeWorkCenter/WorkCntr", // "EmployeeWorkCenter" === navigation property from Employee
  operator: FilterOperator.StartsWith,
  value1: sWorkCntr
});

(我假设解析导航属性EmployeeWorkCenter会返回一个对象而不是另一个集合。否则,这将无法在V2中工作)