如何在Text中显示多个行号的汇总值 [SAPUI5]

How to display the summarized value of multiple rows numbers in a Text [SAPUI5]

如标题所述,假设我有以下型号:

model = {
         0:{"count":3},
         1:{"count":4}
        };

问题 1:如何在文本中将计数显示为汇总数字,即 7?

<Text text="{model>count}" />

问题二:有技术上的问题,可以这样总结吗?

我尝试了什么: Formatter 函数。

<Text text="{ path: 'model>count', formatter:'.formmater.sumCount'}" />

问题: Formatter,函数sumCount,确实得到每一行的值,即3、4等...,这意味着它不具有遍历整个 model 并将所有计数器和 return 汇总值添加到 <Text>

的总体能力

Question 2: Is there a technicality, which could allow such summarization ?

您可以通过格式化程序实现。

假设您在控制器中定义了以下sap.ui.model.json.JSONModel

    var oMyModel = new sap.ui.model.json.JSONModel({
                0: { "count": 3 },
                1: { "count": 4 }
            });
            this.getView().setModel(oMyModel, "myModel");

并且您有以下 formatter.js 文件:

sap.ui.define([], function () {
    "use strict";
    return {
        sumTwoEntries: function (iValue1, iValue2) {
            return iValue1 + iValue2;
        },
        sumAllEntries: function (oObject) {
            var iSum = 0;
            var aObjectKeys = Object.keys(oObject);
            for (var i = 0; i < aObjectKeys.length; i++) {
                iSum += oObject[i].count;
            }
            return iSum;
        }
    };
});

这会起作用:

                            <!-- Displays the first Entrie -->
                            <Text text="{myModel>/0/count}"/>
                            <!-- Displays the second Entrie -->
                            <Text text="{myModel>/1/count}"/>
                            <!-- Displays the summarized Value of both Entries -->
                            <Text text="{ parts: [ { path: 'myModel>/0/count'}, { path: 'myModel>/1/count'}], formatter: '.formatter.sumTwoEntries'}"/>
                            <!-- Displays the summarized Value of x Entries -->
                            <Text text="{ path: 'myModel>/', formatter: '.formatter.sumAllEntries'}"/>