app.controller("inspectionScope", [ "$rootScope", "$scope", "$state", "$timeout", "$interval", "SweetAlert", "$modal", "Restangular", "api_bpm_data", "api_solution", "api_user_data", "api_wechatfile", "api_sysinfo", "api_statistic", "api_bpm", "jry_api_bpm", "api_simple", function ( $rootScope, $scope, $state, $timeout, $interval, SweetAlert, $modal, Restangular, api_bpm_data, api_solution, api_user_data, api_wechatfile, api_sysinfo, api_statistic, api_bpm, jry_api_bpm, api_simple ) { var loginUser = $rootScope.user; $scope.everyDay = { time: new Date(), everyDayNotices: [ {id:0 ,name:'线上', checked:false}, {id:1 ,name:'线下', checked:false} ] } // 回显数据 start $scope.stateModel = {id: $state.params.id}; $scope.getInspectionById= function () { api_user_data.fetchgetData("inspection", $state.params.id).then((res) => { console.log(res.data) if(res.status == 200){ $scope.stateModel = res.data; console.log($scope.stateModel); $scope.modelName = $scope.stateModel.modelName || $scope.stateModel.title; $scope.everyDay = { time: $scope.stateModel.dayMsgTime?new Date($scope.stateModel.dayMsgTime):new Date(), everyDayNotices: [ {id:0 ,name:'线上', checked:$scope.stateModel.dayMsgOnline == 1}, {id:1 ,name:'线下', checked:$scope.stateModel.dayMsgOffline == 1} ] } if ($scope.stateModel.model != 1) { $scope.stateModel.model = 0; } // 通知规则 console.log($scope.incidentManageruserList) console.log($scope.notice.inspectionMsgRules) $scope.notice.user = []; var msgRuleIds = $scope.stateModel.msgRules?$scope.stateModel.msgRules.split(','):[]; var msgUserIds = $scope.stateModel.msgUsers?$scope.stateModel.msgUsers.split(','):[]; if(msgRuleIds.length){ msgRuleIds.forEach(v => { for (let i = 0; i < $scope.notice.inspectionMsgRules.length; i++) { if($scope.notice.inspectionMsgRules[i].id == v){ $scope.notice.inspectionMsgRules[i].checked = true; } } }) } if(msgUserIds.length){ msgUserIds.forEach(v => { let item = $scope.incidentManageruserList.find(vv => vv.id == v); if(item){ $scope.notice.user.push(item); } }) } } }); }; console.log($state.params.id) // $scope.stateModel = JSON.parse($state.params.model); // console.log($scope.stateModel); // $scope.modelName = $scope.stateModel.modelName || $scope.stateModel.title; // if ($scope.stateModel.model != 1) { // $scope.stateModel.model = 0; // } // 回显数据 end // 通知 start // 获取事件经理角色的人 $scope.incidentManageruserList = []; $scope.getUserListByRole = function () { var postData = { idx: 0, sum: 1000, "user": { "roledata": { "rolecode": "incident manager" }, "simple": true } }; api_user_data.fetchDataList("user", postData).then((res) => { $scope.incidentManageruserList = res.list; $scope.getInspectionById(); }); }; // 通知规则 $scope.notice = { user: [], inspectionMsgRules: [] } $scope.getInspectionMsgRules = function(){ var postData = { key: "inspection_msg_rules", type: "list" } api_wechatfile.getDictionary(postData).then(function (res) { $scope.notice.inspectionMsgRules = res.map(v => ({...v, checked:false})); $scope.getUserListByRole(); }) } $scope.getInspectionMsgRules(); // 通知 end $scope.xinzeng = false; $scope.shanchu = false; $scope.bianji = false; for (var i = 0; i < loginUser.menu.length; i++) { if (loginUser.menu[i].link == "shijianshezhi_xinzeng") { $scope.xinzeng = true; } if (loginUser.menu[i].link == "shijianshezhi_shanchu") { $scope.shanchu = true; } if (loginUser.menu[i].link == "shijianshezhi_bianji") { $scope.bianji = true; } } $scope.listDataTop = []; $scope.listDataBottom = []; $scope.leftListIdx = -1; $scope.leftListName = "巡检范围"; $scope.leftListCli = function (idx, data) { $scope.leftListIdx = idx; // $scope.stateModel = data; $scope.stateModel.currentId = data.id; $scope.modelName = data.modelName; $scope.copyInspection.tree = []; $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); }; // 全选 // 把指令的方法绑定到控制器 $scope.setDirectiveFn = function (fn) { $scope.selectAll = fn; } // 事件分类开始 var loginUser = $rootScope.user; //组 api_user_data .fetchDataList("group", { idx: 0, sum: 1000 }) .then(function (data) { $scope.group = data.list; }); api_wechatfile .getDictionary({ key: "incident_priority", type: "list" }) .then(function (data) { $scope.priority = data; }); /* ----- tree----- */ var i = 0; $scope.out = []; $scope.outdata = []; $scope.subdata = {}; $scope.sub1data = {}; $scope.addcate = false; $scope.add1cate = false; $scope.changecate = false; $scope.filterTree = function (data, id) { data = angular.copy(data); var newTree = data.filter((x) => x.id !== id); newTree.forEach( (x) => x.children && (x.children = $scope.filterTree(x.children, id)) ); return newTree; }; $scope.userOrGroupType = {}; //当前节点运维组/运维人员 $scope.dataSave = []; //储存$scope.tree_data $scope.group_user = { // offlineGroup:{},//线下组 // onlineUser:{}//线上人 }; $scope.selectedInspection = { tree: {}, isShowZtree: true, }; $scope.copyInspection = { tree: [], }; $scope.timerZtree = null; $scope.timerZtreeCopy = null; // 新增 $scope.addType1 = function (type) { console.log(); // =============================================== var modalInstance = $modal.open({ templateUrl: "assets/views/system/tpl/inspectionchange.html", controller: function ($scope, scope, $modalInstance) { $scope.type = type; $scope.add1data = {}; $scope.thisParent = {}; //当前点击节点的父节点 $scope.filterTree = function (data, id) { data = angular.copy(data); var newTree = data.filter((x) => x.id !== id); newTree.forEach( (x) => x.children && (x.children = $scope.filterTree(x.children, id)) ); return newTree; }; $scope.try_async_1load = function () { $scope.my_1data = []; var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } }; api_bpm_data .fetchDataList("inspectionType", data) .then(function (response) { var data = response.list; data = data.filter((v) => !v.parent); //只显示一级 var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; object.id = data[i].id; if (angular.isDefined(data[i].user)) { object.user = data[i].user || {}; } if (angular.isDefined(data[i].group)) { object.group = data[i].group || {}; } if (angular.isDefined(data[i].parent)) { object.parent = data[i].parent; } object.label = data[i].type; objects.push(object); } $scope.my_1data = convertParentToChildList(objects); $scope.tree_1data = angular.copy($scope.my_1data); // 把选中的巡检范围当作父类回显 if (scope.selectedInspection.tree.id) { // 区分新增和修改 if (type == "add") { $scope.thisParent = data.find( (v) => v.id == scope.selectedInspection.tree.id ); if ($scope.thisParent) { $scope.thisParent.label = $scope.thisParent.type; $scope.thisParent.isExpanded = true; $scope.thisParent.selected = true; $scope.addparent = true; } else { $scope.addparent = false; } } else if (type == "edit") { // 编辑 console.log($scope.my_1data, "原始数据"); $scope.tree_1data = $scope.my_1data.filter( (v) => v.id != scope.selectedInspection.tree.id ); //过滤本条 $scope.thisParent = data.find( (v) => v.id == scope.selectedInspection.tree.pId ); if ($scope.thisParent) { $scope.thisParent.label = $scope.thisParent.type; $scope.thisParent.isExpanded = true; $scope.thisParent.selected = true; $scope.addparent = true; } else { $scope.addparent = false; } $scope.add1data.type = scope.selectedInspection.tree.name; $scope.add1data.id = scope.selectedInspection.tree.id; } } else { $scope.addparent = false; } }); }; $scope.try_async_1load(); function convertListToTree(data, treeMap) { var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup var root = null; //Initially set our loop to null //loop over data for (var i = 0; i < data.length; i++) { var datum = data[i]; //each node will have children, so let's give it a "children" poperty datum.children = []; //add an entry for this node to the map so that any future children can //lookup the parent idToNodeMap[datum.id] = datum; //Does this node have a parent? // console.log("datum="+JSON.stringify(datum)) if (typeof datum.parent === "undefined") { //Doesn't look like it, so this node is the root of the tree root = datum; treeMap[datum.id] = root; } else { //This node has a parent, so let's look it up using the id parentNode = idToNodeMap[datum.parent.id]; //We don't need this property, so let's delete it. // delete datum.parent; //Let's add the current node as a child of the parent node. parentNode.children.push(datum); } } return root; } function convertParentToChildList(data) { var treeMap = {}; var list = []; convertListToTree(data, treeMap); angular.forEach(treeMap, function (item) { list.push(item); }); return list; } // ------------------------------------------------ $scope.title = type == "add" ? "巡检范围新增" : "巡检范围修改"; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; $scope.savercode = function (data) { if (data && data.type) { if ($scope.addparent) { data.parent = $scope.add1data.parent || $scope.thisParent; } else { data.parent = null; } scope.selectedInspection.tree = {}; $modalInstance.close(data); } else { SweetAlert.swal( { title: "新增失败!", text: "请填写必填项!", type: "error", confirmButtonColor: "#DD6B55", }, function () {} ); } }; }, resolve: { scope: function () { return $scope; }, }, }); // liaomingming modalInstance.result.then(function (selectedItem) { console.log($scope); if (selectedItem.type) { selectedItem.deleteFlag = 0; selectedItem.formUiName = "inspection_LinHu_detail"; selectedItem.formUiEdit = "inspection_LinHu_confirm"; selectedItem.processKey = "bpm_inspection"; var filedata = { inspectionType: selectedItem }; console.log(filedata); // return; api_bpm_data[type == "add" ? "addData" : "updData"]( "inspectionType", filedata ).then(function (response) { if (response) { if (response.status == 200) { $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); SweetAlert.swal({ title: type == "add" ? "增加成功!" : "修改成功!", type: "success", }); } else { SweetAlert.swal({ title: type == "add" ? "增加失败!" : "修改失败!", type: "error", }); } } }); } else { SweetAlert.swal( { title: "新增失败!", text: "请填写必填项!", type: "error", confirmButtonColor: "#DD6B55", }, function () {} ); } }); }; /* -----start remove incidet category----- */ // 删除 $scope.remove1 = function () { var modalInstance = $modal.open({ templateUrl: "assets/views/delete.html", controller: function ($scope, scope, $modalInstance, api_bpm_data) { $scope.title = "巡检范围删除?"; $scope.connect = "确定要删除此巡检范围?"; $scope.ok = function () { $modalInstance.close(scope.selectedInspection.tree); }; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; }, size: "sm", resolve: { scope: function () { return $scope; }, }, }); modalInstance.result.then(function (selectedItem) { if (selectedItem) { var rmvList = []; rmvList.push(selectedItem.id); if (selectedItem.children && selectedItem.children.length > 0) { SweetAlert.swal({ title: "删除失败!", text: "请先删除选中的范围的子级", type: "error", }); } else { api_bpm_data .rmvData("inspectionType", rmvList) .then(function (response) { if (response.data) { SweetAlert.swal( { title: "删除成功!", type: "success", confirmButtonColor: "#007AFF", }, function () { $scope.selectedInspection.isShowZtree = false; setTimeout(() => { $scope.selectedInspection.isShowZtree = true; }, 0); } ); } else { SweetAlert.swal({ title: "操作异常!", text: "系统异常,请稍后重试,或者联系管理员!", type: "error", }); } }); } } }); }; /* -----end remove incidet category----- */ function convertListToTree(data, treeMap) { var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup var root = null; //Initially set our loop to null //loop over data for (var i = 0; i < data.length; i++) { var datum = data[i]; //each node will have children, so let's give it a "children" poperty datum.children = []; //add an entry for this node to the map so that any future children can //lookup the parent idToNodeMap[datum.id] = datum; //Does this node have a parent? // console.log("datum="+JSON.stringify(datum)) if (typeof datum.parent === "undefined") { //Doesn't look like it, so this node is the root of the tree root = datum; treeMap[datum.id] = root; } else { //This node has a parent, so let's look it up using the id parentNode = idToNodeMap[datum.parent.id]; //We don't need this property, so let's delete it. // delete datum.parent; //Let's add the current node as a child of the parent node. parentNode.children.push(datum); } } return root; } function convertParentToChildList(data) { var treeMap = {}; var list = []; convertListToTree(data, treeMap); angular.forEach(treeMap, function (item) { list.push(item); }); return list; } $scope.closethis = function (formdata) { formdata.category = ""; formdata.parent.category = ""; }; /* -----start submit incidet category----- */ // 保存设置 $scope.selectedchangecategory = { value: {} }; $scope.$watch("selectedchangecategory.value", function (newVal) { console.log(newVal); if (newVal.user) { $scope.group_user["onlineUser"] = newVal.user || undefined; } else { $scope.group_user["onlineUser"] = undefined; } if (newVal.group) { $scope.group_user["offlineGroup"] = newVal.group || undefined; } else { $scope.group_user["offlineGroup"] = undefined; } }); $scope.editInspection = function (tree, id, newVal, type) { tree.forEach((v) => { if (v.id == id) { v[type] = newVal; } else if (v.children) { $scope.editInspection(v.children, id, newVal, type); } }); }; $scope.$watch("group_user.offlineGroup", function (newVal) { console.log($scope.selectedchangecategory.value); console.log($scope.copyInspection.tree); console.log(newVal, "group"); $scope.editInspection( $scope.copyInspection.tree, $scope.selectedchangecategory.value.id, newVal, "group" ); if (newVal && newVal.id) { $scope.selectedchangecategory.value.group = newVal; } else { delete $scope.selectedchangecategory.value.group; } }); $scope.$watch("group_user.onlineUser", function (newVal) { // console.log($scope.selectedchangecategory.value) // console.log($scope.copyInspection.tree) // console.log(newVal, 'user') $scope.editInspection( $scope.copyInspection.tree, $scope.selectedchangecategory.value.id, newVal, "user" ); if (newVal && newVal.id) { $scope.selectedchangecategory.value.user = newVal; } else { delete $scope.selectedchangecategory.value.user; } }); // 保存设置 liaomingming $scope.submit1change = function () { console.log($scope.everyDay); console.log($scope.notice); console.log($scope.copyInspection.tree); console.log($scope.stateModel); if ($scope.stateModel.model === 0) { // 不设置为模块的时候需要弹窗提示 var modalInstance = $modal.open({ templateUrl: "assets/views/system/tpl/inspectionModel.html", controller: function ($scope, scope, $modalInstance) { $scope.add1data = { model: false }; $scope.title = "提示"; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; $scope.savercode = function (data) { console.log(scope.copyInspection.tree); scope.stateModel.model = data.model ? 1 : 0; scope.saveCommon(scope); $modalInstance.close(data); }; }, resolve: { scope: function () { return $scope; }, }, }); modalInstance.result.then(function (selectedItem) {}); return; } $scope.saveCommon($scope); }; // 暂存 $scope.submit1change_save = function (flag) { $rootScope.isMask = true; var arr = $scope.TreeToArr($scope.copyInspection.tree, []); $scope.stateModel.modelName = $scope.modelName; $scope.stateModel.typeIds = arr.map((v) => v.id).toString(); arr = arr.map((v) => ({ inspection: $scope.stateModel.id, inspectionType: v.id, inspectionTypeName: v.name, parent: v.parent, user: v.user?v.user.id:undefined, userName: v.user?v.user.name:undefined, group: v.group?v.group.id:undefined, groupName: v.group?v.group.groupName:undefined, })); // 一条都没选择 if (arr.length === 0) { arr.push({ inspection: $scope.stateModel.id }); } console.log(arr); // return; api_simple.addListData("inspectionBind", arr).then(function (response) { if (response) { if (response.status == 200) { var filedata = { close: "关闭", save: "暂存", submit: "下一步", inspection: $scope.stateModel, }; console.log($scope.notice); var inspectionMsgRules = $scope.notice.inspectionMsgRules.filter(v => v.checked); var user = $scope.notice.user; if(inspectionMsgRules.length){ filedata.inspection.msgRules = inspectionMsgRules.map(v=>v.id).toString() }else{ delete filedata.inspection.msgRules; } if(user.length){ filedata.inspection.msgUsers = user.map(v=>v.id).toString() }else{ delete filedata.inspection.msgUsers; } var dayMsgOnline = $scope.everyDay.everyDayNotices.find(v => v.id === 0).checked;//线上 var dayMsgOffline = $scope.everyDay.everyDayNotices.find(v => v.id === 1).checked;//线下 var dayMsgTime = $scope.everyDay.time; filedata.inspection.dayMsgOnline = dayMsgOnline ? 1: 0; filedata.inspection.dayMsgOffline = dayMsgOffline ? 1: 0; filedata.inspection.dayMsgTime = dayMsgTime ? (new Date(dayMsgTime).getTime()): (new Date().getTime()); // 修改巡检计划 baba api_bpm_data .addData("inspection", filedata) .then(function (response) { $rootScope.isMask = false; if (response) { if (response.status == 200) { SweetAlert.swal( { title: "暂存成功!", type: "success", }, function () { if(flag){ $state.go('app.inspection.editor', { formKey: 'inspectionform', service: 'api_bpm_data', model: JSON.stringify({ model: { inspection: filedata.inspection } }) }); }else{ $scope.selectedchangecategory.value = {}; $scope.copyInspection.tree = []; $scope.changecateObj.changecate = false; $scope.selectedInspection.isShowZtree = false; setTimeout(()=>{ $scope.selectedInspection.isShowZtree = true; },0) } } ); } else { SweetAlert.swal({ title: "保存失败!", type: "error", }); } } }); } else { $rootScope.isMask = false; SweetAlert.swal({ title: "保存失败!", type: "error", }); } }else{ $rootScope.isMask = false; } }); }; // 上一步 $scope.submit1change_prev = function(){ SweetAlert.swal( { title: "您是否暂存当前设置!", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "是", cancelButtonText: "否", }, function (isConfirm) { if (isConfirm) { $scope.submit1change_save(true); }else{ window.history.back(); } } ); } $scope.closeModel = function () { $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表 }; $scope.TreeToArr = function (tree, arr) { tree.forEach((v) => { if (v.children) { arr.push(v); $scope.TreeToArr(v.children, arr); } else { arr.push(v); } }); return arr; }; $scope.saveCommon = function ($scope) { if (!$scope.modelName.trim()) { SweetAlert.swal({ title: "策略名称不能为空!", type: "error", }); return; } $rootScope.isMask = true; var arr = $scope.TreeToArr($scope.copyInspection.tree, []); $scope.stateModel.modelName = $scope.modelName; $scope.stateModel.typeIds = arr.map((v) => v.id).toString(); arr = arr.map((v) => ({ inspection: $scope.stateModel.id, inspectionType: v.id, inspectionTypeName: v.name, parent: v.parent, user: v.user?v.user.id:undefined, userName: v.user?v.user.name:undefined, group: v.group?v.group.id:undefined, groupName: v.group?v.group.groupName:undefined, })); // 一条都没选择 if (arr.length === 0) { arr.push({ inspection: $scope.stateModel.id }); } console.log(arr); // return; api_simple.addListData("inspectionBind", arr).then(function (response) { $rootScope.isMask = false; if (response) { if (response.status == 200) { $scope.stateModel.status = { id: 120 }; //强制扭转状态为正常 var filedata = { close: "关闭", save: "暂存", submit: "下一步", inspection: $scope.stateModel, }; console.log($scope.notice); var inspectionMsgRules = $scope.notice.inspectionMsgRules.filter(v => v.checked); var user = $scope.notice.user; if(inspectionMsgRules.length){ filedata.inspection.msgRules = inspectionMsgRules.map(v=>v.id).toString() }else{ delete filedata.inspection.msgRules; } if(user.length){ filedata.inspection.msgUsers = user.map(v=>v.id).toString() }else{ delete filedata.inspection.msgUsers; } var dayMsgOnline = $scope.everyDay.everyDayNotices.find(v => v.id === 0).checked;//线上 var dayMsgOffline = $scope.everyDay.everyDayNotices.find(v => v.id === 1).checked;//线下 var dayMsgTime = $scope.everyDay.time; filedata.inspection.dayMsgOnline = dayMsgOnline ? 1: 0; filedata.inspection.dayMsgOffline = dayMsgOffline ? 1: 0; filedata.inspection.dayMsgTime = dayMsgTime ? (new Date(dayMsgTime).getTime()): (new Date().getTime()); // 修改巡检计划 baba api_bpm_data .addData("inspection", filedata) .then(function (response) { if (response) { if (response.status == 200) { SweetAlert.swal( { title: "保存成功!", type: "success", }, function () { $state.go("app.inspection.inspectPlan"); //跳转巡检计划列表 } ); } else { SweetAlert.swal({ title: "保存失败!", type: "error", }); } } }); } else { SweetAlert.swal({ title: "保存失败!", type: "error", }); } } }); }; $scope.add1data = {}; $scope.addparent = false; // 事件分类结束 // 获取组数据 $scope.getGroupList = function () { var postData = { idx: 0, sum: 1000, group: { selectType: "nouser" }, }; api_user_data.fetchDataList("group", postData).then((res) => { $scope.groupList = res.list; }); }; $scope.getGroupList(); // 获取巡检计划数据 $scope.getGroupList = function (model) { var postData = { idx: 0, sum: 3, inspection: { model: model }, }; api_user_data.fetchDataList("inspection", postData).then((res) => { if (model == 1) { $scope.listDataTop = res.list; //巡检范围策略模板 } else { $scope.listDataBottom = res.list; //往期巡检范围策略 } }); }; $scope.getGroupList(1); $scope.getGroupList(); // 获取人数据 $scope.getUserList = function () { var postData = { idx: 0, sum: 1000, user: { name: "", selectType: "pinyin_qs", }, }; api_user_data.fetchDataList("user", postData).then((res) => { $scope.userList = res.list; }); }; $scope.getUserList(); $scope.changecateObj = { changecate: false, }; // 缓存的绑定数据-http $scope.cacheHttp = { value: [], }; }, ]); app.directive("zTree", function () { // ztree liaomingming return { restrict: "EA", scope: { selectInspectionType: "=", copyInspectionType: "=", stateModel: "=", changecate: "=", cachehttp: "=", setFn: '&' }, controller: function ($rootScope,$scope, $element, api_bpm_data, api_simple) { console.log($scope); getTreeInfo(); function getTreeInfo() { $rootScope.isMask = true; $scope.treeInfo = []; var data = { idx: 0, sum: 1000, inspectionType: { deleteFlag: 0 } }; $scope.isShowZtree = false; api_bpm_data .fetchDataList("inspectionType", data) .then(function (response) { var data = response.list; console.log(data); var objects = []; for (var i = 0; i < data.length; i++) { var object = {}; object.id = data[i].id; if (angular.isDefined(data[i].user)) { object.user = data[i].user || {}; } if (angular.isDefined(data[i].group)) { object.group = data[i].group || {}; } if (angular.isDefined(data[i].parent)) { object.parent = data[i].parent; object.pId = data[i].parent.id; } else { object.pId = 0; } object.name = data[i].type; objects.push(object); } $scope.treeInfo = convertParentToChildList(objects); $scope.treeInfo = deleteEmputyChildren($scope.treeInfo); console.log($scope.treeInfo); // ==================start================ api_simple .fetchDataList("inspectionBind", { idx: 0, sum: 1000, inspectionBind: { inspection: $scope.stateModel.currentId || $scope.stateModel.id }, }) .then(function (response) { $rootScope.isMask = false; $.fn.zTree.destroy("z-tree"); $.fn.zTree.init($($element), setting, $scope.treeInfo); $scope.isShowZtree = true; var result = response.list || []; $scope.cachehttp = angular.copy(result); result = result.map((v) => v.inspectionType); console.log(result); var zTree = $.fn.zTree.getZTreeObj("z-tree"); var nodesAll = zTree.transformToArray(zTree.getNodes());//全部节点转换成数组array $('#c_all').prop('checked', nodesAll.length === result.length); result.forEach((v) => { // 设置路径树节点选中 // 1、根据id获取树的某个节点: var node = zTree.getNodeByParam("id", v); // 2、设置node节点选中状态: // zTree.selectNode(node); // 3、设置node节点checked选中: node.checked = true; //注:设置checked属性之后,一定要更新该节点 zTree.updateNode(node); // console.log($scope.copyInspectionType) // zTree.expandNode(node); // setTimeout(() => { // zTreeOnCheck(null, null, node); // }, 0); }); // zTree.expandAll(false); //展示选中的数据 var arr = []; $scope.cachehttp.forEach(v=>{ arr.push({ id: v.inspectionType, name: v.inspectionTypeName, group: v.group?{id:v.group,groupName:v.groupName}:undefined, user: v.user?{id:v.user,name:v.userName}:undefined, parent: v.parent ? { id: v.parent.inspectionType } : undefined, pId: v.parent ? v.parent.inspectionType : 0, }); }) $scope.copyInspectionType = tranListToTreeData(arr); }); // ==================end================ }); } function deleteEmputyChildren(data) { data.forEach((v) => { if (v.children.length) { deleteEmputyChildren(v.children); } else { delete v.children; } }); return data; } function convertParentToChildList(data) { var treeMap = {}; var list = []; convertListToTree(data, treeMap); angular.forEach(treeMap, function (item) { list.push(item); }); return list; } function convertListToTree(data, treeMap) { var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup var root = null; //Initially set our loop to null //loop over data for (var i = 0; i < data.length; i++) { var datum = data[i]; //each node will have children, so let's give it a "children" poperty datum.children = []; //add an entry for this node to the map so that any future children can //lookup the parent idToNodeMap[datum.id] = datum; //Does this node have a parent? // console.log("datum="+JSON.stringify(datum)) // if (typeof datum.parent === "undefined") { if (datum.pId === 0) { //Doesn't look like it, so this node is the root of the tree root = datum; treeMap[datum.id] = root; } else { //This node has a parent, so let's look it up using the id // parentNode = idToNodeMap[datum.parent.id]; parentNode = idToNodeMap[datum.pId]; //We don't need this property, so let's delete it. // delete datum.parent; //Let's add the current node as a child of the parent node. parentNode.children.push(datum); } } return root; } var setting = { check: { enable: true, chkStyle: "checkbox", chkboxType: { Y: "ps", N: "ps" }, }, callback: { onClick: onMouseDown, onCheck: zTreeOnCheck, }, view: { showIcon: false, showLine: false, }, }; // $.fn.zTree.init($($element), setting, $scope.treeInfo); //点击菜单项 function onMouseDown(event, treeId, treeNode) { console.log(event, treeId, treeNode); $scope.selectInspectionType = treeNode; } // 点击复选框 function zTreeOnCheck(event, treeId, treeNode) { console.log(event, treeId, treeNode); $scope.changecate = false; var treeObj = $.fn.zTree.getZTreeObj("z-tree"); var nodesAll = treeObj.transformToArray(treeObj.getNodes());//全部节点转换成数组array var nodes_arr = treeObj.getCheckedNodes(true); var nodes = angular.copy(nodes_arr); $('#c_all').prop('checked', nodesAll.length === nodes_arr.length); console.log(nodes); if ($scope.cachehttp && $scope.cachehttp.length) { var result = angular.copy($scope.cachehttp); zTreeOnCheckCommon(result, nodes); } else { api_simple .fetchDataList("inspectionBind", { idx: 0, sum: 1000, inspectionBind: { inspection: $scope.stateModel.id }, }) .then(function (response) { var result = response.list || []; zTreeOnCheckCommon(result, nodes); }); } } function tranListToTreeData(nodes){ var arr = nodes.filter((v) => v.pId === 0); arr.forEach((v) => { // v.children = []; delete v.children; }); arr.forEach((v) => { nodes.forEach((vv) => { if (vv.pId == v.id) { if(v.children){ v.children.push(vv); }else{ v.children = [vv]; } } }); }); return arr; } $scope.clickDirective = function () { console.log('全选'); var zTree = $.fn.zTree.getZTreeObj("z-tree");//得到tree对象 treeMenu是我的treeid var node = zTree.getNodes();//得到全部节点 var nodes = zTree.transformToArray(node);//全部节点转换成数组array var checkNode = zTree.getCheckedNodes().length;//得到被选中的节点数量,zTree.getCheckedNodes()和zTree.getCheckedNodes(true)一样,可以传参(true/false),默认是true if (checkNode < nodes.length) {//已选中的节点数小于总数 - 全选 zTree.checkAllNodes(true);//给全部节点设置为true zTreeOnCheckCommon([], nodes) } else{ zTree.checkAllNodes(false);//给全部节点设置为false zTreeOnCheckCommon([], []) } } $scope.setFn.call($scope, { params: $scope.clickDirective }) // 点击复选框-common function zTreeOnCheckCommon(result, nodes) { // 如果有值,则不覆盖 if($scope.copyInspectionType.length){ result = []; $scope.copyInspectionType.forEach(v=>{ result.push(v); if(v.children && v.children.length){ v.children.forEach(vv=>{ result.push(vv); }) } }) }else{ result = result.map((v) => ({ id: v.inspectionType, name: v.inspectionTypeName, group: v.group?{id:v.group,groupName:v.groupName}:undefined, user: v.user?{id:v.user,name:v.userName}:undefined, parent: v.parent ? { id: v.parent.inspectionType } : undefined, pId: v.parent ? v.parent.inspectionType : 0, })); } for (var i = 0; i < nodes.length; i++) { for (var j = 0; j < result.length; j++) { if (nodes[i].id == result[j].id) { nodes[i] = angular.copy(result[j]); } } } $scope.copyInspectionType = tranListToTreeData(nodes); } }, }; }); app.directive("zTreeCopy", function () { // ztree liaomingming return { restrict: "EA", scope: { copyInspectionType: "=", changecate: "=", selectedchangecategory: "=", }, link: function (scope, element, attrs) { scope.$watch("copyInspectionType", function (newVal) { console.log(newVal); var setting = { view: { showIcon: false, showLine: false, }, callback: { onClick: onMouseDown, }, }; $.fn.zTree.destroy("z-tree-copy"); $.fn.zTree.init($(element), setting, newVal); // var treeObj = $.fn.zTree.init($(element), setting, newVal); // treeObj.expandAll(true); }); //点击菜单项 function onMouseDown(event, treeId, treeNode) { console.log(event, treeId, treeNode); scope.selectedchangecategory = treeNode; scope.changecate = true; console.log(scope.copyInspectionType); } }, controller: function ($scope, $element, api_bpm_data) {}, }; });