Implement the check box selection function to implement the check box function
Cold weather in April 9, 2015
Here, "user authorization" is used as an example to describe
Implementation Method 1 (suitable for modal pop-up scenarios): the background code is as follows:
/*** User authorization **/public String authorization () throws Exception {// 0 indicates the authorization request page, 1 indicates that data needs to be updated to the database if (getAction (). equals ("0") {user = userService. get (userId); // get all permissions of the user allRoleList = roleService. getAllRole (); // Set <Role> userRoleList = user. getRoles (); // loop all permissions. if the user has been selected, the tag will be marked. The foreground will display the hook outer: for (Role userRole: userRoleList) {inner: for (Role allRole: allRoleList) {if (userRole. getRoleId (). equals (allRole. getRoleId () {allRole. setIsCheck (1); break inner ;}} return ROLE ;}else {userService. authorization (userId, roles); setPageData (); return SUCCESS ;}}
In general, this code compares the existing permissions of a user with all permissions of the user, the existing permissions are marked with isCheck (1) in all the permissions of the user, so you can check the selected action through the following code at the front end of the interface.
The front-end code is as follows:
<! -- If the user has been granted the permission, the permission should be checked in --> <c: forEach items = "$ {requestScope. allRoleList} "var =" role "> <c: choose> <c: when test =" $ {role. isCheck = 1} "> <input type =" checkbox "name =" roles "value =" $ {role. roleId} "checked =" checked ">$ {role. roleName} </c: when> <c: otherwise> <input type = "checkbox" name = "roles" value = "$ {role. roleId} ">$ {role. roleName} </c: otherwise> </c: choose> </c: forEach>
Note that when you need to put an object and ArrayList in json, you can put this object and ArrayList in a Map, and Map the same output.
Of course, some colleagues say that I am not marking in the background. Can I do it directly at the front end. Of course you can, so we have the second approach.
Method 2 ):
<c:choose> <c:when test="${not empty customerIdsMap}"> <input type="checkbox" name="ids" value="${proposer.id}" <c:forEach items="${customerIdsMap}" var="cid"> <c:if test="${cid == proposer.id}"> checked="checked" </c:if> </c:forEach> />${proposer.trueName} </c:when> <c:otherwise> <input type="checkbox" name="ids" value="${proposer.id}" />${proposer.trueName} </c:otherwise></c:choose>
However, this implementation requires that the customerIdsMap and proposer objects be returned in the background respectively,These two objects must be in the same domain.For example, Json and request cannot be cross-mixed. Because the objects in the request are easy to refresh and lost.