diff --git a/cloud-common-plugin/src/main/resources/templates/tree/tree-user.html b/cloud-common-plugin/src/main/resources/templates/tree/tree-user.html index ae3300e..1fe423e 100644 --- a/cloud-common-plugin/src/main/resources/templates/tree/tree-user.html +++ b/cloud-common-plugin/src/main/resources/templates/tree/tree-user.html @@ -10,6 +10,7 @@ +
@@ -69,6 +70,35 @@ border: '1px dotted silver' }); } + // 节点是否已经选择 + function isNodeChecked(userId, departmentId) { + for(var i = 0, item; item = selectDepartmentUserArray[i++];) { + if(departmentId == item.departmentId && userId == item.userId) { + return true; + } + } + return false; + } + // 选择人员 + function addSelectDepartmentUserArray(id, name, title, departmentId) { + selectDepartmentUserArray.push({ + userId: id, + userName: name, + userTitle: title, + departmentId: departmentId, + }); + $('#selectUsers').append(''+ title +' '); + } + // 删除人员 + function removeSelectDepartmentUserArray(userId, departmentId) { + for(var i = 0, item; item = selectDepartmentUserArray[i++];) { + if(departmentId == item.departmentId && userId == item.userId) { + selectDepartmentUserArray.splice(--i, 1); + $('#selected_user_'+ userId).remove(); + break; + } + } + } // 初始化数据 var initTimeout = null; var isInit = false; @@ -82,7 +112,6 @@ } } } - // selectUser(zTree.getCheckedNodes(true)); } function initCheckData(zTree) { if(selectedUsers) { @@ -95,7 +124,7 @@ selectedUsers: selectedUsers }, null, function(code, data) { for(var i = 0, item; item = data[i++];) { - initSelectDepartmentUserArray(item.userId, item.userName, ('【'+ item.departmentName +'】'+ item.userName), item.departmentId); + addSelectDepartmentUserArray(item.userId, item.userName, ('【'+ item.departmentName +'】'+ item.userName), item.departmentId); } initSelectNode(zTree); }, function(code, data) { @@ -134,7 +163,9 @@ }, callback: { onCheck: function(event, treeId, treeNode) { - selectUser(zTree.getCheckedNodes(true)); + var checkedNodes = zTree.getCheckedNodes(true); + var unCheckNodes = zTree.getCheckedNodes(false); + selectUser(checkedNodes, unCheckNodes); return false; }, onAsyncSuccess: function(event, treeId, treeNode) { @@ -150,26 +181,31 @@ initFrame(); initThree(); // 选择人员 - function initSelectDepartmentUserArray(id, name, title, departmentId) { - console.log(title); - selectDepartmentUserArray.push({ - userId: id, - userName: name, - userTitle: title, - departmentId: departmentId, - }); - $('#selectUsers').append(''+ title +' '); - } - function selectUser(checkedNodes) { - selectDepartmentUserArray.splice(0, selectDepartmentUserArray.length); - $('#selectUsers').empty(); - for(var i = 0, item = checkedNodes[i]; item = checkedNodes[i++];) { - if(item.title && item.title != '') { - initSelectDepartmentUserArray(item.id.substring(2, item.id.length), item.name, item.title, item.pId); + function selectUser(checkedNodes, unCheckNodes) { + // 清空未选中的列表 + for(var i = 0, item; item = unCheckNodes[i++];) { + if(item.title) { + removeSelectDepartmentUserArray(item.id.substring(2, item.id.length), item.pId); + } + } + for(var i = 0, item; item = checkedNodes[i++];) { + if(item.title) { + var userId = item.id.substring(2, item.id.length); + if(!isNodeChecked(userId, item.pId)) { + addSelectDepartmentUserArray(userId, item.name, item.title, item.pId); + } } } top.dialog.dialogData.selectedDepartmentUsers = selectDepartmentUserArray; } + $(document).on('click', '.remove-selected-user', function() { + var zTree = $.fn.zTree.getZTreeObj('leftTree'); + removeSelectDepartmentUserArray(this.dataset.userid, this.dataset.departmentid); + // 删除选中状态 + var node = zTree.getNodeByParam('id', 'u_'+ this.dataset.userid, null); + zTree.checkNode(node, false, true); + top.dialog.dialogData.selectedDepartmentUsers = selectDepartmentUserArray; + }); $('.confirm').on('click', function() { closeBox(); });