as the title states, let’s say i do have the following model:
JavaScript
5
1
model = {
2
0:{"count":3},
3
1:{"count":4}
4
};
5
Question 1: How can i display count as summarized number i.e. 7 in a Text?
JavaScript
2
1
<Text text="{model>count}" />
2
Question 2: Is there a technicality, which could allow such summarization ?
What i tried:
Formatter
function.
JavaScript
2
1
<Text text="{ path: 'model>count', formatter:'.formmater.sumCount'}" />
2
Issue: Formatter
, function sumCount
, does get the value of each row, i.e. 3, 4 etc…, which means it doesn’t have an overarching capability to loop through the whole model
and add all the counters and return
the summarized value to the <Text>
Advertisement
Answer
Question 2: Is there a technicality, which could allow such summarization ?
You can achieve that via an formatter.
Suppose you have defined the following sap.ui.model.json.JSONModel
in the controller:
JavaScript
6
1
var oMyModel = new sap.ui.model.json.JSONModel({
2
0: { "count": 3 },
3
1: { "count": 4 }
4
});
5
this.getView().setModel(oMyModel, "myModel");
6
and you have the following formatter.js
file:
JavaScript
17
1
sap.ui.define([], function () {
2
"use strict";
3
return {
4
sumTwoEntries: function (iValue1, iValue2) {
5
return iValue1 + iValue2;
6
},
7
sumAllEntries: function (oObject) {
8
var iSum = 0;
9
var aObjectKeys = Object.keys(oObject);
10
for (var i = 0; i < aObjectKeys.length; i++) {
11
iSum += oObject[i].count;
12
}
13
return iSum;
14
}
15
};
16
});
17
this will work:
JavaScript
9
1
<!-- Displays the first Entrie -->
2
<Text text="{myModel>/0/count}"/>
3
<!-- Displays the second Entrie -->
4
<Text text="{myModel>/1/count}"/>
5
<!-- Displays the summarized Value of both Entries -->
6
<Text text="{ parts: [ { path: 'myModel>/0/count'}, { path: 'myModel>/1/count'}], formatter: '.formatter.sumTwoEntries'}"/>
7
<!-- Displays the summarized Value of x Entries -->
8
<Text text="{ path: 'myModel>/', formatter: '.formatter.sumAllEntries'}"/>
9