JSF Primefaces set button enabled when table row is selected
Solution 1
One solution could be using
<p:ajax event="rowSelect" update=":deleteButton" listener="#{bean.someListener}" />
inside datatable.
This catches the row selection event, calls a listener and updates the button.
Now you could define the listener in the backing bean that just updates the value of a boolean instance variable, that reflects the disabled/enabled status of the button in the view:
<p:commandButton id="deleteButton" value="Delete" disabled="#{bean.selectedBoolean}" />
You can take a look at primefaces showcase for a similar scenario:
http://www.primefaces.org/showcase/ui/datatableRowSelectionInstant.jsf
Hope this helps.
Solution 2
Thanks for jsfviky71 ! I write:
<h:form id="form">
<p:dataTable value="#{bean.patients}" var="item"
selectionMode="single" rowKey="#{item.id}"
selection="#{bean.selected}" >
<p:ajax event="rowSelect" update=":form:deleteButton" listener="#{bean.onRowSelect}" />
// data in rows
</p:dataTable>
<p:commandButton id="deleteButton" value="Delete" disabled="#{bean.disabled}"/>
And in my bean:
private Boolean disabled = true;
// getter and setter
public void onRowSelect(SelectEvent event) {
disabled = false;
}
Hope this will help to others
Oleksandr H
Updated on June 14, 2022Comments
-
Oleksandr H almost 2 years
I have a list of users in a table and with disabled Delete button. I need to enable the Delete button when I select the row in the table. How can I do this?
<p:dataTable value="#{userBean.patients}" var="item" selectionMode="single" rowKey="#{item.id}" selection="#{userBean.selected}" onRowSelected="deleteButton.disabled='false';"> // HOW TO WRITE THIS EVENT CORRECTLY????? // columns </p:dataTable> //This button must be enable after I click on any table row <p:commandButton id="deleteButton" value="Delete" disabled="true" />
Maybe, I need to use onRowClick event. I dont know the name of this event
-
Rong Nguyen almost 11 yearsProvide your code you have tried. When row was selected, you have to set
enable=true
andupdate
delete button. -
BalusC almost 11 yearsThere are several ways to select a row in a PF datatable. Please tell/show how you're doing that. Only then we can tell how to hook a listener on that way.
-
Oleksandr H almost 11 yearsI posted code in the topic
-