選擇用戶是很常見的功能,可以拆成兩個要點:顯示已選項、保存已選項。本文先談顯示。
因為是彈出視窗,內容主要走異步取得。而畫面其實分成兩塊資料——待選項與已選項——所以有兩種處理方式:
方法一:後端依資料一次產生完整的 HTML,前端一次異步取回直接塞進容器。
方法二:待選項與已選項分別用兩次異步取得,前端再用 JavaScript 比對、設定勾選狀態。
個人不喜歡把太多邏輯寫在頁面上,所以偏好並推薦方法一:後端把 HTML 拼好,前端只負責放進去。
// 彈出選擇共享檔案的對話框
function showShareFile() {
disableFileArea();
if (!chooseObj.isChoosed()) {
handleWarm("請先選擇檔案或者目錄");
enableFileArea();
return;
}
$('#shareRange').html('');
showflowcontent('fxcontentflow');
$.ajax({
url: '../share/showShareRange.do',
cache: false,
type: 'post',
dataType: 'html',
async: true,
contentType: "application/x-www-form-urlencoded;charset=utf-8",
data: { 'signid': chooseObj.id, 'objtype': chooseObj.type },
success: function (html) {
$('#shareRange').html(html); // 後端回傳的 HTML 直接注入
}
});
}
<div class="flowcontent" id="fxcontentflow">
<div id="fxloadfile" class="content">
<div class="title">
<strong>分享檔案</strong>
<input type="button" class="closebtn gb" onClick="hideflowcontent(this)" title="關閉" />
</div>
<div class="body">
<div class="file" id="shareRange"><!-- 共享範圍由 AJAX 填入 --></div>
<div class="btns">
<input type="button" class="submitbtn" value="" onClick="shareFile()" />
<input type="button" class="cancelbtn gb" onClick="closeflowcontent('fxcontentflow')" />
</div>
</div>
</div>
</div>
/**
* 顯示指定檔案、資料夾的共享範圍(共享用戶)
* @param signid 選中的檔案 id 或資料夾 id
* @param objtype 操作對象類型(file、folder)
* return 0 表示無權限操作 / 1 表示有權限操作
*/
public void showShareRange(HttpServletRequest request, HttpServletResponse response) {
String signid = request.getParameter("signid") == null ? "" : request.getParameter("signid");
String objtype = request.getParameter("objtype") == null ? "" : request.getParameter("objtype");
// 取得目前的共享用戶清單
List<String> userIdList = fileShareManager.showShareRange(signid, objtype);
try {
// 把共享範圍轉成 HTML
String str = fileShareManager.trunToShareRangeHtml(userIdList);
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write(str);
pw.flush();
pw.close();
} catch (Exception e) {
logger.info(e);
e.printStackTrace();
}
}
/**
* 把共享範圍轉換成 HTML 格式
* @param userIdList 已經共享的人員列表
*/
public String trunToShareRangeHtml(List<String> userIdList) throws Exception {
IOrgServiceClient client = new IOrgServiceClient();
IOrgServicePortType service = client.getIOrgServiceHttpPort();
List<WebDeptment> deptlist = Ws_DeptCenter.getAllDepts();
StringBuffer sb = new StringBuffer();
for (WebDeptment dept : deptlist) {
List<DmUser> userList = userManager.getUserListByDeptid(dept.getDepId(), dept.getActdepId(), service);
sb.append("<div class=\"fxtitle\">" + dept.getDepMiniName() + "</div>");
sb.append("<ul class=\"fxxz\">");
for (DmUser user : userList) {
String userid = user.getUserId();
String username = user.getUserName();
sb.append("<li>");
if (userIdList.contains(userid)) {
// 已在共享清單中,加上 checked
sb.append("<input type='checkbox' checked name='shareUserId' value='")
.append(userid).append("'>").append(username).append(" ");
} else {
sb.append("<input type='checkbox' name='shareUserId' value='")
.append(userid).append("'>").append(username).append(" ");
}
sb.append("</li>");
}
sb.append("</ul>");
}
return sb.toString();
}
僅供對照,不需自行實作:
<div class="fxtitle">院領導</div> <ul class="fxxz"> <li><input type="checkbox" name="shareUserId" value="xiaolin">肖林</li> <li><input type="checkbox" name="shareUserId" value="wangshuotong">王碩佟</li> <li><input type="checkbox" name="shareUserId" value="wangshengyang">汪勝洋</li> </ul> <div class="fxtitle">辦公室</div> <ul class="fxxz"> <li><input type="checkbox" name="shareUserId" value="lujianping">陸建平</li> <li><input type="checkbox" checked name="shareUserId" value="guoshunlan">郭順蘭</li> <li><input type="checkbox" name="shareUserId" value="fangying">方穎</li> <li><input type="checkbox" checked name="shareUserId" value="songweilei">宋維蕾</li> <li><input type="checkbox" checked name="shareUserId" value="lijing">李靖</li> </ul>
前端只需一行 $('#shareRange').html(html) 就完成渲染,勾選狀態、部門分組、排序規則全部由後端決定。這樣做的好處是規則集中在一處,前端不必重複實作一遍「哪些人已被選」的比對邏輯;缺點是前後端耦合度提高,若同一份資料還要在其他地方以不同形式呈現,改版成本會變高。團隊可以依實際情況權衡。