Seems like a good idea, thank you, I'll try it today.
MathieuPiette ( 2016-04-26 06:23:49 +0800 )edit-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
Is it possible to prevent/cancel the selection on a Listbox? When I click an item, I want to run a command to verify I can change the selection. If I can't, the event should be stopped so that the selection wouldn't change.
I tried this:
zul:
<listbox onSelect="@command('confirmYouCanSelect')" ... >
...
</listbox>
ViewModel:
@Command
public void confirmYouCanSelect(@ContextParam(ContextType.TRIGGER_EVENT) Event event) {
if (someTest) {
event.stopPropagation();
}
}
But this doesn't work, the event is not stopped and the clicked item is selected.
Did I miss something obvious?
Thank you!
Hello MathieuPiette,
You could use the selectedItem and check if a value has been set.
Best Regards,
Darksu
Seems like a good idea, thank you, I'll try it today.
MathieuPiette ( 2016-04-26 06:23:49 +0800 )editHi Mathieu,
the command is called after the event has already successfully updated the selection. So that's not a way to prevent selection.
To enable this you'd need a custom event (s.th. like onBeforeSelection) that fires before the real onSelect is executed (requires a custom Listbox component).
Apart from that the SelectionEvent contains the previousSelection so you can also revert it: https://www.zkoss.org/javadoc/8.0.0/zk/org/zkoss/zk/ui/event/SelectEvent.html#getPreviousSelectedObjects%28%29
@ContextParam(ContextType.TRIGGER_EVENT) SelectEvent selectEvent
A third option is to make the items non selectable in the listbox while rendering. This will prevent this situation in the first place. https://www.zkoss.org/javadoc/latest/zk/org/zkoss/zul/Listitem.html#setSelectable%28boolean%29
Thank you for your answer. Reverting the selection is not really an option, because there is a form object that would have to be restored too and I don't know how to do. I would prefer not to disable all selection if it's possible but, this is indeed a solution.
MathieuPiette ( 2016-04-26 06:26:26 +0800 )editI'll make a custom Listbox if Darksu's solution doesn't work. That's a pity there is no onBeforeSelection event, or at an other way of running some code before the selection takes place. What's the use of event.stopPropagation() if it's too late when we can call it?
MathieuPiette ( 2016-04-26 06:28:12 +0800 )editAnd I think of a 4th option.
You can set the save option after a specific command.
If this command fails, let's say by a WrongValueException, the selectedItem will never be updated.
Fiddle where you can test it :
It seems to work, indeed. But I implemented Darksu's solution because it seemed more natural to me.
MathieuPiette ( 2016-04-26 12:59:27 +0800 )editAsked: 2016-04-25 14:54:51 +0800
Seen: 44 times
Last updated: Apr 26 '16