Saturday, November 28, 2009

ActionListener and showPopupBehavior

Many times developers complain that actionListener of a button is not invocked when they add a showPopupBehavior. Yet that is not a bug.

By default showPopupBehavior the action type is Action that is a client event and does not invoke server actions. That is described in af:showPopupBehavior documentation:

The af:showPopupBehavior tag is a declarative way to show a af:popup in response to a client-side event. The client event is specified using the triggerType attribute. The "action" event is the default triggerType if one is not provided.
....

All actionListener method bindings and associated action listeners will not be invoked when the triggerType of "action" is used.

So the solution is to just set the triggerType of ShowPopupBehavior to click.



Then the actionListener and popup are invoked.



Edit 19/12/12:
As posted in the comments bellow, this is not the best possible workaround. It is more like a quick and dirty solution. With this solution even if the button is disabled the popup is still opened without the Action listener invoked.
A better solution if you need an action listener on buttons that opens a popup is not to use the showPopupBehavior but to open the popup from within the action listener by binding the popup to the managed bean and use popup.show() method.

9 comments:

  1. For me it didn't worked in Safari 4 browser with 'click' event... Should I buy Windows and IE? Eh... :)

    ReplyDelete
    Replies
    1. Hi Andrejus, if you are not happy with solution, why don't you provide a "correct" way of dealing with this problem ?

      Delete
  2. Excellent solution!

    Do we have something similar for "file download listener" and "export collection listener" too?

    Thanks

    ReplyDelete
  3. Hello Husain. From what i have seen af:fileDownloadActionListener and af:exportCollectionActionListener does not prevent execution of actionListener of buttons they are in.
    Can you be more specific?

    Thanks for comment
    Michael

    ReplyDelete
  4. It works only with CommandToolbarbutton and not for Commandbutton.. that is strange though !

    ReplyDelete
  5. Probably because commandToolbarButton has PartialSubmit set to TRUE by default, whereas commandButton does not. Try setting PartialSubmit to TRUE for commandButton and see if it then works.

    ReplyDelete
  6. This is a really shitty workaround, as setting it to click doesnt have a fix order of invoking the pop up and the actionListener. its weird.

    ReplyDelete
  7. Thanks, Matthew! PartialSubmit = True worked!

    ReplyDelete