首页 / 浏览问题 / 其他 / 问题详情
ztree树的复选框实现二维地图图层显隐控制
2EXP 2021年08月10日
loadTree2D: function () {
					$.ajax({
						type: 'GET',
						url: "js/data.json",
						success: function (rs) {
							let setting = {
								data: {
									simpleData: {
										enable: true,
										idKey: "id",
										pIdKey: "parentId",
										rootPId: 0
									},
									key: {
										checked: "checked",
										name: 'name'
									}
								},
								view: {
									showLine: true, // 是否显示节点之间的连线
								},
								check: {
									enable: true,
									chkStyle: "checkbox",
									chkboxType: {
										"Y": "ps",
										"N": "ps"
									}
								},
								callback: {
									onClick: function (event, treeId, treeNode) {
										treeObj.checkNode(treeNode, !treeNode.checked, true);
										that.setLayerStatus2D(treeNode);

									},
									onCheck: function (event, treeId, treeNode) {
										that.setLayerStatus2D(treeNode);
									}
								}
							};
							let treeObj = $.fn.zTree.init($("#layerTree2D"), setting, rs);
							let nodes = treeObj.getNodes();
							treeObj.checkAllNodes(true);
							if (nodes.length > 0) {
								for (let i = 0; i < nodes.length; i++) {
									treeObj.expandNode(nodes[i], true, false, false); //默认展开第一级节点
								}
							}
						}
					});
				},
setLayerStatus2D: function (treeNode) {
					let treeObj = $.fn.zTree.getZTreeObj("layerTree2D");
					let layerName = treeNode.layerName;
					let resultData = [];
					for (var i = 0; i < map.layers.length; i++) {
						resultData.push(map.layers[i].layerName);
					}
					if (treeNode.layerName == undefined) {
						let nodes = treeObj.getNodes();
						let childNodes = [];
						for (let item in nodes) {
							for (let index in nodes[item].children) {
								childNodes.push(nodes[item].children[index]);
							}
						}
						for (var j = 0; j < childNodes.length; j++) {
							let layerNameChecked;
							for (var k = 0; k < resultData.length; k++) {
								if (layerNameChecked == resultData[k]) {
									if (childNodes[j].getCheckStatus().checked == true) {
										map.addLayer(map.layers[i]);
									} else {
										map.removelayer(map.layers[i]);
									}
								}
							}
						}
					} else {
						for (let i = 0, len = resultData.length; i < len; i++) {
							if (layerName == resultData[i]) {
								if (treeNode.getCheckStatus().checked == true) {
									map.addLayer(map.layers[i]);
								} else {
									map.removelayer(map.layers[i]);
								}
							}
						}
					}
				},	

1个回答

杨雪
1
1,010EXP 2021年08月10日
好的,我用的是MapboxGL,不知道这样可以吗?
...