Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Primefaces set button enabled when table row is selected

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

like image 708
Oleksandr H Avatar asked Dec 07 '25 23:12

Oleksandr H


1 Answers

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

like image 127
Oleksandr H Avatar answered Dec 10 '25 12:12

Oleksandr H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!