0

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

asked 2012-11-09 08:39:01 +0800

czynga gravatar image 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)">
     <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>

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2012-11-14 10:00:07 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Or use a Custom Converter instead.

link publish delete flag offensive edit

answered 2012-11-09 09:28:31 +0800

Steva77 gravatar image Steva77 flag of Italy
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.

link publish delete flag offensive edit

answered 2012-11-09 09:21:10 +0800

czynga gravatar image czynga
171

updated 2012-11-09 09:22:10 +0800

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.

link publish delete flag offensive edit

answered 2012-11-09 09:16:02 +0800

czynga gravatar image 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?

link publish delete flag offensive edit

answered 2012-11-09 08:50:43 +0800

Steva77 gravatar image Steva77 flag of Italy
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>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-11-09 08:39:01 +0800

Seen: 92 times

Last updated: Nov 14 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More