0

How to Use VariableResolver with a ListBox

asked 2021-01-20 06:14:11 +0800

RomanZK gravatar image RomanZK
1 1

updated 2021-01-20 11:22:49 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

How can I use a variable resolver with a ListBox.

I have tried the following approaches:

<?variable-resolver class="com.LovResolver"?>

<listbox  mold="select" width="100px"
          selectedItem="@bind(each.attributes.accountCategory.value)">
     <listitem label="${each.value}" forEach="${country}" value="${each.code}"/>
</listbox>

The Lov display but the value in field accountCategory does not display.

OR

 <listbox model="${country}" mold="select" width="200px"
          selectedItem="@bind(each.attributes.accountCategory.value)">
    <template name="model" var="lov">
        <listitem label="${lov.value}" value="${lov.code}" />
    </template>
 </listbox>

The above code snippet throws errors.

Points to note:

1) My data source is a class that has a map of attributes.

2) I have implemented a variable resolver that returns the list of values as follows:

@Override
public Object resolveVariable(String name) throws XelException {

     ....  // some code here


      List<GenericLOV> listOfValues = new ArrayList<GenericLOV>();


      listOfValues.add(new GenericLOV(new Short("566"), "Nigeria"));
      listOfValues.add(new GenericLOV(new Short("891"), "Yugoslavia"));
      listOfValues.add(new GenericLOV(new Short("894"), "Zambia"));

      ListModelList<GenericLOV> listModelLov = new ListModelList<GenericLOV>();

      listModelLov.addAll(listOfValues);


      return listModelLov;
}

3) The GenericLov is a class that has two fields

private Short code;
private String value;

with getters and setters
delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2021-01-20 12:35:59 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

updated 2021-01-20 12:38:45 +0800

Your problem is not with variable resolver but rather the fact that your model contains different object than the selected value (which I assume is just a number (short))

Since you didn't provide any error messages or runnable code I can't comment on why things are not working in your attempt. Instead here my take on your scenario as a runnable ZK fiddle:

https://zkfiddle.org/sample/1002pcs/1-listbox-item-selection

Instead of your GenericLov I simply use a LinkedHashMap which is essentially the same.

Then I make sure the model contains only the Short numbers (which is the selected value as well), the display label is converted from Short to String using a @converter (For convenience I defined a read only converter for display purposes).

I expose both the list of countryCodes and the countryDisplayConverter via VariableResolver. However a variable resolver is not necessary here (you could simply have getters on the viewmodel returning the list of country codes and the converter).

I hope this helps, feel free post a modified version of the example in case you have further questions.

link publish delete flag offensive edit

answered 2021-01-26 04:35:36 +0800

RomanZK gravatar image RomanZK
1 1

updated 2021-01-26 09:55:04 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

I am just realising I made a mistake. The field in question (i.e. accountCategory) is a multi valued field. It returns List<short>.

So I adjusted your code snippet as follows:

 <listbox model="@init(countryCodes)" mold="select" width="100px"
          selectedItems="@bind(each.attributes.accountCategory.values)" 
          multiple="true">
    <template name="model" var="countryCode">
        <listitem label="@init(countryCode) 
                         @converter(countryDisplayConverter)" />
    </template>
 </listbox>

The code works.

However, I also want to add that the previous code I shared now works as well.

<?variable-resolver class="com.LovResolver"?>
<listbox mold="select" width="100px"
         selectedItems="@bind(each.attributes.accountCategory.values)" 
         multiple="true">
    <listitem label="${each.description}" forEach="${accountCategory}" 
              value="${each.code}"/>
</listbox>

This code uses the generic lov class.

Good to know that zk offers different options to achieve the same functionality.

Thanks.

link publish delete flag offensive edit

answered 2021-01-26 09:56:55 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

thanks for the update, good to hear you found a working version in for your application

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
1 follower

RSS

Stats

Asked: 2021-01-20 06:14:11 +0800

Seen: 12 times

Last updated: Jan 26

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