目的:
1、前端输出树形数据
[
{
"id":1,
"title":"防辐射技术有限公司",
"spread":true,
"children":[
{
"id":2,
"title":"财务部",
"spread":true,
"children":[
{
"id":3,
"title":"生产部",
"spread":true,
"children":[
]
}
]
},
{
"id":4,
"title":"销售部",
"spread":true,
"children":[
{
"id":31,
"title":"销售王牌",
"spread":true,
"children":[
]
},
{
"id":32,
"title":"销售1+1",
"spread":true,
"children":[
{
"id":33,
"title":"销售1+1+1",
"spread":true,
"children":[
]
}
]
}
]
},
{
"id":5,
"title":"行政部",
"spread":true,
"children":[
{
"id":6,
"title":"人事部",
"spread":true,
"children":[
]
}
]
},
{
"id":7,
"title":"研发中心",
"spread":true,
"children":[
{
"id":8,
"title":"产品部",
"spread":true,
"children":[
]
},
{
"id":9,
"title":"研发部",
"spread":true,
"children":[
]
},
{
"id":10,
"title":"硬件部",
"spread":true,
"children":[
]
}
]
}
]
}
]
2、后端输出List数据
{"data": [
{
"DeptID": 1,
"DeptNo": "00001",
"DeptName": "防辐射技术有限公司",
"FatherID": 0
},
{
"DeptID": 2,
"DeptNo": "00002",
"DeptName": "财务部",
"FatherID": 1
},
{
"DeptID": 3,
"DeptNo": "00003",
"DeptName": "生产部",
"FatherID": 2
},
{
"DeptID": 4,
"DeptNo": "00004",
"DeptName": "销售部",
"FatherID": 1
},
{
"DeptID": 5,
"DeptNo": "00005",
"DeptName": "行政部",
"FatherID": 1
},
{
"DeptID": 6,
"DeptNo": "00006",
"DeptName": "人事部",
"FatherID": 5
},
{
"DeptID": 7,
"DeptNo": "00007",
"DeptName": "研发中心",
"FatherID": 1
},
{
"DeptID": 8,
"DeptNo": "00008",
"DeptName": "产品部",
"FatherID": 7
},
{
"DeptID": 9,
"DeptNo": "00009",
"DeptName": "研发部",
"FatherID": 7
},
{
"DeptID": 10,
"DeptNo": "00010",
"DeptName": "硬件部",
"FatherID": 7
},
{
"DeptID": 31,
"DeptNo": null,
"DeptName": "销售王牌",
"FatherID": 4
},
{
"DeptID": 32,
"DeptNo": null,
"DeptName": "销售1+1",
"FatherID": 4
},
{
"DeptID": 33,
"DeptNo": null,
"DeptName": "销售1+1+1",
"FatherID": 32
}
]
}
过程:
1、转成树型数据方法
// List转成Tree
function toTreeData(data, attributes) {
// resData 是从后端传过来的数据
let resData = data;
let tree = [];
for (let i = 0; i < resData.length; i++) {
// FatherID 是父级ID,对应自己的数据 rootID 是配置中的父级ID
if (resData[i].FatherID === attributes['rootId']) {
let obj = {
id: resData[i][attributes.id],
title: resData[i][attributes.name],
spread: true,
children: []
};
tree.push(obj);
resData.splice(i, 1);
i--;
}
}
run(tree);
function run(chiArr) {
if (resData.length !== 0) {
for (let i = 0; i < chiArr.length; i++) {
for (let j = 0; j < resData.length; j++) {
if (chiArr[i].id === resData[j][attributes.parentId]) {
let obj = {
id: resData[j][attributes.id],
title: resData[j][attributes.name],
spread: true,
children: []
};
chiArr[i].children.push(obj);
resData.splice(j, 1);
j--;
}
}
run(chiArr[i].children);
}
}
}
return tree;
}
2、调用方法( PS:结合过程的1和2就可以转数据格式)
// 属性配置信息
var attributes = {
id: 'DeptID', // 对应list 数据中的ID
parentId: 'FatherID',// 对应list 数据中的父ID
name: 'DeptName', // 对应list 数据中的名称
rootId: 0 // 自定义最外层的父ID
};
//赋值
console.log('转成树形数据',JSON.stringify(toTreeData(data, attributes)));
3、将Ajax封装成方法,便于其他地方调用( PS:这是个人文件需要写的方法)
// 调用方法
var getDept = function () {
var item = '';
$.ajax({
url: 'http://xxx.xx.xx.xx:xxxx/api/dep'
, method: 'GET'
, async: false
, datatype: 'json'
, success: function (res) {
if (res.code === 0) {
var data = res.data;
// 属性配置信息
var attributes = {
id: 'DeptID',
parentId: 'FatherID',
name: 'DeptName',
rootId: 0
};
//赋值
console.log('转成树形数据',JSON.stringify(toTreeData(data, attributes)));
item=toTreeData(data, attributes);
} else {
layer.msg(res.msg, {icon: 5, time: 2000});
location.href = 'views/login.html'; //后台主页
}
}, error: function (res) {
layer.msg('出错了', res)
}
});
return item;
};
4、调用方法
<script>
//自定义全局变量
var tmp_data; // 存放页面加载时数据
var tmp_dept_data = []; // 上级的id和title
var add_id = '';// 用于存放添加后返回的ID
//打开界面则加载,获取所有数据
$(document).ready(function () {
tmp_data = getDept();
});
</script>
5、layui tree组件的重载
tree.render({
elem: '#test9'
, data: tmp_data
// 默认节点打开状态
, spread: true
, id: 'DeptName'
, edit: ['add', 'update', 'del'] //操作节点的图标
, operate: function (obj) {
var type = obj.type; //得到操作类型:add、edit、del
var data = obj.data; //得到当前节点的数据
var elem = obj.elem; //得到当前节点元素
//Ajax 操作
tmp_dept_data.push({'id': data.id}); //得到节点索引
tmp_dept_data.push({'title': data.title}); //得到节点索引
if (type === 'add') { //增加节点
admin.req({
url: 'http://xxx.xx.xx.xx:xxxx/api/dep'
, method: 'POST'
, datatype: 'json'
, data: {
'access_token': token,
'id': data.id,
'title': '未命名',
}
, success: function (res) {
layer.msg(res.msg, {offset: '15px', icon: 1, time: 1000});
add_id = res.data;
// 调用重载tree方法
updateTree();
}
});
return add_id;
} else if (type === 'update') { //修改节点
admin.req({
url: 'http://xxx.xx.xx.xx:xxxx/api/dep'
, method: 'PUT'
, datatype: 'json'
, data: {
'access_token': token,,
'id': data.id,
'title': data.title,
}
, success: function (res) {
layer.msg(res.msg, {offset: '15px', icon: 1, time: 1000});
}
});
// console.log('这是打印看看能获取什么', obj.data, JSON.stringify(tmp_dept_data));
} else if (type === 'del') { //删除节点
}
tmp_dept_data.splice(0, tmp_dept_data.length);//清空数组
}
});
// 重载tree 组件
var updateTree = function () {
// 调用获取参数方法
data = getDept();
//可以重载所有基础参数
tree.reload('DeptName', {
elem: '#test9'
, data: data
// 默认节点打开状态
, spread: true
, edit: ['add', 'update', 'del'] //操作节点的图标
});
}
344




被折叠的 条评论
为什么被折叠?



