1

Stop the propagation of the onSelect event of a Listbox

asked 2016-04-25 14:54:51 +0800

MathieuPiette gravatar image MathieuPiette
1567 3

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!

delete flag offensive retag edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2016-04-25 15:07:08 +0800

Darksu gravatar image Darksu
1991 1 4

Hello MathieuPiette,

You could use the selectedItem and check if a value has been set.

Best Regards,

Darksu

link publish delete flag offensive edit

Comments

Seems like a good idea, thank you, I'll try it today.

MathieuPiette ( 2016-04-26 06:23:49 +0800 )edit

I implemented your solution and it works!

MathieuPiette ( 2016-04-26 12:57:19 +0800 )edit
1

answered 2016-04-26 01:30:42 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2016-04-26 01:52:58 +0800

Hi 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

link publish delete flag offensive edit

Comments

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 )edit

I'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 )edit
1

answered 2016-04-26 06:43:46 +0800

chillworld gravatar image chillworld flag of Belgium
5357 4 9
https://github.com/chillw...

updated 2016-04-26 07:07:29 +0800

And 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 :

http://zkfiddle.org/sample/kaqld8/2-mvvm-stop-propagation

link publish delete flag offensive edit

Comments

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 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2016-04-25 14:54:51 +0800

Seen: 44 times

Last updated: Apr 26 '16

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