實現複選框選擇的功能,實現複選框功能

來源:互聯網
上載者:User

實現複選框選擇的功能,實現複選框功能

2015年4月9日 天氣冷
這裡以“使用者授權”為例說明

實現方法一(適合彈出modal的情境):後台代碼如下:
/** * 使用者授權 * */public String authorization() throws Exception {    //action為0代表請求授權頁面,action為1代表需要更新資料到資料庫    if (getAction().equals("0")) {        user = userService.get(userId);        //獲得使用者所有許可權        allRoleList = roleService.getAllRole();        //獲得使用者已有許可權        Set<Role> userRoleList = user.getRoles();        //迴圈所有許可權,如果使用者已經選中,則標記,前台顯示打鉤        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;    }}

這段代碼的大體意思是:在使用者所有許可權中,通過使用者已有的許可權與使用者所有許可權比較,將已有的許可權在使用者所有許可權中做了一個標記isCheck(1),所以在介面前端就可以通過如下的代碼實現勾選的動作了

前端代碼如下:
<!-- 如果使用者已經賦予許可權,進來的時候許可權要勾上 --><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>

需要注意的地方是遇到json中要放一個對象及ArrayList的時候,可以把該對象及ArrayList放到一個Map中,由Map同一輸出
當然有的同學會說,我不在後台在標記,直接在前端做可以嗎。那當然可以,於是就有了第二種做法

實現方法二(適合於從一個跳轉到另一個介面的情境,包括dialog中頁面的跳轉):
<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>

不過這個實現需要在後台分別返回customerIdsMap及proposer的對象,這兩個對象一定要在同一域中,比如Json和request交叉混合就不行。因為request中的對象容易重新整理丟失。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.