From 04af7d3eb75ce3f3f6be3ce84c46af57bae8ab7b Mon Sep 17 00:00:00 2001
From: liuyg <376836862@qq.com>
Date: Sat, 05 Jun 2021 18:06:14 +0800
Subject: [PATCH] 左侧树

---
 widgets/LeftNavigationBar/Widget.js |  193 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 155 insertions(+), 38 deletions(-)

diff --git a/widgets/LeftNavigationBar/Widget.js b/widgets/LeftNavigationBar/Widget.js
index 87a0bf3..5f0382a 100644
--- a/widgets/LeftNavigationBar/Widget.js
+++ b/widgets/LeftNavigationBar/Widget.js
@@ -29,14 +29,63 @@
 			layers: {},
 			startup: function () {
 				// 暴露在外的接口
-				this.begin(this.data);
-				$('#left-navigation-bar-main-search').click(()=>{
-					var val = $('#left-navigation-bar-main-input').val();
-					if(val){
-						console.log(val);
+				// this.begin(this.data);
+				var that = this;
+				this.getData();
 
-					}else{
-						
+				$('.left-navigation-bar-but').click(() => {
+					this.changeImg = !this.changeImg;
+					if (this.changeImg) {
+						$('.l-n-b-b-img').attr("src", "../../images/addL/xzuo.png");//打开
+						$('.jimu-widget-LeftNavigationBar').css({ 'left': '0px' });
+						$('.jimu-widget-instructions').css({ 'left': '260px' });
+					} else {
+						$('.l-n-b-b-img').attr("src", "../../images/addL/xyou.png");//关闭
+						$('.jimu-widget-LeftNavigationBar').css({ 'left': '-250px' });
+						$('.jimu-widget-instructions').css({ 'left': '10px' });
+					}
+				})
+
+
+				$('.jimu-widget-instructions').css({ 'left': '260px' });
+				$('#left-navigation-bar-main-search').click(() => {//搜索事件
+					var val = $('#left-navigation-bar-main-input').val();
+					if (val) {
+						var clearOur = () => {
+							let dom = $(`[data-id]`);
+							// console.log(dom);
+							dom.removeClass('layui-tree-spread');
+							let domDiv = dom.find('.layui-tree-pack');
+							domDiv.css({ 'display': 'none' })
+						}
+							, a
+							, useit = (d) => {
+								for (var k in d) {
+									if (d[k].who == 'father') {
+										that.father = d[k].id;
+									}
+									if (d[k].title.indexOf(val) != -1) {//选中id
+										a = '' + d[k].id;
+										// var b = a.split('');
+										// for (var i in b) {
+										let id = that.father;//选中项的所有父级id
+										// 	// console.log(id)
+										let dom = $(`[data-id='${id}']`);
+										dom.addClass('layui-tree-spread');
+										let domDiv = dom.find('.layui-tree-pack').eq(0);
+										domDiv.css({ 'display': 'block' })
+										// }
+										console.log(a, that.father)
+									}
+									if (d[k].children) {
+										useit(d[k].children);
+									}
+								}
+							};
+						clearOur();
+						useit(that.data);
+					} else {
+
 					}
 				})
 			},
@@ -63,54 +112,122 @@
 			resize: function () {
 
 			},
-			begin: function (data) {
-				var that = this;
-				$('.left-navigation-bar-but').click(() => {
-					that.changeImg = !that.changeImg;
-					if (that.changeImg) {
-						$('.l-n-b-b-img').attr("src", "../../images/addL/xzuo.png");
-					} else {
-						$('.l-n-b-b-img').attr("src", "../../images/addL/xyou.png");
+			getData: function () {
+				var url = 'http://171.34.76.171:8880/zhsd-wetResource/GeoData/queryGeoDataTree'
+					, that = this;
+
+				axios.get(url, {
+					headers: {
+						"token": 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJlY2hpc2FuIiwic3ViIjoiYWRtaW4iLCJpYXQiOjE2MjI4Nzc0MTIsImV4cCI6MTYyNTQ2OTQxMn0.f2GhYYINfYgSbyKTqqGluyU3rdy_TT8mf15iLILS1l2EUSye_h9O6CSUVWpLYMrdrjzqDWwrbROxfhy-8ly0uA'
+					}
+				}).then((res) => {
+					if (res.data.code == '200') {
+						that.changeData(res.data.data);//重写数据
 					}
 				})
+			},
+			changeData: function (val) {//重写数据
+				var data = [],
+					i = 1,
+					inits = (d, childData) => {
+						var b = childData || [];
+						for (var k in d) {
+
+							let a = {
+								title: d[k].name,
+								// id: d[k].id,
+								// field: String(d[k].id),
+								id: i,
+								field: String(i)
+							}
+							b.push(a);
+							i++;
+							if (d[k].children.length != 0) {
+								b[k].children = [];
+								b[k].who = 'father';
+								inits(d[k].children, b[k].children)
+							}
+						}
+
+					};
+				inits(val, data);
+				// console.log(data)
+
+				// var i = 0,
+				// 	ccinit = (a) => {
+				// 		i++;
+				// 		for (let k in a) {
+				// 			if (a[k].children) {
+				// 				a[k].who = i;
+				// 				// console.log(a[k].id)
+				// 				ccinit(a[k].children)
+				// 			}
+				// 		}
+				// 	};
+				// ccinit(this.data);
+				// console.log(this.data)
+				this.data = data;
+				this.begin(this.data);
+			},
+			begin: function (data) {
+				var that = this;
 
 				layui.use(function () {
 					var tree = layui.tree
 						, layer = layui.layer
-						, util = layui.util;
+						, util = layui.util
+						, $ = layui.$;
 					tree.render({
 						elem: '#test1' //默认是点击节点可进行收缩
 						, data: data
+						, id: 'demoId1'
 						, showCheckbox: true  //是否显示复选框
 						, click: function (obj) {
 							var data = obj.data;  //获取当前点击的节点数据
-							layer.msg('状态:' + obj.state + '<br>节点数据:' + JSON.stringify(data));
+							// layer.msg('状态:' + obj.state + '<br>节点数据:' + JSON.stringify(data));
+							console.log(obj);
+							// 树点击三角形样式修改
+							// var ot = $('.layui-tree-iconArrow');
+							// ot.removeClass("left-navigation-bar-main-down");
+							// var jt = $(`[data-id='${data.id}']`).find('.layui-tree-iconArrow').eq(0);
+							// jt.addClass('left-navigation-bar-main-down');
+							// 树点击样式修改
+							// var ot = $('.layui-tree-entry');
+							// ot.removeClass("left-navigation-bar-main-current");
+							// var jh = $(`[data-id='${data.id}']`).find('.layui-tree-entry').eq(0);
+							// jh.addClass('left-navigation-bar-main-current');
 						}
 						, showLine: false  //是否开启连接线
+						, oncheck: function (obj) {
+							console.log(obj.data.id, '--', obj.checked); //得到当前点击的节点数据
+							// console.log(obj.checked); //得到当前节点的展开状态:open、close、normal
+							// console.log(obj.elem); //得到当前节点元素
+						}
 					});
 
-					// util.event('lay-demo', {
-					// 	getChecked: function (othis) {
-					// 		var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
+					//按钮事件
+					util.event('lay-demo', {
+						getChecked: function (othis) {
+							var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
 
-					// 		layer.alert(JSON.stringify(checkedData), { shade: 0 });
-					// 		console.log(checkedData);
-					// 	}
-					// 	, setChecked: function () {
-					// 		tree.setChecked('demoId1', [12, 16]); //勾选指定节点
-					// 	}
-					// 	, reload: function () {
-					// 		//重载实例
-					// 		tree.reload('demoId1', {
+							layer.alert(JSON.stringify(checkedData), { shade: 0 });
+							console.log(checkedData);
+						}
+						, setChecked: function () {
+							tree.setChecked('demoId1', [12, 114]); //勾选指定节点
+						}
+						, reload: function () {
+							//重载实例
+							tree.reload('demoId1', {
 
-					// 		});
+							});
 
-					// 	}
-					// });
+						}
+					});
 				})
-
 			},
 			changeImg: true,
+			father: [],
 			data: [{
 				title: '采集站点'
 				, id: 1
@@ -247,7 +364,7 @@
 					, id: 22
 					, field: '2-2'
 					, children: [{
-						title: '北深渊'
+						title: '北深渊4'
 						, id: 221
 						, field: '2-2-1'
 					}, {
@@ -289,7 +406,7 @@
 					, id: 24
 					, field: '2-4'
 					, children: [{
-						title: '北深渊'
+						title: '北深渊5'
 						, id: 241
 						, field: '2-4-1'
 					}, {
@@ -317,7 +434,7 @@
 					, id: 31
 					, field: '3-1'
 					, children: [{
-						title: '北深渊'
+						title: '北深渊2'
 						, id: 311
 						, field: '3-1-1'
 					}, {
@@ -338,7 +455,7 @@
 					, id: 32
 					, field: '3-2'
 					, children: [{
-						title: '北深渊'
+						title: '北深渊3'
 						, id: 321
 						, field: '3-2-1'
 					}, {
@@ -380,7 +497,7 @@
 					, id: 34
 					, field: '3-4'
 					, children: [{
-						title: '北深渊'
+						title: '北深渊1'
 						, id: 341
 						, field: '3-4-1'
 					}, {

--
Gitblit v1.9.3