5 Replies Latest reply on Apr 21, 2008 7:09 PM by roger phillips

    Delete selected row from DataModel

    roger phillips Newbie

      I am using an ArrayList as the source for a DataModel. It works fine for display, and edit, but I get an error when I try to delete the selected row.


      The backing bean is a stateful session bean. Here are some code snippets:



         @DataModel
          private List<Owner> owners = new ArrayList<Owner>();
          
          public List<Owner> getOwners() { return owners; }
      
      ...
      
          public void edit(Owner ownr)
          {
           System.out.println("owner name = " + ownr.getFirstName());
           ownerEdit = "true";
      
                
           // create new local object - allow cancel
           owner = new Owner(ownr.getId(), ownr.getFirstName(), ...
          }
           
          public void delete()
          {
           System.out.println("owner id = " + ownerIndex);
           owners.remove(findOwner(ownerIndex));
          }
      



      Here's the xhtml code:



         <rich:dataTable id="owners" value="#{owners}" var="ownr"
                      rendered="#{not empty owners}">
                   
               <h:column>
                 <f:facet name="header">First Name</f:facet>
                            #{ownr.firstName}
               </h:column>
                                          
      ...       
                        
           <h:column>
                 <s:link action="#{createAccount.edit(ownr)}" value="Edit" />
              </h:column>
                        
           <h:column>
              <s:link action="#{createAccount.delete}" value="Delete"
                                  rendered="#{ownr.id != 1}">
                                 <f:param name="ownerId" value="#{ownr.id}"/>
              </s:link>
              </h:column>
      
           </rich:dataTable>
      



      The error I get when I remove the selected object from the arrayList is:



      Caused by: java.lang.IllegalArgumentException
           at javax.faces.model.ListDataModel.getRowData(ListDataModel.java:139)
           at org.jboss.seam.databinding.DataModelBinder.getSelection(DataModelBinder.java:69)
      


      Thanks




































        • 1. Re: Delete selected row from DataModel
          Nicklas Karlsson Master

          Could there be something with the fact that there is no submit (s:link)? Try a commandLink or something submitting...

          • 2. Re: Delete selected row from DataModel
            Daniel Roth Apprentice

            I do it this way. Works fine for me


            <rich:dataTable 
             value="#{regionList}" var="region" id="products" rows="10" width="100%">
                 <h:column>
                      <f:facet name="header">
                           <h:outputText value="#{messages['regions.account']}" styleClass="text" />
                      </f:facet>
                      <h:outputText value="#{region.account}" styleClass="text" />
                 </h:column>
                 <h:column>
                      <f:facet name="header">
                           <h:outputText value="#{messages['regions.delete']}" styleClass="text" />
                      </f:facet>
                      <s:button action="#{adminRegions.delete(region)}" value="#{messages['regions.delete']}" disabled="#{region.deleted}" styleClass="text" />
                 </h:column>
                 <h:column>
                      <f:facet name="header">
                           <h:outputText value="#{messages['regions.undelete']}" styleClass="text" />
                      </f:facet>
                      <s:button action="#{adminRegions.undelete(region)}" value="#{messages['regions.undelete'] }" disabled="#{!region.deleted}" styleClass="text" />
                 </h:column>
            </rich:dataTable>
            




                @DataModel
                private List<Region> regionList;
            
                @In
                private EntityManager em;
            
                @Factory("regionList")
                public void findRegions() {
                    regionList = em.createQuery("SELECT o FROM Region o ORDER by o.id desc").getResultList();
                }
            
            
                public void delete(Region region) {
                    region.setDeleted(true);
                    em.persist(region);
                }
                
                public void undelete(Region region) {
                    region.setDeleted(false);
                    em.persist(region);
                }    
            
            




            Hopefully I didn't cut out anything essential :-)

            • 3. Re: Delete selected row from DataModel
              roger phillips Newbie

              Thanks for the response. The difference between your example and what I'm doing is that I'm physically removing an object from an ArrayList in the delete method rather than a soft delete. The stack error indicates an illegal argument error trying to fetch rowData from the ListDataModel wrapper.


              All the examples I've see use entity queries (resultList) with DataModel - no ArrayList.


              • 4. Re: Delete selected row from DataModel
                roger phillips Newbie

                Thanks for your response. The s:link is working fine and returning the selected object id using @DataModelSelectonIndex. I've also returned the object itself successfully using both delete(ownr) and @DataModelSelection.


                I think I'm dealing with an ArrayList issue.

                • 5. Re: Delete selected row from DataModel
                  roger phillips Newbie

                  The work-around is to create a new ArrayList in the delete method. I'm runnng Seam 1.2.1 and this is apparently fixed in Seam 2.0.