How to make combobox display autocomplete options?

This is my zul code:

<combobox id="digitalPublisherCombobox" value="@load(ivm.inventory.digitalPublisherName)" onOK="@command('setDigitalPublisher', digitalPublisherBox = self)" onSelect="@command('setDigitalPublisher', digitalPublisherBox = self)" onChanging="@command('setupQuicksearchByEvent', searchlayout = event, prefix = 'PUB', tags = 'PublisherName, PublisherNameTranslit')" mold="rounded" hflex="1" buttonVisible="false" autodrop="true"> <comboitem self="@{each=entry}" value="@{entry.key}" label="@{entry.value}"/> </combobox> 

And this is the implementation of QuickSearch:

 @Command public void setupQuicksearchByEvent(@BindingParam("searchlayout")Event event, @BindingParam("prefix") String prefix, @BindingParam("tags") String tags) throws WrongValueException, SearchException, IOException { if(event instanceof InputEvent) { InputEvent inputEvent = (InputEvent) event; String inputText = inputEvent.getValue(); List<String> searchFields = Arrays.asList(tags.split(",")); ListModel model = new ListModelMap(ZKLogic.findDocsStartingWith(prefix, searchFields, "proxy", inputText), true); ListModel subModel = ListModels.toListSubModel(model, Autocompleter.MAP_VALUE_CONTAINS_COMPARATOR, 10); Combobox searchBox = (Combobox) event.getTarget(); searchBox.setModel(subModel); searchBox.setItemRenderer(new ComboitemRenderer() { @Override public void render( Comboitem item, Object data, int pos ) throws Exception { String publisherString = data.toString(); UID key = getUidFromPublisherString(publisherString); int startIndex = publisherString.indexOf('=') + 1; String publisher = publisherString.substring(startIndex); item.setLabel(publisher); item.setValue(key); } }); } } 

ZKLogic.findDocsStartingWith return the map using the UID key and String value.

With the code above, I got a dropdown list when switching to another window. I need to enter something, then select another browser or notepad window - and combo elements will be displayed immediately.

So my question still needs to be answered, are there any methods for reproducing this window switch in the code? Or maybe I should do something with autocompletion, because I have work with preloaded lists, but this thing should only return 10 entries from db, not all 70,000 entries, each time the user enters something then in the field.

Edit 09/20/2013: The problem still exists. Rename the question a bit, because what I need is to call the render parameter with code. Is there any way to do this? The code hasn't changed much, but the print option in the render method said that this method can skip two or more Change events and suddenly display text for one option.

Perhaps you know another autocomplete option in the zk framework where the database is involved? I am ready to change the implementation if there is a manual with its working implementation.

+6
source share
3 answers

The solution is simple. Indeed. Nothing is better than brute force, but I think I tried to avoid it and use it in despair.

  @Command public void setupQuicksearchByEvent(@BindingParam("searchlayout")Event event, @BindingParam("prefix") String prefix, @BindingParam("tags") String tags) throws WrongValueException, SearchException, IOException { if(event instanceof InputEvent) { InputEvent inputEvent = (InputEvent) event; String inputText = inputEvent.getValue(); List<String> searchFields = Arrays.asList(tags.split(",")); Map<UID, String> publishers = ZKLogic.findDocsStartingWith(prefix, searchFields, "proxy", inputText); Combobox searchBox = (Combobox) event.getTarget(); searchBox.getChildren().clear(); for (Map.Entry<UID, String > entry : publishers.entrySet()) { Comboitem item = new Comboitem(); item.setLabel(entry.getValue()); item.setValue(entry.getKey()); searchBox.appendChild(item); } } } 
0
source

Ok, I see two problems, you have to solve first.

  • Setting up the renderer with each call to setupQuicksearchByEvent(...) .
    this is not logical, because each time the same. Add something like to the zul combobox tag
      itemRenderer="@load(ivm.myRenderer)" .... 
  • If you want a total of 10 elements, don't let the db query return more than 10.
    If you use JPA click here or for sql here or just a little google.

After resolving these two problems, we can eliminate them as the cause of the unexpected behavior and correct it if it is still present.

Edit

Well, I have two possible ways to fix this.

  • Call Combobox#invalidate()
    This should force zk to restart the Combobox , but it can lead to poor performance, and I would not prefer it.

  • Use a Listbox with the selected form instead of Combobox .
    To force rerender to use Listbox#renderAll()

+3
source

Try setting the selected item in your combo box or throwing an event associated with it

+1
source

Source: https://habr.com/ru/post/953295/


All Articles