281 lines
7.8 KiB
Plaintext
281 lines
7.8 KiB
Plaintext
let dtree, util, tree, layer;
|
|
let type = "";
|
|
let nodeIndex = []
|
|
let data = [];
|
|
let nodeList;
|
|
let isempty=false;
|
|
|
|
function setForm(node, puId, type) {
|
|
layui.extend({
|
|
dtree: '../../../../js/layui_dtree/dtree/dtree'
|
|
}).use(['util', 'dtree', 'layer'], function () {
|
|
util = layui.util;
|
|
dtree = layui.dtree;
|
|
layer = layui.layer;
|
|
nodeList = node.split(',');
|
|
util.event('lay-active', {
|
|
query: function () {
|
|
let obj =[]
|
|
$.each($(".unCheckBtn"), function (index, item) {
|
|
if (!isEmpty($(item).children().attr("value"))) {
|
|
let nodes = $(item).children().attr("value");
|
|
$.each(data, function (index, itemData) {
|
|
if (itemData.id === $(item).attr("value")) {
|
|
let parentId = itemData.id;
|
|
obj.push({
|
|
index:nodes,
|
|
parentIndex:parentId
|
|
})
|
|
}
|
|
})
|
|
}
|
|
});
|
|
getPuSet(puId, JSON.stringify(obj));
|
|
},
|
|
reset: function () {
|
|
},
|
|
});
|
|
getGroupNode(type);
|
|
tree = dtree.render({
|
|
elem: "#tree",
|
|
initLevel: 2,
|
|
data: [],
|
|
checkbar: true,//开启复选框
|
|
checkbarType: "only",
|
|
ficon: ['1'],
|
|
icon: "-1",
|
|
checkbarFun: checkBarFun,
|
|
done: function (res, $ul, first) {
|
|
$('li[data-index="1"] div:first').addClass('top');
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* 树单选框监听事件
|
|
* @type {{chooseDone: checkBarFun.chooseDone}}
|
|
*/
|
|
let checkBarFun = {
|
|
chooseDone: function (obj) {
|
|
if(obj.length==0){
|
|
console.log("取消了")
|
|
isempty=true;
|
|
}else{
|
|
isempty=false;
|
|
let span = $('li[value=' + obj[0].parentId + '] span');
|
|
span.text("(" + obj[0].context + ")");
|
|
span.attr("value", obj[0].nodeId);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 改变按钮样式
|
|
*/
|
|
function changItem(dom, parentDom, css) {
|
|
let item = $(dom);
|
|
if (!item.hasClass(css)) {
|
|
$.each(parentDom.children(), function (index, item) {
|
|
$(item).removeClass(css);
|
|
});
|
|
}
|
|
item.addClass(css);
|
|
}
|
|
|
|
|
|
/**
|
|
*获取分组节点
|
|
*/
|
|
function getGroupNode(type) {
|
|
let loadingMsg = layer.msg("数据加载中,请稍候...", {icon: 16, scrollbar: false, time: 0,});
|
|
Ajax().post({
|
|
headers: {
|
|
"encrypt": sm3(JSON.stringify({}))
|
|
},
|
|
url: dataUrl + 'proteam/pot/deviceMachine/getGroupNode',
|
|
data: {
|
|
"type": type
|
|
},
|
|
async: true,
|
|
success: function (data) {
|
|
console.log(data);
|
|
if (data.code === 200) {
|
|
traversalData(data.data);
|
|
} else {
|
|
|
|
}
|
|
}
|
|
});
|
|
layer.close(loadingMsg);
|
|
}
|
|
|
|
/***
|
|
* 转换数据
|
|
* @param data
|
|
*/
|
|
function traversalData(data) {
|
|
let treeType = [];
|
|
let treeData = [];
|
|
$.each(data, function (index, item) {
|
|
let obj = {
|
|
id: item.index,
|
|
title: item.name,
|
|
parentId: item.parentIndex,
|
|
children: []
|
|
}
|
|
if (obj.parentId === "0") {
|
|
treeType.push(obj);
|
|
} else {
|
|
obj.checkArr = nodeIndex.indexOf(Number(obj.id)) > -1 ? "1" : "0";
|
|
treeData.push(obj);
|
|
}
|
|
});
|
|
treeMenuList(treeType, treeData);
|
|
itemList(treeType, treeData);
|
|
}
|
|
|
|
/**
|
|
* 树类型
|
|
* @param treeType
|
|
* @param treeData
|
|
*/
|
|
function itemList(treeType, treeData) {
|
|
let html = "";
|
|
$.each(treeType, function (index, item) {
|
|
let value = "";
|
|
let name = "(未分配)";
|
|
$.each(item.children, function (indexData, itemData) {
|
|
if (itemData.parentId === item.id) {
|
|
if (nodeList.includes(itemData.id)) {
|
|
value = itemData.id;
|
|
name = "(" + itemData.title + ")";
|
|
itemData.checkArr = [{"type": "0", "checked": "1"}];
|
|
}
|
|
}
|
|
})
|
|
console.log(item);
|
|
if (index === 0) {
|
|
html += '<li class="item unCheckBtn checkBtn" value="' + item.id + '">' + item.title + '<span style="letter-spacing: 0;color: red;font-size: 12px;display: contents;" value ="' + value + '">' + name + '</span></li>'
|
|
//加载树
|
|
dtree.reload(tree, {
|
|
data: [item],
|
|
});
|
|
} else {
|
|
html += '<li class="item unCheckBtn" value="' + item.id + '">' + item.title + '<span style="letter-spacing: 0;color: red;font-size: 12px;display: contents;" value ="' + value + '">' + name + '</span></li>'
|
|
}
|
|
});
|
|
$('#itemList').html(html);
|
|
$('.unCheckBtn').click(function () {
|
|
if (type !== this.value) {
|
|
changItem(this, $(this).parent(), 'checkBtn');
|
|
type = this.value + "";
|
|
let value = $(this).children().attr("value")
|
|
$.each(treeType, function (index, item) {
|
|
if (item.id === type) {
|
|
$.each(item.children, function (indexChildren, itemChildren) {
|
|
itemChildren.checkArr = "0";
|
|
if (itemChildren.id === value) {
|
|
itemChildren.checkArr = "1";
|
|
}
|
|
});
|
|
//加载树
|
|
dtree.reload(tree, {
|
|
data: [item],
|
|
});
|
|
}
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 遍历数据
|
|
* @param treeType
|
|
* @param treeData
|
|
*/
|
|
function treeMenuList(treeType, treeData) {
|
|
$.each(treeType, function (indexType, itemType) {
|
|
let children = [];
|
|
$.each(treeData, function (indexData, itemData) {
|
|
if (itemType.id === itemData.parentId) {
|
|
children.push(itemData);
|
|
treeMenuList(itemData, treeData)
|
|
}
|
|
});
|
|
itemType.children = children;
|
|
});
|
|
|
|
data = treeType;
|
|
}
|
|
|
|
/**
|
|
* 查询设备
|
|
* @param puId
|
|
* @param type
|
|
*/
|
|
function getPu(puId, type) {
|
|
Ajax().post({
|
|
headers: {
|
|
"encrypt": sm3(JSON.stringify({puId: puId, all: type}))
|
|
},
|
|
url: dataUrl + 'proteam/pot/deviceMachine/getPu',
|
|
data: {puId: puId, all: type},
|
|
async: true,
|
|
success: function (data) {
|
|
if (data.code === 200) {
|
|
$.each(data.data, function (index, item) {
|
|
nodeIndex.push(index);
|
|
});
|
|
getGroupNode(type);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 分组节点添加设备
|
|
* @param puId
|
|
* @param nodes
|
|
* @param parentId
|
|
*/
|
|
function getPuSet(puId, node) {
|
|
let loadingMsg = layer.msg("数据加载中,请稍候...", {icon: 16, scrollbar: false, time: 0,});
|
|
Ajax().post({
|
|
headers: {
|
|
"encrypt": sm3(JSON.stringify({
|
|
puIds: puId,
|
|
node: node,
|
|
oldNode:isempty+''
|
|
}))
|
|
},
|
|
url: dataUrl + 'proteam/pot/deviceMachine/getPuSet',
|
|
data: {
|
|
puIds: puId,
|
|
node: node,
|
|
oldNode:isempty+''
|
|
},
|
|
async: true,
|
|
success: function (data) {
|
|
layer.close(loadingMsg);
|
|
if (data.code === 200) {
|
|
parent.layer.msg("分配成功", {icon: 1, scrollbar: false, time: 2000,});
|
|
closePage();
|
|
} else {
|
|
parent.layer.msg(data.msg, {icon: 0, scrollbar: false, time: 2000,});
|
|
}
|
|
},error: function (){
|
|
layer.close(loadingMsg);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
* 关闭页面
|
|
*/
|
|
function closePage() {
|
|
let index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
|
|
parent.layer.close(index); //再执行关闭
|
|
} |