14 Replies Latest reply on Dec 12, 2008 10:43 AM by luke.poldo.mailinator.com

    Delete row in datatable

    swathis

      Hi All,
      Can anybody tell me if there is any way to delete the selected rows in datatable using check boxes. I have an entity which has a boolean field, and I can retrieve the data in datatable and the first column contains check boxes


      <rich:dataTable id="contentDetails" value="#contentDetails.resultList}" 
      var="content"rendered="#{not empty contentDetails.resultList}">
                                    
      <rich:column >
           <f:facet name="header">Select</f:facet>
               <h:selectBooleanCheckbox value="#contentListHome.instance.selected}">           
                 </h:selectBooleanCheckbox>
      </rich:column>
      <rich:column >
           <f:facet name="header">Value</f:facet>
              #{content.Value}
      </rich:column>
      <rich:column >
           <f:facet name="header">Name</f:facet>
                #{content.Name}
      </rich:column>
      </rich:dataTable>
      <h:commandButton id="Delete" value="Delete"
                action="#{contentListHome.remove}" 
      rendered="#{contentListHome.managed}"/>



      I could not map the check box to the delete button.
      Any suggestions .......


      Thankyou
      Swathi


        • 1. Re: Delete row in datatable
          cosmo

          Try using the variable from the dataTable in the h:selectBooleanCheckbox value.



          <h:selectBooleanCheckbox value=#{content.selected} />



          • 2. Re: Delete row in datatable
            swathis

            Thank you Aldo Bonzi,I tried it but not working :( , my action class does not contain any code in the remove method.(except overriding the super method remove. What I actually want my code to do is..... When the data is retrieved from database to datatable I want to delete the rows in the data table by selecting the rows using checkbox's .)



            @Override
            @End
            public String remove() {
              try {
                 super.remove();
                 facesMessages.add("Successfully deleted Content ");
                  } catch (Exception e) {
                 facesMessages.add("Deleting Content  Failed");
                 return null;
                  }
                      return "done";
            }



            I think that is the reason why my check box is not getting mapped to values which I want to delete.
            Any suggestions please....

            • 3. Re: Delete row in datatable
              mail.micke

              Hi


              The EL for the boolean check box is wrong, missing a {.


              How is it you determine which element to remove?


              After a quick glance at your page code it only looks like you are setting a boolean variable without any information regarding which row it belongs to. Curious about your logic in the super class.


              Micke

              • 4. Re: Delete row in datatable
                swathis

                Hi Mikael,
                Actually it was typing mistake in the post... my actual code contains


                <rich:dataTable id="contentDetails"
                 value="#{contentDetails.resultList}" 
                var="content"rendered="#{not empty contentDetails.resultList}">
                                              
                <rich:column >
                     <f:facet name="header">Select</f:facet>
                         <h:selectBooleanCheckbox
                          value="#{contentListHome.instance.selected}">           
                           </h:selectBooleanCheckbox>
                </rich:column>
                <rich:column >
                     <f:facet name="header">Value</f:facet>
                        #{content.Value}
                </rich:column>
                <rich:column >
                     <f:facet name="header">Name</f:facet>
                          #{content.Name}
                </rich:column>
                </rich:dataTable>
                <h:commandButton id="Delete" value="Delete"
                          action="#{contentListHome.remove}" 
                rendered="#{contentListHome.managed}"/>


                • 5. Re: Delete row in datatable
                  swathis

                  And Mikael coming to my action class


                  @Name("contentListHome")
                  public class ContentListHome extends EntityHome<Content> implements     Serializable {
                  
                  @In
                  EntityManager entityManager;
                  
                  @In(scope=ScopeType.SESSION, required=false)
                  @Out(scope=ScopeType.SESSION, required=false)
                  Long categoryIdConversation;
                  
                  @In(create=true)
                  Content content;
                  
                  @Override
                  @Begin(join = true)
                  public void create() {
                       super.create();
                  }
                  
                  @Override
                  @End
                  public String persist() {
                     List result=(List) entityManager.createQuery("from Content content    where value=:currentValue")          .setParameter("currentValue",content.getValue()).getResultList();
                    if ((result == null) || (result.size() == 0)) {
                    log.info("categoryIdConversation is"+categoryIdConversation);
                    getInstance().setContentCategoryID(getCategoryIdConversation());
                    getInstance().setCreatedBy(identity.getUsername());
                    getInstance().setCreatedOn(cal.getTime());
                    log.info("(BEFORE PERSIST)id is:"+getInstance().getContentCategoryID());
                    super.persist();
                    return "done";
                  }
                  else{
                       facesMessages.add(
                       "content : #{content.value} already existed");
                       return null;
                     }
                  }
                  
                  @Override
                  public Object getId() {
                  if (id == null) {
                       return super.getId();
                  } else {
                       return id;
                       }
                  }
                  
                  @Override
                  @End
                  public String remove() {
                       try {
                            super.remove();
                            log.info("Successfully deleted");
                            facesMessages.add("Successfully deleted Content ");
                            } catch (Exception e) {
                            facesMessages.add("Deleting Content  Failed");
                            return null;
                            }
                  
                            return "done";
                       }
                  }



                  I am overriding super class methods here......
                  Can you please tell me where did I do mistake, What should be my code in remove method.......any suggestions please.......

                  • 6. Re: Delete row in datatable
                    mail.micke

                    Hi


                    Afraid I can't help you much, don't use the EntityHome stuff. Only really played with the JPA stuff.


                    Seems like super.persist() saves the instance variable. So a guess would be that in remove() you need to set it to what should be removed. But that is only a guess.


                    Think you'll have to wait for someone else to chip in, or take a look at the examples and figure out how to do this.


                    Another thing:


                    I still don't understand how you can figure out which item has been selected via #{contentListHome.instance.selected}, are you sure you shouldn't assign the boolean property of #{content} (assuming that is the same class as instance ?


                    Then you could iterate over your list objects and remove all the ones with their selected property set to true.


                    Cheers,
                    micke

                    • 7. Re: Delete row in datatable
                      swathis

                      Hey Mikael,
                      I have an entity with field boolean,


                      @Name("content")
                      @Entity
                      public class Content implements Serializable {
                           private Long id;
                              private String value;
                           private String name;
                           private boolean selected;
                      .............
                      getters & setters
                      }
                           


                      I have pointed the selected field to the checkbox



                      • 8. Re: Delete row in datatable
                        dstas

                        hi,


                        maybe this can help you... i am new here... so if it wrong... i apologize


                        http://seamframework.org/Community/PassingInstanceToHomeClass

                        • 9. Re: Delete row in datatable
                          mail.micke

                          Did you try the suggestion of setting the the selected property of the iteration variable ( #{content.selected})?


                          I've implemented remove-via-checkbox that way (not using JPA).


                          In my remove method I simply iterate over the list of objects being displayed on the screen and remove those which have their selected property equal to true.


                          Not sure how this multiple remove solution fits with the EntityHome approach, my guess is that there might be a proper way of doing it. Then again it might not :)


                          • 10. Re: Delete row in datatable
                            swathis

                            Mikael If you don't mind can I have a look at your code that how did you iterate over the list of objects being displayed on the screen and removed those which have their selected property equal to true.
                            I just want to have some idea how did you do it....


                            Thanks in advance....


                            Swathi.

                            • 11. Re: Delete row in datatable
                              mail.micke

                              In my case it was very simple, something like this:



                              public class MyBacking{
                              
                              
                              @In
                              MyObjDao myObjDao;
                              
                              private List<MyObj> data;
                              
                              @Create initData(){
                              data = myObjDao.getAllData();
                              }
                              
                              public String remove(){
                              
                              for(MyObj mo: data){
                                 if(mo.isSelected() ){
                                   myObjDao.remove(mo);
                                 }
                              }
                              
                              //Reload the data
                              initData();
                              }
                              
                              }
                              



                              Just made that code up, but that is basically how I did it. Alternatively you could use an Iterator and remove the objects from the list instead of re-fetching them from the databse.


                              Hope it helps,
                              Micke

                              • 12. Re: Delete row in datatable
                                luke.poldo.mailinator.com

                                I hide the row in a dataTable with Javascript, I hope this can help you


                                <s:selectItems value="#{userList.userList}" var="listaUsers" label="#{listaUsers.name}" />
                                
                                <a4j:support oncomplete="new Effect.Fade(this.up('tr'));" event="onviewactivated" ajaxSingle="true" action="#{documento.setAssegnatoDa(identity.getUser())}" reRender="assegnatoDa"/>



                                Prototype it's powerfull, more that Richfaces let you think :)

                                • 13. Re: Delete row in datatable
                                  karan

                                  hey Luke simon can u explain your code in brief I didn't understand what do you mean by hiding the row in data table


                                  Karan

                                  • 14. Re: Delete row in datatable
                                    luke.poldo.mailinator.com
                                    <rich:dataTable id="contentDetails" value="#contentDetails.resultList}" 
                                    var="content"rendered="#{not empty contentDetails.resultList}">
                                                                  
                                    <rich:column >
                                         <f:facet name="header">Select</f:facet>
                                             <h:selectBooleanCheckbox value="#{contentListHome.instance.selected}" />
                                                <a4j:support oncomplete="new Effect.Fade(this.up('tr'));" event="onchange" ajaxSingle="true" action="#{contentListHome.remove(contentListHome.instance.id)}" />
                                    
                                    </rich:column>
                                    <rich:column >
                                         <f:facet name="header">Value</f:facet>
                                            #{content.Value}
                                    </rich:column>
                                    <rich:column >
                                         <f:facet name="header">Name</f:facet>
                                              #{content.Name}
                                    </rich:column>
                                    </rich:dataTable>
                                    <h:commandButton id="Delete" value="Delete" action="#{contentListHome.remove}" rendered="#{contentListHome.managed}"/>
                                    



                                    Ok, I didn't test this, but it should work (uhm...it's impossible to tell 'it works' in Seam).
                                    It's simple i do an ajax call to delete the item from db and I delete the row with scriptaculous.


                                    oncomplete="new Effect.Fade(this.up('tr'));"



                                    this take the table row tr, and fade out...