0

I have a Facelets page with a h:dataTable. In each row of the h:dataTable, i am displaying some enabled 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.

Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
Sreeram
  • 3,160
  • 6
  • 33
  • 44

1 Answers1

1

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.

Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • Thanks for the response Sebastian.Can you be more clear about storing the changed row's id's? – Sreeram Sep 20 '11 at 08:13
  • Yes, I updated my question. This should help. Otherwise I'd recommend reading some stuff about how Java/JSF works in this direction. You have the basic structure of how you do this - saving the Service depends on your environment, so I can only explain generically. – Sebastian Wramba Sep 20 '11 at 08:19
  • Thanks once again Sebastian.I solved the storing selected rows from this link http://stackoverflow.com/questions/2524514/how-to-use-jsfs-hselectbooleancheckbox-with-hdatatable-to-create-one-object-pe[link] – Sreeram Sep 20 '11 at 10:07
  • I used selectoneRadio to display the user with options to disable or Enable.I linked it to serviceStatus in the Service class as you said. That Solved my problem.Now i am able to get which row is selected and the status which user selects.Thanks for the help. – Sreeram Sep 20 '11 at 10:12