简体   繁体   中英

h:selectBooleanCheckBox action on select

I have a <h:selectBooleanCheckBox> as part part of my JSF which I want to run a bean method when it's state has changed from unchecked to checked.

I have the following controller bean

@Named
@ViewScoped
public class UserController {

   @Inject
   private UserService userService;

   @Inject
   private LocationService locationService;

   private UserFilter userFilter;

   private List<User> users;
   private List<Location> locations;

   @PostConstruct
   public void init() {
       users = userService.listAll();
       locations = locationService.listAll();
       userFilter = new UserFilter();
   }

   public List<User> getUsers() {
       return users;
   }

   public void setUsers(List<User> users) {
       this.users = users;
   }

   public List<Location> getLocations() {
       return locations;
   }

   public void setLocations(List<Location> locations) {
       this.locations = locations;
   }

   public void listAllUsers() {
       users = userService.listAll();
   }

   public void findUsers() {
      // code that uses the UserFilter
      // to decide which user filter find method to use
   }
}

The UserFilter is a simple DTO

public class UserFilter {

    private boolean allUsers = true;

    private String username;
    private String location;

    //getters and setters
}

And my JSF has is like so

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Users</title>
    </h:head>
    <h:body>
        <h1>Users</h1>
        <h:form id="filterForm">
            <h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers">
                <f:ajax render="filterGrid"/>
            </h:selectBooleanCheckbox><h:outputText value ="All users"/>
            <h:panelGrid id="filterGrid" columns="3">
                <h:inputText id="userName" value="#{userController.userFilter.userName}" disabled="#{userController.userFilter.allUsers}"/>
                <h:selectOneMenu id="selectLocation" value="#{userController.userFilter.location}" disabled="#{userController.userFilter.allUsers}">
                    <f:selectItems value="#{userController.locations}" var="location" itemValue="#{location.location}" itemLabel="#{location.location}"/>
                </h:selectOneMenu>
                <h:commandButton value="Filter" disabled="#{userController.userFilter.allUsers}" action="#{userController.findUsers()}"/>
            </h:panelGrid>
        </h:form>
        <h:form rendered="#{not empty userController.users}">
            <h:dataTable value="#{userController.users}" var="user">
                <h:column>#{user.name}</h:column>
                <h:column>#{user.location.location}</h:column>
                <h:column><h:commandButton value="delete" action="#{userController.delete(user)}"/></h:column>
            </h:dataTable>
        </h:form>
        <h:panelGroup rendered="#{empty userController.users}">
            <p>Table is empty! Please add new items.</p>
        </h:panelGroup>
        <h3>Add user</h3>
        <h:form id="user">
            <p>Value: <h:inputText id="name" /></p>
            <p>
                <h:commandButton value="add" action="#{userController.add(param['user:name'])}"/>
            </p>
        </h:form>
    </h:body>
</html>

As you can see by default it lists all users, then when the checkbox is unchecked you have the option to filter on username/location.

What I want is for the check box to run the userController.listAllUsers() method when it's state moves from unchecked to checked.

And a small additional question, how do I get the checkbox to appear in the same row as the panel grid items?

I have a habit of answering my own questions it seems! I needed an additional <f:ajax tag that rendered the user form and had the listener attribute set

So something like

<h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers">
    <f:ajax render="filterGrid"/>
    <f:ajax render="usersForm" listener="#{userController.listAllUsers()}"/>
</h:selectBooleanCheckbox><h:outputText value ="All users"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM