OData 树绑定与片段中的过滤器 [英] OData Tree binding with filters in a fragment

查看:67
本文介绍了OData 树绑定与片段中的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SAPUI5 应用程序启动时使用 oModel.read() 从 OData V2 服务读取数据.数据从后端正确加载到 ODataModel 中.后来我想在片段中使用我的一个实体(值帮助).

I read the data from an OData V2 service at the startup of my SAPUI5 application with oModel.read(). The data are correctly loaded from the backend into the ODataModel. Later I would like to use one of my entities in a fragment (value help).

作为显示片段中数据的控件是一个sap.m.Tree控件.我不能使用本地 JSONModel 将数据绑定到片段,但必须坚持使用 ODataModel.

As the control to display the data in the fragment is a sap.m.Tree control. I can not use a local JSONModel to bind the data to the fragment but have to stick to the ODataModel.

如何进行绑定以使数据显示在我的片段中?

How has the binding to be done to get data displayed in my fragment?

这是oModel.read:

oModel.read("/CategoriesSet", {
  filters: aFilters,
  success: function(oResult) {
    // ...
  },
});

在值帮助中,片段被称为:

In the Value Help, the fragment is called:

onCatValueHelp: function(oEvent){
  if (!this._oDialog) {
    this._oDialog = sap.ui.xmlfragment("<XXXXX>.view.Categories", this);
    this.getView().addDependent(this._oDialog);
  }
  this._oDialog.open();
},

这是片段:

<Tree id="CatTree" mode="MultiSelect"
  items="{
    path: '?????',
    parameters: {
      countMode: 'Inline',
      operationMode: 'Client',
      numberOfExpandedLevels: 0
    }
  }">
  <StandardTreeItem title="{CatName}" tooltip="{CatID}" />
</Tree>

如果我使用 '/CategoriesSet' 作为路径,那么我将数据加载到片段中,但数据会再次从后端获取,但没有过滤器.由于后端调用的性能不是很好,我宁愿使用之前 oModel.read() 中模型中已经存在的数据.

If I use '/CategoriesSet' for the path, then I get the data loaded into the fragment, but the data is then fetched from the backend again, but without the filter. And as the backend call is not very performant, I would rather use the data that is already existing in the model from the previous oModel.read().

推荐答案

这个答案所示,你可以初步在 XML 中定义静态过滤器.

As shown in this answer, you can preliminarily define static filters in XML.

但是,如果需要动态确定过滤器值,则必须在 JS 中创建过滤器实例并将它们传递给树绑定.您可以将绑定定义保留在 XML 中,但聚合绑定应先suspend: true(以避免不必要地发送多个请求).

If the filter values, however, need to be dynamically determined, you'll have to create the filter instances in JS and pass them to the tree binding. You can keep the binding definition in XML but the aggregation binding should be suspended: true first (in order to avoid sending multiple requests unnecessarily).

<Tree id="CatTree" items="{
  path: '/CategoriesSet',
  ...,
  suspended: true
}">
  <StandardTreeItem title="{CatName}" tooltip="{CatID}" />
</Tree>

// As soon as the Tree is accessible from JS:
const suspendedTreeBinding = myTree.getBinding("items");
suspendedTreeBinding.filter(aFilters, "Application");
suspendedTreeBinding.resume(); // starts sending a single data request but with the filter

无需事先调用oModel.read().

离题但尽量避免使用旧的工厂函数sap.ui.xmlfragment,因为它自 UI5 v1.58 起已被弃用!使用 sap/ui/core/Fragment.load 代替.例如.像这样.

Off-topic but try to avoid using the old factory function sap.ui.xmlfragment if possible as it's deprecated since UI5 v1.58! Use sap/ui/core/Fragment.load instead. E.g. like this.

这篇关于OData 树绑定与片段中的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆