Use DataModel#getRowData() to obtain the current row in action method.
@ManagedBean
@ViewScoped
public class Usermanager {
private List doctors;
private DataModel doctorModel;
@PostConstruct
public void init() {
doctors = getItSomehow();
doctorModel = new ListDataModel(doctors);
}
public void followDoctor() {
Doctor selectedDoctor = doctorModel.getRowData();
// ...
}
// ...
}
Use it in the datatable instead.
And get rid of that h:inputHidden
next to the h:commandButton
in the view.
An -less elegant- alternative is to use f:setPropertyActionListener
.
public class Usermanager {
private Long doctorId;
public void followDoctor() {
Doctor selectedDoctor = getItSomehowBy(doctorId);
// ...
}
// ...
}
With the following button:
Related:
- The benefits and pitfalls of @ViewScoped - Contains CRUD example using
DataModel
.