-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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)"> <listhead> <listheader label="Item"></listheader> <listheader label="Price"></listheader> </listhead> <template name="model" var="tableItem"> <listitem> <listcell> <label value="@load(tableItem.description)"></label> </listcell> <listcell> <label value="@load(tableItem.price)"></label> </listcell> </listitem> <template> <listfoot> <listfooter align="right" label="Total:"></listfooter> <listfooter> <label>??? what goes here ???</label> </listfooter> </listfoot> </listbox>
Or use a Custom Converter instead.
I agree, ugly and not working :)
Move your logic to vm, and try to manage notification properly - a bit annoying but powerful.
I was thinking more about something like
<label value="@load(vm.tableModel[0].price + vm.tableModel[1].price + vm.tableModel[2].price)"></label>
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?
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>
Asked: 2012-11-09 08:39:01 +0800
Seen: 92 times
Last updated: Nov 14 '12