0

Combobox dropdown filter is case significant.

asked 2009-09-09 13:14:25 +0800

baskaraninfo gravatar image baskaraninfo
526 2 2 9

updated 2009-09-09 13:15:02 +0800

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.

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2009-09-10 08:54:59 +0800

dpino gravatar image dpino
36

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

link publish delete flag offensive edit

answered 2009-09-10 10:44:53 +0800

dpino gravatar image dpino
36

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());
    	}
}

link publish delete flag offensive edit

answered 2009-09-10 10:54:13 +0800

baskaraninfo gravatar image baskaraninfo
526 2 2 9

updated 2009-09-10 10:56:46 +0800

Thank you, Diego :)

I will let you know, if I found a way to have Shift key working with this.

link publish delete flag offensive edit

answered 2009-09-10 11:46:42 +0800

baskaraninfo gravatar image baskaraninfo
526 2 2 9

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.

link publish delete flag offensive edit

answered 2009-09-15 17:06:12 +0800

dpino gravatar image dpino
36

Good to know you're not facing that problem. I'm using ZK 3.6.1, maybe it's because of that. I have to switch to 3.6.2 then.

Regards,

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2009-09-09 13:14:25 +0800

Seen: 1,012 times

Last updated: Sep 15 '09

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