aspose file tools*
The moose likes JSF and the fly likes JSF Delete datatableRow on Button Click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF Delete datatableRow on Button Click" Watch "JSF Delete datatableRow on Button Click" New topic
Author

JSF Delete datatableRow on Button Click

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

I have some issues in deleting a row on click of a button. I have a datatable, which contains 5 columns. The fifth column is a button. I have a requirement, where, when I click on the button, the row data corresponding to the button needs to be deleted. Im trying to populate the column values using a datamodel. Below is the code that I have written to acheive the row deletion. But the row is not getting deleted.



The code for the listener method is specified below



Im using the adminVODataModel to populate the datatable. Im trying to remove the corresponding row using the above code. But the row is not getting deleted.

Please Assist. Thanks in Advance
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
There's a much easier way to do it, you'll be pleased to here.

<h:commandButton id="buttonID1" image="/images//delete.png" style="width:20px" action="#{bean.deleteMethod}">
<f:setPropertyActionListener value="#{var}" target="#{bean.selectedThing}"/>
<p:ajax/>
</h:commandButton>

so just use setProperty... if the dataTable is var="var" and it's displaying a List<Something> then in the bean you
have private Something selectedThing; You could always use p:commandButton although I don't remember if it has an
image attribute (it has icon=), or a p:commandLink with a nested h:graphicImage and lose the child p:ajax tag.

You didn't show what the dataTable value attribute is but I hope this is valid.

Regards,
Brendan.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16240
    
  21

Actually, there should be an option on the "p:ajax" tag that allows you to tell JSF to re-render the updated table view after the AJAX listener method is executed. I don't know what it is in IceFaces, but in RichFaces, it's simply "reRender="xxx"", where "xxx" is the simple ID of the datatable as indicated by its "id=" attribute. I think if you use the JSF2 "f:ajax", it's simply called "render=".

No PropertyActionListener is required.


Customer surveys are for companies who didn't pay proper attention to begin with.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
The problem is that the action method won't know what row is being deleted without the setPropertyActionListener.
I don't know if there's anything new in PF3 to do this but it's how they do it in the showcase.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16240
    
  21

Brendan Healey wrote:The problem is that the action method won't know what row is being deleted without the setPropertyActionListener.
I don't know if there's anything new in PF3 to do this but it's how they do it in the showcase.


If that's really true, I'd recommend dropping the PrimeFaces AJAX and converting to JSF2 AJAX, because it not only doesn't need the listener, it's more in line with non-AJAX programming, where when an action is fired: the table's DataModel object has its rowData and rowIndex values pointing to the selected row on entry to the action (or AJAX listener) method, which is what Varshini's AJAX code is expecting.
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

Thanks Tim/ Brendan for the response.

Tim, as you suggested, I changed the ajax call. I used f:ajax and included the render attribute. I have changed the code and below is the updated one.



The code corresponding to the action listener is



When I click the delete button for the first time, the corresponding row get deleted and it renders the updated table in the view (this works fine only in ecllipse browser. The table updation is not happening when i try to run the same in the chrome/IE browser). Upon consecutive clicks, nothing works. The actionlistener method is getting invoked, in all the cases. This Im able to confirm since, Im included some sysout statements in the actionlistener method and that gets printed each time when I click the delete button.
Help on this is much appreciated. Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16240
    
  21

I don't know why there should be a per-browser difference, but it appears that you are attempting to remove the same row twice - once by its index (getRowIndex) and once by its value (getRowData).
 
jQuery in Action, 2nd edition
 
subject: JSF Delete datatableRow on Button Click