-
FEATURED COMPONENTS
First time here? Check out the FAQ!
As default, Combobox dropdown filter is case significant.
Can we have filtering without case significance?
Example:
<window title="Combo box with auto drop down" border="normal"> Auto-complete Combobox: <combobox id="combo" autodrop="true" buttonVisible="false" /> <zscript> String[] _dict = { "abacus", "accuracy", "acuity", "bird", "bingle", "blog", "cabane", "cape", "cease", "yea", "yellow", "zebra", "zk", }; ListModel dictModel = new SimpleListModel(_dict); combo.setModel(dictModel); </zscript> </window>
If we type 'a', it will show the matches. But, if we type 'A', it will display nothing in the drop-down.
Thanks.
AFAIK, there's no option for setting combobox case-sensitivy. The matching between input text and elements from the model is done by SimpleListModel.getSubModel(), there's where the matching happens.
What you can basically do is to extend SimpleListModel and overwrite getSubModel() to customize it with your own matching function. Take a look at SimpleListModel.getSubModel() source code.
Still there's another problem related with this which is important to notice. If some of your words contains a major letter in the middle, pressing <shift> in a combobox causes the text selected to be autocompleted, in other words, it's not possible to type a major letter in the middle of a word in a combobox. Try for example "aBacus" in your list and see what happens :) (I'm searching of a solution to this, in case you find out something, let me know).
Hope it helped,
Diego
OK, more or less I found a solution to this. Since overwriting SimpleListModel.getSubModel() you can control how the matching is done, it's possible to do a non case-sensitive matching, preventing the user from pressing shift key.
Something like this should work, use this SimpleListModelExt class to create a ListModel and feed your combobox.
public class SimpleListModelExt extends SimpleListModel { public SimpleListModelExt(List data) { super(data); } public ListModel getSubModel(Object value, int nRows) { final String idx = value == null ? "" : objectToString(value); if (nRows < 0) nRows = 10; final LinkedList data = new LinkedList(); for (int i = 0; i < getSize(); i++) { if (idx.equals("") || entryMatchesText(getElementAt(i).toString(), idx)) { data.add(getElementAt(i)); if (--nRows <= 0) break; // done } } return new SimpleListModelExt(data); } @Override public boolean entryMatchesText(String entry, String text) { return entry.toLowerCase().startsWith(text.toLowerCase()); } }
Thank you, Diego :)
I will let you know, if I found a way to have Shift key working with this.
Hi Diego,
I can able to use shift key while typing in the combobox and its not making autocomplete. I doubt you may be using older versions.
Am using 3.6.2 version for my development.
Example code:
import java.util.LinkedList; import java.util.List; import org.zkoss.zul.ListModel; import org.zkoss.zul.SimpleListModel; public class CustomSimpleListModel extends SimpleListModel { public CustomSimpleListModel(String[] data) { super(data); } public CustomSimpleListModel(List data) { super(data); } public ListModel getSubModel(Object value, int nRows) { final String idx = value == null ? "" : objectToString(value); if (nRows < 0) nRows = 10; final LinkedList data = new LinkedList(); for (int i = 0; i < getSize(); i++) { if (idx.equals("") || entryMatchesText(getElementAt(i).toString(), idx)) { data.add(getElementAt(i)); if (--nRows <= 0) break; // done } } return new CustomSimpleListModel(data); } public boolean entryMatchesText(String entry, String text) { return entry.startsWith(text); } }
I have two items such like, "Zenxces" & "ZenXces".
Now I can able to select exactly upto Zenx or ZenX, thereafter there is only unique item matching the criteria, otherwise its not auto completing even if I use shift-key.
Let me know, if you still facing any problem.
Thanks.
Asked: 2009-09-09 13:14:25 +0800
Seen: 1,016 times
Last updated: Sep 15 '09