-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi All,
I have a requirement to use chosenbox but the filter functionality should work if the input value is found anywhere in the chosen option list. I analyzed the filter functionality works with "Startwith" of the input value but i have to change it to "contains".
For examnple: if the listmodel contain 3 options as: ABC BAC CCB
Then if user enter "a" as input then it should filter both "ABC" and "BAC" as both contains the letter "a".
another way is to use a ListSubModel
<zk>
<chosenbox width="300px" noResultsText="no matches"
apply="zk.support.forum.ChosenBoxComposer"/>
</zk>
and create the model in your composer
package zk.support.forum;
import java.util.Comparator;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zkmax.zul.Chosenbox;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.ListModels;
public class ChosenBoxComposer extends SelectorComposer<Chosenbox> {
@Override
public void doAfterCompose(Chosenbox comp) throws Exception {
super.doAfterCompose(comp);
//Create a ListModelList containing/providing the available elements
ListModelList<String> listModel = Stream.of(Locale.getAvailableLocales())
.map(Locale::getDisplayName)
.collect(Collectors.toCollection(ListModelList::new));
//comparator to match the items
Comparator<String> comparator = (search, localeName) ->
localeName.toLowerCase().contains(search.toLowerCase()) ? 0 : 1;
//wrap the model into a ListSubModel using the comparator,
//and a result limit of 10 elements
ListModel<String> subModel = ListModels.toListSubModel(listModel, comparator, 10);
comp.setModel(subModel);
}
}
when typing up to 10 matching elements are shown in the available choices.
http://books.zkoss.org/wiki/ZKComponentReference/Input/Chosenbox#SubListModel
Quick fix => copy class, past it in your project (same FQN) and alter the startsWidth to contains.
Good fix =>
ZK should think about updating this line :
s.toLowerCase(java.util.Locale.ENGLISH).startsWith(prefix)
To :
match(s,prefix)
where method match:
protected boolean match(String s, String prefix) {
return s.toLowerCase(java.util.Locale.ENGLISH).startsWith(prefix);
}
Advantage is when subclassing this class => you can override the match method and implement your own one. (I'll notify ZK of this topic)
With the subclass you can use the chosenbox like this :
<chosenbox use="my.path.MyChosenbox" ... />
Greetz chill.
Asked: 2016-03-23 09:12:08 +0800
Seen: 38 times
Last updated: Mar 23 '16