-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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.
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.
Asked: 2021-01-20 06:14:11 +0800
Seen: 12 times
Last updated: Jan 26 '21
bug with intboxes on mobile devices
zk keikai-how to add custom button/label to formulabar?
zk-keikai- update multiple cells parallel at same time asynchronously
zk-keikai-How to auto fit column width based on text
zk-keikai-ClipboardPateEvent-called twice