Delete the selected row from datatable

UI Components for JSF
Post Reply
draco
Posts: 18
Joined: 20 Sep 2010, 17:24

21 Nov 2010, 18:41

Hi,

Sorry for a question that I should already know the answer to, but how do I go about deleting the selected row from the datatable? I would have assumed that I would need to get the index of the selected row, and then remove the data from the list by that index (myList.remove(index)). I can't seem to find out how to get the index of the selected row...

Thanks,
B
PrimeFaces-3.2, Glasfish 3.1, JSF 2.0, IDEA 11, Mojarra 2.0.3

draco
Posts: 18
Joined: 20 Sep 2010, 17:24

22 Nov 2010, 17:32

Anyone?

Because I have

Code: Select all

<p:datatable selection="#{myFace.selectedRow}">
I can do the following:

Code: Select all

public void onDeleteClicked(ActionEvent event)
{
if (selectedRow != null)
{
  selectedRow = null;
  myList.remove(selectedRow);
}
}
That might work if myList is an java.util.ArrayList. Does anyone know how I can get the selectedRowIndex?
PrimeFaces-3.2, Glasfish 3.1, JSF 2.0, IDEA 11, Mojarra 2.0.3

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

22 Nov 2010, 21:11

You might consider taking advantage of a method expression to pass the model object representing a row from the view to the server. It would look something like:

Code: Select all

<h:form>
<p:dataTable var="myEntity" value="#{myBean.entities}">
...
    <p:column>
        <p:commandLink ajax="true" action="#{myBean.removeEntity(myEntity)}" update="@form growl" process="@this">
            <span title="Remove" class="ui-icon ui-icon-trash"/>
        </p:commandLink>
    </p:column>
</p:dataTable>
</h:form>
<p:growl/>

daniel.camargo
Posts: 24
Joined: 22 Oct 2010, 20:00

22 Nov 2010, 21:40

This works fine for me:

Code: Select all

<p:column headerText="Options">
	<p:rowEditor />
	<p:commandLink
		action="#{mBean.action}"
		value=" " styleClass="ui-icon ui-icon-trash"
		update="@this" ajax="true">
		<f:setPropertyActionListener
			target="#{tabelaOutrasDespesasMBeanForm.selectedItem}"
			value="#{item}" />
	</p:commandLink>
</p:column>
PS:#{item} is the datatable var.
JSF2 - Primefaces-3.1.RC1

draco
Posts: 18
Joined: 20 Sep 2010, 17:24

23 Nov 2010, 11:35

Hi guys,

Thank you very much for your replies. I do understand what you mean. But in my case I don't want a column specifically for deleting the row, but instead, have a footer with a delete button on it, that is enabled when a row is selected.

Example: http://www.primefaces.org/showcase/ui/d ... Single.jsf

Imagine the "View" button was actually a "Delete" button, and imagine that I want to pass in the rowIndex for that selected row, rather than the object that represents that row. The reason I want the rowIndex instead of the selectedRow is because I want to do the following:

Code: Select all

public void onDeleteClicked(ActionEvent event)
{
  // myList is the list that the datatable uses to render the rows on the page.
  myList.remove(rowIndex);
}
I don't want to do what you have done in another thread bumble.bee (http://primefaces.prime.com.tr/forum/vi ... f=3&t=6176) whereby you access the database and delete a row from it with an entityManager. I am not working with entities in my scenario.

Again thank you very much for your replies
PrimeFaces-3.2, Glasfish 3.1, JSF 2.0, IDEA 11, Mojarra 2.0.3

daniel.camargo
Posts: 24
Joined: 22 Oct 2010, 20:00

23 Nov 2010, 13:54

The problem seems to be how to send the right index during the row selection event.

The only solution I came up with is involving an ugly "work around" (too ugly to be posted here), and I believe that others can present a better solution.
JSF2 - Primefaces-3.1.RC1

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

23 Nov 2010, 15:26

You might try taking a look at the source:

http://code.google.com/p/primefaces/sou ... elper.java

It looks like the server pulls out the selected row index using the request param name: table clientId + "_selection".

You might also try "binding" a datatable component.

blake.neal
Posts: 30
Joined: 23 May 2011, 22:03

15 Jun 2011, 23:06

@daniel.camargo

How did you get that to work? is there some weird stack order that fires in just the right way to get consistent results? for some reason, I can't get the setPropertyActionListener to do its job even though I see it implemented in various primefaces components...

Hope this reaches you. Thanks for any info you can provide.
Primefaces 4.0 // Majorra 2.1.24 // Tomcat 7.x

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 69 guests