简体   繁体   English

SAPUI5 可重用表片段数据绑定

[英]SAPUI5 Reusable Table Fragment Data Binding

I am starting to develop SAPUI5, trying to apply concepts and best practices from other web dev toolkits I know so far, please be kind as my knowledge is still fairly limited.我开始开发 SAPUI5,尝试应用我目前知道的其他 Web 开发工具包的概念和最佳实践,请善待我的知识仍然相当有限。

I want to re-structure a project and replace copy-paste code with reusable parts.我想重新构建一个项目并用可重复使用的部分替换复制粘贴代码。 Custom controls are not the right way as far as I checked, basically it is purely standard functionality of a SAPUI5 control with different data binding.据我检查,自定义控件不是正确的方法,基本上它是具有不同数据绑定的 SAPUI5 控件的纯粹标准功能。 The data binding and propagation should be done via XML as it fits the project architecture best, my initial idea was to use fragments.数据绑定和传播应该通过 XML 完成,因为它最适合项目架构,我最初的想法是使用片段。

Sample: An identical table should be used multiple times in the same view and in different views, single model with different object arrays.示例:应在同一视图和不同视图中多次使用相同的表,具有不同对象数组的单个模型。

Fragment:分段:

    <Table items="{???}">
        <columns>
            <Column >
                <Text text="Name"/>
            </Column>
            <Column>
                <Text text="Amount"/>
            </Column>
        </columns>
        <items>
            <ColumnListItem>
                <cells>
                    <ObjectIdentifier title="{dataModel>name}"/>
                    <Text text="{dataModel>amount}"/>
                </cells>
            </ColumnListItem>
        </items>
    </Table>
</core:FragmentDefinition>

Model:模型:

let oTemp = new JSONModel({
    data: {
        a: [{
            name: "Product 1 Entry 1",
            amount: "Product 1 Amount 1"
        }, {
            name: "Product 1 Entry 2",
            amount: "Product 1 Amount 2"
        }],
        b: [{
            name: "Product 2 Entry 1",
            amount: "Product 2 Amount 1"
        }, {
            name: "Product 2 Entry 2",
            amount: "Product 2 Amount 2"
        }]
    }
});
this.getView().setModel(oTemp, "dataModel");

XML View Usage: XML 视图用法:

<core:Fragment objectBindings="dataModel>/data/a" type="XML" fragmentName="...view.ReusableTable"></core:Fragment>
<core:Fragment objectBindings="dataModel>/data/b" type="XML" fragmentName="...view.ReusableTable"></core:Fragment>

I tried various ways to bind the item aggregation in the table or the fragment itself, without success.我尝试了各种方法来绑定表中的项目聚合或片段本身,但没有成功。 As hopefully shown in this sample, I want to pass a specific model property to the fragment and its table to display a different set of items (a or b) without the use of javascript in the view controller.如本示例所示,我想将特定模型属性传递给片段及其表以显示一组不同的项目(a 或 b),而无需在视图控制器中使用 javascript。

Desired Output:期望的输出:

2 Tables, identical header (Name, Amount column) with 2 rows each using data from datamodel, property a [] and b [] 2 个表,相同的标题(名称、金额列),每行 2 行使用来自数据模型、属性 a [] 和 b [] 的数据

From what I saw in the SAPUI5 library offering regarding code reuse, fragments should be the best way to achieve this result as no additional controller logic is required to display |从我在 SAPUI5 库中看到的关于代码重用的内容来看,片段应该是实现此结果的最佳方式,因为不需要显示额外的控制器逻辑 | interact.相互作用。 I would appreciate any suggestions how to approach this, or maybe change the approach in general if necessary?如果有必要,我会很感激任何建议如何解决这个问题,或者可能会改变一般的方法? Thank you lots!非常感谢!

for XML:对于 XML:

<core:Fragment fragmentName="fragmentlocation.fragmentName" type="XML"/>

controller binding:控制器绑定:

if (!this._DialogFragment) {
    Fragment.load({
    id: oView.getId(),
    name: "fragmentlocation.name",
    controller: this
  }).then((oDialog) => {
    this._DialogFragment = oDialog;
    oView.addDependent(this._DialogFragment );
    this._DialogFragment.open();
  });
} else {
  this._DialogFragment.open();
}

Turns out it wasn't that complicated after all.事实证明,事情并没有那么复杂。

https://plnkr.co/edit/ssWEKqICoY5il5o3 https://plnkr.co/edit/ssWEKqICoY5il5o3

Fragments (and basically every other control which is a ManagedObject ) have a property binding .片段(以及基本上是ManagedObject的所有其他控件)具有属性binding Set this to your list items:将此设置为您的列表项:

<core:Fragment binding="{dataModel>/data/a}" fragmentName="ui.Table" type="XML" />
<core:Fragment binding="{dataModel>/data/b}" fragmentName="ui.Table" type="XML" />

To reuse this binding inside your fragment just point relatively to the current context (which is basically no path at all)要在片段中重用此绑定,只需相对于当前上下文(基本上根本没有路径)

<Table items="{dataModel>}">

If your JSONModel had no name you would literally write items="{}" to get the same result.如果您的 JSONModel 没有名称,您将直接写items="{}"以获得相同的结果。

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

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