当前位置: 首页 > news >正文

el-tree选中数据重组成树

vue+element-ui 实现el-tree选择重新生成一个已选中的值组成新的数据树,效果如下

<template>
	<div  class="flex">
		<el-tree class="tree-row" :data="list" ref="tree" :props="{children: 'children', label: 'itemCategoryName'}" show-checkbox node-key="id" default-expand-all  @check-change="getChecked">
		</el-tree>
		<el-tree class="tree-row" :data="selectedTreeData"  :props="{children: 'children', label: 'itemCategoryName'}"  node-key="id" default-expand-all   :expand-on-click-node="false">
			<span class="custom-tree-node" slot-scope="{ node, data }">
				<span>{{ node.label }}</span>
				<span>
				<el-button
					type="text"
					size="mini"
					@click="() => remove(node, data)">
					Delete
				</el-button>
				</span>
			</span>
		</el-tree>
	</div>
</template>
<script>
export default {
	data() {
		return {
			list: [
				{
					"id": "4100000000000071",
					"itemCategoryCode": "001",
					"itemCategoryName": "白酒类",
					children: [{
						"id": "41000020000000071",
						"itemCategoryCode": "0012",
						"itemCategoryName": "白酒类2",
						children: [{
							"id": "41000040000000071",
							"itemCategoryCode": "0014",
							"itemCategoryName": "白酒类4",
						},
						{
							"id": "41000050000000071",
							"itemCategoryCode": "0015",
							"itemCategoryName": "白酒类5",
						},
						{
							"id": "41000060000000071",
							"itemCategoryCode": "0016",
							"itemCategoryName": "白酒类6",
						}]
					},
					{
						"id": "41000030000000071",
						"itemCategoryCode": "0013",
						"itemCategoryName": "白酒类3",
					}]
				},
			],
			// selectedIds: ['41000040000000071', "41000060000000071"],
			selectedTreeData:[]
		}
	},
	created() {
	},
	methods: {
		getChecked() {
			this.selectedIds = this.$refs.tree.getCheckedKeys(true);
            this.extractSubtrees();
		},
		extractSubtrees() {
			const subtrees = [];
			for (const id of this.selectedIds) {
				const subtree = this.extractSubtree(this.list, id);
				if (subtree) {
					subtrees.push(subtree);
				}
			}
			this.selectedTreeData = this.mergeSubtrees(subtrees);
		},
		extractSubtree(treeData, targetId) {
			for (const node of treeData) {
				if (node.id === targetId) {
					// return this.cloneAndFilterChildren(node); 可以用这个也可以用下边的方式
					return { ...node };
				}
				if (node.children) {
					const subtree = this.extractSubtree(node.children, targetId);
					if (subtree) {
						// return this.cloneAndFilterChildren({ ...node, children: [subtree] });可以用这个也可以用下边的方式
						return { ...node, children: [subtree] };
					}
				}
			}
			return null;
		},
		cloneAndFilterChildren(node) {
			if (!node.children || node.children.length === 0) {
				return { ...node, children: [] };
			}
			const filteredChildren = node.children
				.map(child => this.cloneAndFilterChildren(child))
				.filter(child => this.selectedIds.includes(child.id) || child.children.length > 0);
			return { ...node, children: filteredChildren };
		},
		mergeSubtrees(subtrees) {
			this.mergedTree = [];
			const idMap = {};

			for (const subtree of subtrees) {
				this.addSubtreeToMergedTree(this.mergedTree, subtree, idMap);
			}

			return this.mergedTree;
		},
		addSubtreeToMergedTree(mergedTree, subtree, idMap) {
			if (!subtree) return;

			const node = { ...subtree, children: [] };

			if (!idMap[node.id]) {
				idMap[node.id] = node;
				mergedTree.push(node);
			} else {
				// Object.assign(idMap[node.id], node);
			}

			if (subtree.children) {
				for (const child of subtree.children) {
					this.addSubtreeToMergedTree(mergedTree[mergedTree.length-1].children, child, idMap);
				}
			}
		},
		remove(node, data) {
			let keys = [];
			this.getRemovedIds(data,keys);
			keys.map(item => {
				this.selectedIds.splice(this.selectedIds.indexOf(item), 1);
			})
			this.$refs.tree.setCheckedKeys(this.selectedIds);
		},
		getRemovedIds(data,keys) {
			if (data.children && data.children.length > 0) {
				data.children.map(item => {
					this.getRemovedIds(item,keys);
				})
			} else {
				keys.push(data.id);
			}
		},
	}
}

</script>
<style lang="less" scoped>
.flex {
	display: flex;
	justify-content: space-evenly;
}
.tree-row{
	min-width: 200px;
}
.custom-tree-node{
	display: flex;
	flex:1;
	justify-content: space-around;
}
</style>

相关文章:

  • s1K 数据集:是一个用于提升语言模型推理能力的高质量数据集。包含 1,000 个问题,每个问题都配有详细的 推理路径 和 答案。
  • 共同性思考:数据标注研究与数据标注工作者 工作范式思考
  • MicroPython+ESP32:一.开发环境的搭建
  • 使用synchronized解决高并发场景
  • 线性模型 - 二分类问题的损失函数
  • 基于JAVA的流浪动物救助平台的设计与实现(LW+源码+讲解)
  • 14.学成在线开发小结
  • Python异步编程优雅实践
  • 安装 Docker Desktop 修改默认安装目录到指定目录
  • 机械学习基础-6.更多分类-数据建模与机械智能课程自留
  • Android Studio安装配置及运行
  • mysql 学习16 视图,存储过程,存储函数,触发器
  • 基础排序算法
  • js第十二题
  • git版本控制工具介绍
  • Python简单语法
  • 从二叉树到红黑树
  • 【R语言】GitHub Copilot安装-待解决
  • 6.2.2 E-R模型
  • 系统思考—系统结构
  • adobe dreamweaver cs6/陕西seo主管
  • 韩国网站怎么打开/山东网页定制
  • ps做字幕模板下载网站有哪些/如何做营销策划方案
  • 中国网站建设第一品牌/软文推广是什么
  • 58同城给做网站/互联网精准营销
  • 合肥web网站建设/seo应用领域有哪些