I have a databale on index.xhtml
What you can do is to use the [getRowData()][1] method on the Java bean to directly get the object located on the line that hosts the button on which the user clicked.
An example of code:
public void editBook(ActionEvent evt) {
// We get the table object
HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource());
// We get the object on the selected line.
Object o = table.getRowData();
// Eventually, if you need the index of the line, simply do:
int index = table.getRowIndex();
// ...
}
// Method to get the HtmlDataTable.
private HtmlDataTable getParentDatatable(UIComponent compo) {
if (compo == null) {
return null;
}
if (compo instanceof HtmlDataTable) {
return (HtmlDataTable) compo;
}
return getParentDataTable(compo.getParent());
}
The JSF code now looks like:
In addition, do not forget to change the signature of editBook() method, by setting a javax.faces.event.ActionEvent argument.