Revision history [back]

click to hide/show revision 1
initial version

asked 2016-01-02 22:21:56 +0800

KlausWr gravatar image KlausWr

MVVM combobox with model: isn't there an easy way to deal with the value???

I am really a fan of zk. But some of the concepts(?) still drive me crazy!

It took me now 2 hours to figure out that there is no easy way to use a combobox (in MVVM with model binding) and deal with the comboitem's "value" as the comobox' selected code instantly.

While this simple sample (works):

java: 
   public List<String> getList() {...} // "A", "B", "C"
   public String getSelectedItem() {...}
   public void setSelectedItem(String item) {...}

zul: <combobox model="@load(vm.list)" selectedItem="@bind(vm.selectedItem)" />
       <template name="model">
          <comboitem label="@load(each)" />
       </template>
      </combobox>

You cannot just add a "value" to the comboitem and expect this value to be returned when selected:

java: 
   public List<NameValuePair> getList() {...} // "A"->"1", "B"->"2", "C"->"3"
   public String getSelectedItem() {...}
   public void setSelectedItem(String item) {...}

zul: <combobox model="@load(vm.list)" selectedItem="@bind(vm.selectedItem)" />
       <template name="model">
          <comboitem label="@load(each.name)" value="@load(each.value)" />
       </template>
      </combobox>

Well, the setter setSelectedItem gets called (!) but the value is NOT the value of the selected item (e.g. "2") BUT the mangeled instance name of the selected NameValuePair or Comboitem instance or ??? (huh??).

After googeling tons of questions and answers I now know that the selectedItem always deals with an instance of Comboitem and you have to use getValue() on this instance to get the real value. Same for setting the initial selection. Somehow same. Never as easy as a string compare!

And there are hundreds of other tips to get it done (custom item renderer, model class implementing Comparable, ...

Hey, isnt't it the most obvious use of a combobox to present a human readable label and let select a machine readable unique code and then use this code again to mark the selected item in the combobox?

I know that combobox obviously works as designed.

But then - sorry - the design is over engineered and crappy.

Still a fan ;-( Klaus

MVVM combobox with model: isn't there an easy way to deal with the value???

I am really a fan of zk. But some of the concepts(?) still drive me crazy!

It took me now 2 hours to figure out that there is no easy way to use a combobox (in MVVM with model binding) and deal with the comboitem's "value" as the comobox' selected code instantly.

While this simple sample (works):

java: 
   public List<String> getList() {...} // "A", "B", "C"
   public String getSelectedItem() {...}
   public void setSelectedItem(String item) {...}

zul: <combobox model="@load(vm.list)" selectedItem="@bind(vm.selectedItem)" />
       <template name="model">
          <comboitem label="@load(each)" />
       </template>
      </combobox>

You cannot just add a "value" to the comboitem and expect this value to be returned when selected:

java: 
   public List<NameValuePair> getList() {...} // "A"->"1", "B"->"2", "C"->"3"
   public String getSelectedItem() {...}
   public void setSelectedItem(String item) {...}

zul: <combobox model="@load(vm.list)" selectedItem="@bind(vm.selectedItem)" />
       <template name="model">
          <comboitem label="@load(each.name)" value="@load(each.value)" />
       </template>
      </combobox>

Well, the setter setSelectedItem gets called (!) but the value is NOT the value of the selected item (e.g. "2") BUT the mangeled instance name of the selected NameValuePair or Comboitem instance or ??? (huh??).

After googeling tons of questions and answers I now know that the selectedItem always deals with an instance of Comboitem and you have to use getValue() on this instance to get the real value. Same for setting the initial selection. Somehow same. Never as easy as a string compare!

And there are hundreds of other tips to get it done (custom item renderer, model class implementing Comparable, ...

Hey, isnt't it the most obvious use of a combobox to present a human readable label and let select a machine readable unique code and then use this code again to mark the selected item in the combobox?

I know that combobox obviously works as designed.

But then - sorry - the design is over engineered overengineered and crappy.

Still a fan ;-( Klaus

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