I'm writing a composite component, based on Combobox
, to do tokenized autocomplete.
The basic idea is that the Combobox
queries the model for items that match the current text, and when the user selects something from the options presented it's added to another list for tokenized presentation. Overall it looks like the inputs you may have seen in Facebook, Apple Mail and various other places.
My current problem seems to stem from not really being able to determine when the user开发者_StackOverflow社区 has selected something from the list, as opposed to merely having navigated up/down the list using cursor keys. Combobox#onSelect
seems to be triggered when navigating the list of options in the drop down, and what I really need is a way to grab the selection when the user has done the "I want this one" action, typically pressing Enter with a selection in the drop down, or manually selecting an entry in the drop down with the mouse.
The best I have so far come up with is to monitor Combobox#onOpen
, check if OpenEvent#isOpen
is false, and then inspect the model manually for selection.
Is there a better way to become aware of, or detect, the "I want this one" scenario and differentiate it from the onSelect
events triggered during "I want the third option down so I'll DOWN, DOWN, DOWN + ENTER"?
The code below is basically what I'm looking at now
public void onOpen$input(OpenEvent oe) {
logger.info("OpenEvent.isOpen: {}", oe.isOpen());
if (oe.isOpen() == false) {
ListModel model = this.input.getModel();
logger.info("model: {}", model);
if (model instanceof Selectable) {
logger.info("model is Selectable");
Set<?> selection = ((Selectable) model).getSelection();
logger.info("selection: {}", selection);
for (Object selected : selection) {
logger.info("selected: {}", selected);
if (selected instanceof Comboitem) {
logger.info("selected is Comboitem");
selected = ((Comboitem) selected).getValue();
logger.info("selected: {}", selected);
}
addToken(selected);
}
}
}
}
Is it only impression or you really try to reimplement Bandbox but for Combobox?
Look at class description for org.zkoss.zul.Bandbox
Seems like they propose to listen to the same event onOpen.
精彩评论