7 Replies Latest reply on May 11, 2010 3:24 PM by Karl Krasnowsky

    Wanted - a4j:commandButton conditional action solution.

    Karl Krasnowsky Newbie

      Hello again,

      The more I try the less I know.

      What I have is a rich:modalPanel object that I'm wanting to use for multiple situations.

       

      The UI portion of it simply displays a dialog that asks for confirmation to continue.

      Within this initial try of mine contains the action on a a4j:commandButton that performs the requested action upon the user clicking on the OK button.

       

      This is fine as long as I'm using it for a specific reason and/or the resulting action is applicable to all conditions. But I want to refer to a different action depending on the context that it was exectued. So, e.g., I have something like this:

       

      <a4j:commandButton value="Yes" ajaxSingle="true" action="#{mybean.mainmenu}"
                     oncomplete="#{rich:component('cancelPanel')}.hide();"
                     reRender="table" >
      </a4j:commandButton>

       

      but what are my options if I want do something other then #{mybean.mainmenu}, say, e.g., #{mybean.logout}?

      It won't read a string property and use that as it's expecting an actual command to execute.

      I'm changing state in my backing bean on the event of the calling client being (in this case a button) clicked so I'm wanting something like this:

       

      action="if (myBean.mainMenu() == true)#{mybean.mainmenu} else #{authbean.logout}"

       

      Or something along those lines?

        • 1. Re: Wanted - a4j:commandButton conditional action solution.
          Rob Acraman Newbie

          You can't do anything else in the action parameter;  That has to be a straight call to a backing bean.  In particular, it certainly does NOT take javascript.

           

          So, you have two options:

          1) Don't have an action, and instead whack it all into your onclick.  You can't call the backing bean directly as in your "something like this";  Instead you'd have to bring in "a4j:jsFunction"s for the job.

          Having one component serve multiple functions is asking for trouble, and will get real messy real fast 

          Not recommended.

           

          2) Much better is to simply have multiple buttons, and only render those that are applicable - eg:

           

          <a4j:commandButton  value="Yes" ajaxSingle="true" action="#{mybean.mainmenu}"

                         rendered="#{not mainbean.isMainMenu}"
                          oncomplete="#{rich:component('cancelPanel')}.hide();"
                          reRender="table" />

          <a4j:commandButton  value="Logout" ajaxSingle="true" action="#{mainbean.logout}" immediate="true"
                       rendered="#{mainbean.isMainMenu}"

                          oncomplete="#{rich:component('cancelPanel')}.hide();"
                          />

          • 2. Re: Wanted - a4j:commandButton conditional action solution.
            Rob Acraman Newbie

            Even though I don't recommend the first option, here's an example - sometimes it does serve a purpose (eg. to wrap some alerts around the call while dubugging, etc).

             

            So your example would become:

             

            <a4j:jsFunction name="showMainmenu" action="{mybean.mainmenu}"/>

            <a4j:jsFunction name="doLogout" action="{authbean.logout}"/>

             

            <a4j:commandButton  value="Yes" ajaxSingle="true"

                           onclick="if  (myBean.mainMenu() == true) showMainmenu() else doLogout()l"
                            oncomplete="#{rich:component('cancelPanel')}.hide();"
                            reRender="table" />

             

             

            But again - I'd REALLY recommend separate buttons!

            1 of 1 people found this helpful
            • 3. Re: Wanted - a4j:commandButton conditional action solution.
              Karl Krasnowsky Newbie

              (edit: I didn't see your 2nd post until after I wrote this... that might be a better solution given this situation)

               

              Hey Rob,

              Problem is, both buttons are required to be available on the page at the same time. e.g., there is a Cancel button (which when selected is required to return to the main menu page), and a logoff button (which when selected returns to the logon page) and I want to provide a like confirmation message for both since in both cases all changes up to that point will be lost if the event proceeds.

               

              Is there functionality available for me to make this context switch for the backing bean and perform the action routine from the javacode? e.g., have a method mybean.doCancelProcess which keys off of a state set when one or the other button is clicked?

               

              thanks,

              Karl

              • 4. Re: Wanted - a4j:commandButton conditional action solution.
                Ilya Shaikovsky Master

                I guess this series of articles will be helpfull

                1 of 1 people found this helpful
                • 5. Re: Wanted - a4j:commandButton conditional action solution.
                  Karl Krasnowsky Newbie

                  Hi Ilya,

                  Well, not surprisingly, this article is where I started, but still the same problem.

                  Fundamentally at issue is that it's the confirmation dialog that issues the action, not the calling client. Most "traditional" such dialogs are normally a simple mechanism for returning true or false depending on what the user chooses and the client who initiated the confirmation dialog has the logic on what to do with the response.

                   

                  This not only handles the action, but it hardcodes the action, expecting all clients wanting confirmation services to do the same thing. So I guess my task is, as the author did in handling the client id, figure out how to make this a dynamic bit in the calling client of the facet.

                  • 6. Re: Wanted - a4j:commandButton conditional action solution.
                    Karl Krasnowsky Newbie

                    Oh, maybe not. While this example is very similar to what I had originally worked from it does appear to be fundamentally different. Thanks!

                    • 7. Re: Wanted - a4j:commandButton conditional action solution.
                      Karl Krasnowsky Newbie

                      just realized how painful it is continuing to use antiquated JSF 1.2 libraries. I gotta get this company to port up to a more usable plateform.