SAPUI5 - 列出项目,查看模型排序问题

时间:2018-03-29 10:03:03

标签: odata sapui5

我正在进行多个OData实体集读取并将结果推送到视图模型中。

视图模型从 QuestionSet 获取所有结果,然后在成功时从 CategorySet 获取其他几个属性。

我想在 EditSurvey 视图中执行的操作是从我的视图模型(view>/Questions)加载所有数据,并按CategoryDesc对结果进行排序(从的 CategorySet )。

看起来 EditSurvey 视图在将 CategorySet 的结果推入视图模型之前尝试加载模型。

有谁知道我怎么排序?

EditSurvey.view.xml

<List
  noDataText="{i18n>noQuestions}"
  items="{
    path: 'view>/Questions',
    sorter: {
      path: 'CategoryDesc',
      descending: false,
      group: true
    }
  }"
  mode="SingleSelectMaster"
  selectionChange="onQuestionSelect"
>

EditSurvey.controller.js

onInit: function() {
  var oRouter = this.getOwnerComponent().getRouter();
  oRouter.getRoute("EditSurvey").attachMatched(this._onRouteMatched, this);
  // Intialise view model
  var oModel = new JSONModel({
    SurveyName: "",
    SurveyDesc: "",
    ReportingFrom: "",
    Questions: [{ }]
  });
  // Set view model
  this.getView().setModel(oModel, "view");
},

_onRouteMatched: function(oEvent) {
  // Get URL parameters passed from Main Page.
  var oArgs = oEvent.getParameter("arguments");
  // Get view model
  var self = this;
  var view = this.getView();
  var oModel = view.getModel();
  var viewModel = view.getModel("view");
  var viewData = viewModel.getData();
  // Read QuestionSet, populate into view model
  oModel.read("/QuestionSet", {
    filters: [
      new Filter("SurveyId", FilterOperator.EQ, oArgs.SurveyId)
    ],
    success: function(oData) {
      viewData.Questions = oData.results;
      viewModel.setData(viewData);
      // Read CategorySet, get CategoryId & CategoryDesc, populate into view model
      return new Promise(function() {
        _.each(viewData.Questions, function(result, index) {
          var surveyModel = self.getView().getModel();
          surveyModel.read("/CategorySet", {
            filters: [
              new Filter("CategoryId", FilterOperator.EQ, result.CategoryId)
            ],
            success: function(oData) {
              viewData.Questions[index].CategoryId = oData.results[0].CategoryId;
              viewData.Questions[index].CategoryDesc = oData.results[0].CategoryDesc;
              viewModel.setData(viewData);
            },
            error: function(oError) {}
          });
        });
      });
    },
    error: function(oError) {}
  });
  // Read SurveySet, populate into view model
  oModel.read("/SurveySet", {
    filters: [
      new Filter("SurveyId", FilterOperator.EQ, oArgs.SurveyId)
    ],
    success: function(oData) {
      viewData.SurveyName = oData.results[0].SurveyName;
      viewData.SurveyDesc = oData.results[0].SurveyDesc;
      viewData.ReportingFrom = oData.results[0].ReportingFrom;
      viewModel.setData(viewData);
    },
    error: function(oError) {}
  });
},

图片显示它获得CategoryDesc但未按CategoryDesc排序:

Image shows it gets CategoryDesc, but not sorted by CategoryDesc

图片显示按照问题集中的属性排序,而不是CategorySet

Image shows it sorted by a property from QuestionSet, not CategorySet

1 个答案:

答案 0 :(得分:1)

您可能真正需要的是服务元数据中QuestionSetCategorySet的关联和导航属性:

<EntityType Name="Question">
  <key>
    <PropertyRef Name="QuestionID"/>
  </key>
  <Property Name="QuestionID" ... />
  <Property Name="CategoryID" ... /> <!-- foreign key -->
  ...
  <NavigationProperty Name="ToCategory" ... />
</EntityType>
<EntityType Name="Category">
  <key>
    <PropertyRef Name="CategoryID"/>
  </key>
  <Property Name="CategoryID" ... />
  <Property Name="CategoryDesc" ... />
  ...
  <NavigationProperty Name="ToQuestions" ... />
</EntityType>

<Association Name="...">
  <End Role="Category" Multiplicity="1" ... /> <!-- a category contains many questions -->
  <End Role="Question" Multiplicity="*" ... /> <!-- a question belongs to one category -->
  <ReferentialConstraint>
    <Principal Role="Category">
      <PropertyRef Name="CategoryID"/>
    </Principal>
    <Dependent Role="Question">
      <PropertyRef Name="CategoryID"/> <!-- as a foreign key -->
    </Dependent>
  </ReferentialConstraint>
</Association>

然后,您可以通过导航属性CategoryDesc QuestionSet CategorySet轻松按<List items="{ path: '/QuestionSet', parameters: { expand: 'ToCategory' }, sorter: { path: 'ToCategory/CategoryDesc', descending: false, group: true } }" mode="SingleSelectMaster" selectionChange=".onQuestionSelect" > 排序。

show

以下是一个工作示例:expand

  • 它使用https://embed.plnkr.co/wAlrHB/ Product n ⎯⎯⎯ 1 Category 。< / LI>
  • 声明性地说出需要什么以及如何分类
  • 不需要一行JS代码。