9 Replies Latest reply on Mar 13, 2007 3:31 PM by app4you

    Rendering buttons in a datatable fails

      Hi all,

      I have a problem with the rendered attribute and buttons in a h:dataTable. I have my list of entries (as in the listing). If I click on the "Edit" button all is ok - I get the edit page.

      <h:form>
       <h:dataTable var="entry" value="#{entryList}" rendered="#{entryList.rowCount>0}">
       <h:column>
       <f:facet name="header">
       <h:outputText value="Title"/>
       </f:facet>
       <h:outputText value="#{entry.title}"/>
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:outputText value="Status"/>
       </f:facet>
       <h:outputText value="#{entry.status}"/>
       </h:column>
       <h:column>
       <h:commandButton value="Edit" action="#{entryAction.edit(entry)}" />
       </h:column>
       </h:dataTable>
       </h:form>
      


      Now I want to extend my table, that the button is only displayed if a status is set to a value allowing the editing of the entry.

      I tried to put the rendered attribute to the h:commandButton like

      <h:commandButton value="Edit" action="#{entryAction.edit(entry)}" rendered="#{entryAction.editable}"/>
      


      than only the allowed entries get a "Edit" button. But if I clicked on the button the page is rerendered and the corresponding action is not executed.

      After that I tried the s:button. Here the method is executed, but the parameter is null in my method.

      Has someone any hints or could help me solving this problem?

      Regards
      Marco



        • 1. Re: Rendering buttons in a datatable fails
          app4you

          Hello,

          You might want to check the followings:
          1. The search result actionBean should be in Session scope (can be conversation scope), or the search result list object has to be in session scope
          2. For the editable checking for each record, then put it @Transient isEditable in the entity bean level
          3. In the entryAction, use @Begin or @Begin(join=true) public ... edit(... entry)

          Hope this could help.

          • 2. Re: Rendering buttons in a datatable fails

            Thank you app4you for your answers.

            I checked your suggestions, but without any success.

            The DataModel was allready in a conversion scope.
            The editable check works showing only the entries which can be edited.
            The edit-Action is never called, so the @Begin can't start a conversion :-(

            But I played a little bit with the rendered attribute. I changed the editable check in the entryAction for test reason that it always returns true. Then it works as in the first example.

            Can the DataModel not dereference the selected entity if there is not a button in each row?

            Thanks for help
            Marco

            • 3. Re: Rendering buttons in a datatable fails

              1. Try to place <h:messages/> and see if it gives any errors.
              2. I am not sure whether h:commandButton supports arguments (try <s:button/>?)

              • 4. Re: Rendering buttons in a datatable fails

                Hi svadu,

                <h:messages /> was already placed. But no error is printed.

                I tried the <s:button> before, but without any changes in the behavior of the page.
                The <h:commandbutton> supports arguments. If you look in my initial post there is already an attribute pasing to the action bean. And this example works.

                • 5. Re: Rendering buttons in a datatable fails
                  app4you

                  Hello,

                  the h:commandButton does not support parameter passing. Use s:button or s:link for parameter passing.

                  If your stateful session List object contains the @DataModel, then it should work in the DataTable using <s:button or h:commandButton.

                  There got to be something missing. Please show your code snippet, xhtml and bean.

                  Thanks

                  • 6. Re: Rendering buttons in a datatable fails

                    Thank you so far.

                    Ok, here some code snippets.

                    The xhtml is similar as in the first post. I put only the rendered attribute on the commandButton:

                    <h:form>
                     <h:dataTable var="entry" value="#{entryList}" rendered="#{entryList.rowCount>0}">
                     <h:column>
                     <f:facet name="header">
                     <h:outputText value="Title"/>
                     </f:facet>
                     <h:outputText value="#{entry.title}"/>
                     </h:column>
                     <h:column>
                     <f:facet name="header">
                     <h:outputText value="Status"/>
                     </f:facet>
                     <h:outputText value="#{entry.status}"/>
                     </h:column>
                     <h:column>
                     <h:commandButton value="Edit" action="#{entryAction.edit(entry)}" rendered="#{entryAction.editable}"/>
                     </h:column>
                     </h:dataTable>
                     </h:form>
                    



                    Here is some code from the corresponding action bean:

                    @Stateful
                    @Name("entryAction")
                    @Scope(ScopeType.CONVERSATION)
                    
                    public class EntryActionBean implements EntryAction, Serializable {
                    
                     private Logger log = Logger.getLogger(EntryActionBean.class);
                    
                     @DataModel
                     private Set<Entry> entryList;
                    
                     @DataModelSelection
                     private Entry entry;
                    
                     @Factory("entryList")
                     public void initEntryList() {
                     log.info("initEntryList");
                     entryList = getEntryList();
                     }
                    
                     public boolean isEditable() {
                     return checkEditable(entry);
                     }
                    
                     public String edit(Entry entry) {
                     log.info("edit Entry " + entry);
                     return "editEntry";
                     }
                    
                     @Remove @Destroy
                     public void destroy() {
                     log.info("Called @Remove/@Destroy");
                     }
                    
                     ...
                    }
                    


                    I also tried out the s:button, but there is no parameter passing to the action bean. I get always a null value.

                    Hope that helps.

                    Marco

                    • 7. Re: Rendering buttons in a datatable fails
                      app4you

                      Here's wat to do to resolve it

                      1. Either make the datamodel list object into the session scope
                      2. or create another action bean to handle the editEntry action
                      3. Don't use h:commandButton with parameter. Don't know why you did n't receive the JSF expression error for doing this passing. USE <s:button> instead.

                      • 8. Re: Rendering buttons in a datatable fails

                        Thank you app4you,

                        So I played another time with my application and I think I resolved the problems with your help.

                        I couldn't put the DataModel in the session scope, because it is only allowed to put it in the page or undefined scope.

                        But I put an @Begin at my factory method. So the action bean stays in the conversions. That's ok for me.

                        Now I can pass parameter via the s:button (The commandButton works too !?!, but I change to the s:button).

                        I tried out creating a 2nd bean for the edit action. But it also works now if all is in the same bean.
                        I think, it must be the conversation.

                        So now the buttons are only shown on those entries, which are editable and the correct entry is passed if I click on "Edit"!

                        Thanks again
                        Marco

                        • 9. Re: Rendering buttons in a datatable fails
                          app4you

                          Yo're welcomed. Glad i could help.