PDA

View Full Version : Sum of Values in Xtemplate



Designworxz
8 Nov 2011, 7:31 PM
How to do total in Xtemplate as in Gridview.



tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div>',
'<tpl if="selected">',
'<div class="left">',
'${SalesPrice}',
'</div>',
'</tpl>',
'</div>',
//------------------> Want to display total of SalesPrice Here
'</tpl>'
);

AndreaCammarata
9 Nov 2011, 1:26 AM
XTemplates allows you ho handle your data just enclosing this kind of operations between {[ .. ]} brackets.
So, just to explain better what I mean, suppose your model has two fields (numberA and numberB).
To sum these values inside your template you should code it in something like:



var tpl = new Ext.XTemplate(
'<tpl for=".">',
'First Number: {numberA}',
'Second Number: {numberB}',
'SUM: {[values.numberA + values.numberB]}',
'</tpl>'
);


Hope this helps.

Designworxz
9 Nov 2011, 3:46 PM
Thanks for prompt reply Andrea, but i need total. My Store is like this




store = new Ext.data.Store({
model: "Store.models.mod",
data:[
{ Code : "001", SalePrice: "2300", selected: true},
{ Code : "002", SalePrice: "245", selected: false},
{ Code : "003", SalePrice: "2456", selected: true},
{ Code : "004", SalePrice: "1345", selected: true}
]
});


Xtemplate when rendered should display like this



Code: 001 SalePrice: 2300
-------------------------------------
Code: 003 SalePrice: 2456
-------------------------------------
Code: 004 SalePrice: 1345
-------------------------------------
Total: 6101

Designworxz
14 Nov 2011, 2:39 PM
Anybody ?

tbd.work
18 Jun 2012, 9:30 AM
I just had the same requirement, found this post and was upset to realize there was no answer on it!

You can get access to the array of data by passing down values. Ie.

tpl: new Ext.XTemplate(
'<tpl for=".">
{price}
</tpl>
{[this.getTotal(values)]}',
function getTotal(records) {
total = 0;

for(record in records)
total += record.data.price

return total;
}
)

This is in Sencha Touch 2