0

Override onsearching in chosenbox

asked 2016-03-23 09:12:08 +0800

Sailu gravatar image Sailu
1

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

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-03-23 12:18:11 +0800

cor3000 gravatar image cor3000
4332 1 7
ZK Team

updated 2016-03-23 12:23:34 +0800

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

link publish delete flag offensive edit
0

answered 2016-03-23 10:27:23 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

updated 2016-03-23 10:33:45 +0800

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.

link publish delete flag offensive edit
Your answer
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
2 followers

RSS

Stats

Asked: 2016-03-23 09:12:08 +0800

Seen: 34 times

Last updated: Mar 23 '16

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