0

Cell level recalc in MVVM grid?

asked 2013-05-15 07:54:01 +0800

davout gravatar image davout
1435 3 18

updated 2013-05-15 07:58:43 +0800

I have a MVVM grid where I am displaying a list of currency exchange rates. The grid has the following columns:

  • From currency (e.g. US Dollars)
  • To currency (e.g. Euro)
  • Multiply or divide (a radio button to choose whether the exchange should be applied as a multiplier or divider)
  • Exchange rate (the exchange rate as s decimal number)
  • Example result (an example rate calculation , like US$100 = EURO 67.54)

The user can choose to enter a new exchange rate in any cell under the 'Exchange Rate' column' or choose to change the 'Multiply/Divide' radio button selection. In either case I would like the values under the 'Example result' column cell to be updated immediately.

How can I do this under MVVM? Is it possible to define '@DependsOn' annotations at a grid or listbox row level?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-05-20 08:49:22 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

An easier way is calculate value in zul page via mvvm,

e.g.,

zul

<zk>
     <window apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.TestVM')">
        <grid model="@bind(vm.model)">
            <template name="model">
                <row>
                    <intbox value="@bind(each.cost)"/>
                    <intbox value="@bind(each.amount)"/>
                    <label value="@bind(each.cost * each.amount)"/>
                </row>
            </template>
        </grid>
    </window>
</zk>

TestVM.java

package test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

/**
 * tested with ZK 6.5.1.1
 * 
 * @author benbai
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {
    ListModelList _model;
    public ListModel getModel () {
        if (_model == null) {
            List l = new ArrayList();
            Random r = new Random();
            for (int i = 0; i < 10; i++) {
                l.add(new TestData(r.nextInt(10), r.nextInt(10)));
            }
            _model = new ListModelList(l);
        }
        return _model;
    }
}

TestData.java

package test;

public class TestData {
    private int _cost;
    private int _amount;
    public TestData (int cost, int amount) {
        _cost = cost;
        _amount = amount;
    }
    public void setCost (int cost) {
        _cost = cost;
    }
    public int getCost () {
        return _cost;
    }
    public void setAmount (int amount) {
        _amount = amount;
    }
    public int getAmount () {
        return _amount;
    }
    public int getTotalPrice () {
        return _cost * _amount;
    }
}

or you need to handle event and notify change manually.

link publish delete flag offensive edit
0

answered 2013-05-20 21:26:01 +0800

davout gravatar image davout
1435 3 18

Thanks for the suggestion...

I'd managed to work out a solution that uses 'onChange' type events on the input fields to call a '@command('updateCalcs') MVVM method that does the dependent field calculations, and then on the dependent fields use a '@load(vm.example,after='updateCalcs') annotation that will update accordingly.

This does seem a little ponderous. I'm wondering why ZK doesn't provide @DependsOn support at a model row level.

link publish delete flag offensive edit

Comments

just need more time, I guess

benbai ( 2013-05-21 00:53:00 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2013-05-15 07:54:01 +0800

Seen: 21 times

Last updated: May 20 '13

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