How to Use VariableResolver with a ListBox

RomanZK
1 1

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>();

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

return listModelLov;
}


3) The GenericLov is a class that has two fields

private Short code;
private String value;

with getters and setters

delete retag edit

3 Replies

cor3000
5868 2 7

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.

RomanZK
1 1

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

 <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.

cor3000
5868 2 7

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

[hide preview]