8 Replies Latest reply on May 6, 2011 10:47 AM by lvdberg

    Edit Action in ContextMenu of DataTable

    bemar

      Hello together,


      I have a DataTable with some lines of Incidents. Now I want to implement a context menu in which the user can edit or view the details of the selected line.
      How can I do this? The problem is I outject the details object by a action method in an application bound IncidentHandler which has the id of the incident as parameter, loads the incident and outjects it to get viewed or edited in the related page.


      So my question is, how can I call the method of the handler with the right id parameter?
      Currently I'm using a s:button. Are there other ways to do this?


      Heres my code. The incidentId parameter is in every case 0 (zero).


      Any help would be apreciated.


      Thx in advance


      Ben


      Handler:
      @Name("incidentHandler")
      @Scope(ScopeType.CONVERSATION)
      public class IncidentHandler
      {
      
          @In
          protected EntityManager entityManager;
      
          @In
          protected FacesMessages facesMessages;
      
          @Out
          private Incident        incident;
      
          @In(create = true)
          Failmessage             failmessage;
      
          @Begin(join = true, flushMode = FlushModeType.MANUAL)
          // @Begin(join = true, flushMode = FlushModeType.MANUAL)
          @Restrict("#{identity.loggedIn}")
          // avoids having to add login redirect logic in flow (see chapter 11)
          public String editIncident(Long incidentId)
          {
              Incident inc = entityManager.find(Incident.class, incidentId);
              if (inc != null)
              {
                  this.incident = inc;
                  return "loaded";
              } else
              {
                  this.incident = new Incident();
                  facesMessages.add("The incident with id ["+incidentId+"] was not found.");
                  return "failed";
              }
          }
          
      .....
      }



      My List Page


      <h:form id="incidentTable" styleClass="edit">
      
                              <rich:contextMenu attached="false" id="menu" submitMode="ajax">
                                      <rich:menuItem ajaxSingle="true" event="oncontextmenu">
                                              <s:button
                                                      action="#{incidentHandler.editIncident(incidentId)}"
                                                      id="incidentEdit" value="#{messages.text_incidentedit}" />
                                      </rich:menuItem>
                                      <rich:menuItem ajaxSingle="true">
                                              <s:button
                                                      action="#{incidentHandler.viewIncident(incidentId)}"
                                                      id="incidentView" value="#{messages.text_incidentview}" />
                                      </rich:menuItem>
                              </rich:contextMenu>
      
                              <rich:panel>
                                      <f:facet name="header">#{messages.text_foundincidents} (#{empty incidentList.resultList ? 0 : (incidentList.paginated ? incidentList.resultCount : incidentList.resultList.size)})</f:facet>
                                      <div class="results" id="incidentList"><h:outputText
                                              value="#{messages.text_nothingfound}"
                                              rendered="#{empty incidentList.resultList}" /> <rich:dataTable
                                              onRowMouseOver="this.style.backgroundColor='#F1F1F1'"
                                              onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'"
                                              id="incidentList" var="_incident"
                                              value="#{incidentList.resultList}"
                                              rendered="#{not empty incidentList.resultList}">
                
                <rich:dataTable>
                ... here are the headers and columns of the table ...
      
                                              <rich:componentControl event="onRowClick" for="menu"
                                                      operation="show">
                                                      <f:param value="#{_incident.idIncident}" name="incidentId" />
                                              </rich:componentControl>
                                      </rich:dataTable>
                              </rich:panel>
      </h:form>



      Here is the list.page file



      <param name="incidentId" />
      
      <navigation from-action="#{incidentHandler.viewIncident(incidentId)}">
                      <rule if-outcome="loaded">
                              <redirect view-id="/Incident.xhtml" />
                      </rule>
                      <rule if-outcome="failed">
                              <redirect view-id="/fail.xhtml" />
                      </rule>
              </navigation>
      
              <navigation from-action="#{incidentHandler.editIncident(incidentId)}">
                      <rule if-outcome="loaded">
                              <redirect view-id="/IncidentEdit.xhtml" />
                      </rule>
                      <rule if-outcome="failed">
                              <redirect view-id="/fail.xhtml" />
                      </rule>
              </navigation>









        • 1. Re: Edit Action in ContextMenu of DataTable
          bemar

          Of course not application bound. Its conversion bound .....

          • 2. Re: Edit Action in ContextMenu of DataTable
            lvdberg

            Hi,


            have you looked at the Richfaces demo page. Its example is really very good and it explains how to bind the different parameters.


            I think you should try something like




            ...
            <rich:menuItem ajaxSingle="true" ... >
                <a4j:actionparam name="yourID" assignTo="#{incidentHandler.yourId}" value="{incidentId}"/>
            </rich:menuItem>
            ...




            This means you have to add an yourId property.


            It's basically transferring the JScript variable to the submit-EL. I haven't tried this, and maybe it's pure rubbish, but have you tried:




            <s:button action="#{incidentHandler.editIncident( {incidentId} )}" id="incidentEdit" value="#{messages.text_incidentedit}" />
            





            Leo

            • 3. Re: Edit Action in ContextMenu of DataTable
              bemar

              Hi Leo,


              your proposal


              <s:button action="#{incidentHandler.editIncident( {incidentId} )}" id="incidentEdit" value="#{messages.text_incidentedit}" />



              isn't working. It didn't expect a brace there.


              Caused by: org.jboss.el.parser.ParseException: Encountered "{" at line 1, column 32.
              Was expecting one of:
                  <INTEGER_LITERAL> ...
                  <FLOATING_POINT_LITERAL> ...
                  <STRING_LITERAL> ...
                  "true" ...
                  "false" ...
                  "null" ...
                  "(" ...
                  ")" ...
                  "!" ...
                  "not" ...
                  "empty" ...
                  "-" ...
                  <IDENTIFIER> ...
                  <NAMESPACE> ...



              But your first proposal first to parse the id to the bean sounds good.
              But how do I pass the id to the property of the bean and execute the editMethod in one step?


              Thx in advance


              • 4. Re: Edit Action in ContextMenu of DataTable
                lvdberg

                Hi,


                including the id between the curly bracktes was simply a wild guess, in case I missed some hidden posibility of which I wasn't aware of ;-)



                The other one is simply using the action attribute of the MenuItem.


                Leo


                • 5. Re: Edit Action in ContextMenu of DataTable
                  bemar

                  Leo van den Berg wrote on May 06, 2011 07:28:


                  Hi,

                  including the id between the curly bracktes was simply a wild guess, in case I missed some hidden posibility of which I wasn't aware of ;-)


                  The other one is simply using the action attribute of the MenuItem.

                  Leo




                  Hi Leo,


                  its partly working with a4j


                  <rich:menuItem ajaxSingle="true" event="oncontextmenu">
                                           <a:commandLink id="incidentEdit"
                                                action="#{incidentHandler.editIncident()}"
                                                value="#{messages.text_incidentedit}">
                                                <a:actionparam name="currentIncidentId"
                                                     assignTo="#{incidentHandler.currentIncidentId}"
                                                     value="{incidentIdIncident}" />
                                           </a:commandLink>
                                      </rich:menuItem>



                  The id property will be set and the editIncident Method will be called and the object will be loaded from the database.
                  But I was not redirected to the edit page.


                  I adapted the home.page.xml file as follows


                  <navigation
                            from-action="#{incidentHandler.editIncident()}">
                            <rule if-outcome="loaded">
                                 <redirect view-id="/IncidentEdit.xhtml" />
                            </rule>
                            <rule if-outcome="failed">
                                 <redirect view-id="/fail.xhtml" />
                            </rule>          
                       </navigation>



                  but nothing happens.


                  Do you have a idea what the problem could be?


                  Thx in advance


                  Ben

                  • 6. Re: Edit Action in ContextMenu of DataTable
                    bemar

                    Why do I can't delete a post?
                    Now here formatted ... ;-)



                    Leo van den Berg wrote on May 06, 2011 07:28:


                    Hi,

                    including the id between the curly bracktes was simply a wild guess, in case I missed some hidden posibility of which I wasn't aware of ;-)


                    The other one is simply using the action attribute of the MenuItem.

                    Leo




                    Hi Leo,


                    its partly working with a4j


                    <rich:menuItem ajaxSingle="true" event="oncontextmenu">
                         <a:commandLink id="incidentEdit"
                          action="#{incidentHandler.editIncident()}"
                          value="#{messages.text_incidentedit}">
                          <a:actionparam name="currentIncidentId"
                           assignTo="#{incidentHandler.currentIncidentId}"
                           value="{incidentIdIncident}" />
                         </a:commandLink>
                        </rich:menuItem>



                    The id property will be set and the editIncident Method will be called and the object will be loaded from the database.
                    But I was not redirected to the edit page.


                    I adapted the home.page.xml file as follows


                    <navigation
                      from-action="#{incidentHandler.editIncident()}">
                      <rule if-outcome="loaded">
                       <redirect view-id="/IncidentEdit.xhtml" />
                      </rule>
                      <rule if-outcome="failed">
                       <redirect view-id="/fail.xhtml" />
                      </rule>
                     </navigation>



                    but nothing happens.


                    Do you have a idea what the problem could be?


                    Thx in advance


                    Ben

                    • 7. Re: Edit Action in ContextMenu of DataTable
                      bemar

                      My fault. Navigation in wrong xml file ;-)


                      Its working now. Thank you very much, you made my day.


                      Nice weekend


                      Ben

                      • 8. Re: Edit Action in ContextMenu of DataTable
                        lvdberg

                        Hi,


                        great that it works now. An additional small remark: Be awara thet the navigation rule is a literal comparison, it will fail if you have additional or missing characters.


                        So if you use myBean.myMethod you shouldn't use myBean.myMethod() in the rule. I found out the hard way!


                        Have a nice weekend also,



                        Leo