element select + tree

element select + tree的使用

复制代码
<template slot="action1" slot-scope="text, record, index">
	<el-select v-model="record.tagValue" multiple placeholder="请选择"
		:filter-method="(e) => filterTree(e, index)" filterable 
		@remove-tag="(e) => removeTag(e, index)">
		<el-option>
			<template slot="default">
				<div>
					<el-tree :data="record.option" show-checkbox node-key="id"
						:props="defaultProps" :ref="'tree' + index"
						:filter-node-method="filterNode"
						@check-change="(data, isSelect, childSelect) => handleCurrentChange(data, isSelect, childSelect, index)">
						<template #default="{ node }">
							<div v-html='highlightName(node,searchVal)'></div>
						</template>
					</el-tree>
				</div>
			</template>
		</el-option>
	</el-select>
</template>

// 搜索
filterNode(value, data) {
	if (!value) return true;
	return data.name.indexOf(value) !== -1;
},
filterTree(val, index) {
	this.searchVal = val
	let tree = 'tree' + index
	this.$refs[tree].filter(this.searchVal);
},
// 搜索字体高亮
highlightName (item,val) {
	let textHtml = "";
	if (val) {
		let highlightTextList = val.split("#~~~~");
		let newName = item.data.name;
		highlightTextList.forEach((text) => {
		if (item.data.name.indexOf(text) > -1) {
			newName = newName.replaceAll(
			text,
			`<span class="minddleContent">${text}</span>`
			);
		}
		});
		textHtml += newName;
	} else {
		textHtml += item.data.name;
	}
	return textHtml;
},
// 获取知识树选中值
handleCurrentChange(data, isSelect, childSelect, index) {
	let tree = 'tree' + index
	let nodes = this.$refs[tree].getCheckedNodes()
	let arrId = []
	if (nodes.length > 0) {
		nodes.map(item => {
			arrId.push(item.id)
		})
	}
	let listData = this.dataSource[index].option
	let listText = this.getStructuredKeys(arrId, nodes, listData)
	this.dataSource[index].tagValue = listText.map(item => {
		return item.name
	})
},
// 知识树选中值递归,选中子集只展示子集,父级全选只展示父级
getStructuredKeys(keys, list, listData) {
	const result = [];
	const processNode = (nodes) => {
		nodes.forEach((node) => {
			if (keys.includes(node.id)) {
				result.push(node);
			} else {
				if (node.children) {
					const childKeys = this.getStructuredKeys(keys, list, node.children);
					console.log(childKeys)
					if (childKeys.length > 0) {
						result.push(...childKeys);
					}
				}
			}
		});
	};
	processNode(listData);
	return this.sortSelectedItems(result, list);
},
// 选中项排序
sortSelectedItems(result, list) {
	const SelectedItems = result.map(v => v.id)
	const sortList = [];
	list.filter(item => {
		if (SelectedItems.includes(item.id)) {
			sortList.push(item);
		}
	})
	return sortList
},
// 移除select-tree选中值
removeTag(tag, index) {
	let tree = 'tree' + index
	const checkNodes = this.$refs[tree].getCheckedNodes();
	const newCheckNodes = checkNodes.filter(item => {
		return item.name !== tag
	})
	const removeIds = [];
	const removeNodes = checkNodes.filter(item => {
		return item.name === tag
	});
	this.getNodeChildIds(removeNodes, removeIds)
	const checkIds = []
	newCheckNodes.forEach(item => {
		if (!removeIds.includes(item.id)) {
			checkIds.push(item.id);
		}
	})
	this.$refs[tree].setCheckedKeys(checkIds)
},
// 移除选中递归
getNodeChildIds(nodes, result) {
	nodes.forEach(item => {
		result.push(item.id)
		if (item.children && item.children.length > 0) {
			this.getNodeChildIds(item.children, result)
		}
	})
},
相关推荐
BLOOM1 分钟前
新一代前端数据mock工具Data Faker
前端·javascript
UIUV2 分钟前
微信小程序开发学习笔记:从架构到实战
前端·javascript·前端框架
程序猿_极客4 分钟前
JavaScript的Web APIs 入门到实战(day2):事件监听与交互实现,轻松实现网页交互效果(附练习巩固)
开发语言·前端·javascript·学习笔记·web apis 入门到实战
Mintopia22 分钟前
🚀 一文看懂 “Next.js 全栈 + 微服务 + GraphQL” 的整体样貌
前端·javascript·全栈
Mintopia24 分钟前
🧬 医疗Web场景下,AIGC的辅助诊断技术边界与伦理
前端·javascript·aigc
半桶水专家28 分钟前
父子组件通信详解
开发语言·前端·javascript
Watermelo61731 分钟前
从vw/h到clamp(),前端响应式设计的痛点与进化
前端·javascript·css·算法·css3·用户界面·用户体验
Moment37 分钟前
快到  2026  年了:为什么我们还在争论  CSS 和 Tailwind?
前端·javascript·css
梵得儿SHI1 小时前
Vue 核心语法详解:模板语法中的绑定表达式与过滤器(附 Vue3 替代方案)
前端·javascript·vue.js·插值语法·vue模板语法·绑定表达式·过滤器机制
江城开朗的豌豆1 小时前
TypeScript枚举:让你的代码更有"选择权"
前端·javascript