# MVVM: how to get a sum of all items into the footer

czynga
171

Hello,

I have a simple listbox (or grid) containing items with description and price. How can I get a sum (total) of all items' price in the footer?

<listbox model="@load(vm.tableModel)">

<template name="model" var="tableItem">
<listitem>
<listcell>
</listcell>
<listcell>
</listcell>
</listitem>
<template>

<listfoot>
<listfooter align="right" label="Total:"></listfooter>
<listfooter>
<label>??? what goes here ???</label>
</listfooter>
</listfoot>
</listbox>


delete retag edit

## 5 Replies

Steva77
1014 3
http://www.research.softe...

If I get your question correctly: "How do I make MVVM calculate a sum automatically?", well, ...
I do think the best way is that you have the sum already calculated in your model and binded to UI... uh?!
Something like:

<label value="@load(vm.tableModel.sum)"></label>

czynga
171

I don't know Steva, I have a funny feeling that it's going to be a nightmare to get all the @NotifyChange done properly in case of editable fields (for example doublebox instead of label). And tableModel is just a plain ListModelList so I guess it would be better to move "sum" filed directly into vm?

czynga
171

I was thinking more about something like

<label value="@load(vm.tableModel[0].price + vm.tableModel[1].price + vm.tableModel[2].price)"></label>


but it's not only ugly but also doesn't work.

Steva77
1014 3
http://www.research.softe...

I agree, ugly and not working :)
Move your logic to vm, and try to manage notification properly - a bit annoying but powerful.

jumperchen
3909 2 8
http://jumperchen.blogspo...

Or use a Custom Converter instead.

[hide preview]