Save all rows in an h:dataTable
I have a Facelets page with a h:dataTable.
In each row of the h:dataTable
, i am displaying some enab开发者_开发知识库led and disabled services of a user.Here is the model object
public class ServiceList {
private long userId;
private long serviceGroupId;
private String serviceGroupName;
private long serviceId;
private String serviceName;
private String serviceUrl;
private String serviceState;
public UserServiceList() {
}
//getters and setters....
}
These are the details i am displaying in a single row of a dataTable. serviceState in the above model object is either 'Y' or 'N'.
my problem is the application user should be able to update the servicestate of all rows of a dataTable at once and update them in the backend database.
1)what additional JSF component do i need to use inside dataTable to achive this? I am thinking of adding one more column with h:selectOneradio
2)How do i get which rows are selected and what status they have set?
I am kind of newbee to JSF.Please help.
Update:At present what i am having is two buttons namely 'Disable Service' and 'Enable Service' in the footer section of the table.
Onclick of Disable Service i am navigating to another page where i show the application user the list of enabled services to disable
And vice-versa for Enabled service button click.
So, let's say you in your Managed Bean you have a list of services you would like the user to edit:
List<Service> serviceList;
You take this List
to be displayed in the data table.
<h:dataTable value="#{yourManagedBean.serviceList}" ... >
Then you can implement a commandButton
that has either an action
or an actionListener
which points to a certain method of your managed bean, like this:
<h:commandButton action="#{yourManagedBean.saveAllAction}" ... >
And the corresponding method to save 'em all is quite straight-forward. You iterate over the managed bean field serviceList
and persist every single entry (however you persist them, like calling the EntityManager
when using Hibernate or any DAO class in between, you name it.)
Concerning the service status: I'd preferably use a selectBooleanCheckbox
for toggling the status, since it's probably a boolean value.
Edit after comment 1:
You have the serviceStatus
in your Service
class. Currently it's a string, but I suppose it should be boolean to toggle active/inactive. If this property is displayed by the selectBooleanCheckbox
it is automatically changed in your corresponding Java class. So calling getServiceStatus()
returns true
or false
, depending on what is selected in the frontend. If you persist the whole Service
object then, you don't have to do anything because any modifications made in the frontend HTML elements are automatically projected to the Java object behind it.
精彩评论