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)
		}
	})
},
相关推荐
Dread_lxy10 分钟前
vue 依赖注入(Provide、Inject )和混入(mixins)
前端·javascript·vue.js
奔跑草-1 小时前
【前端】深入浅出 - TypeScript 的详细讲解
前端·javascript·react.js·typescript
羡与1 小时前
echarts-gl 3D柱状图配置
前端·javascript·echarts
前端郭德纲2 小时前
浏览器是加载ES6模块的?
javascript·算法
JerryXZR2 小时前
JavaScript核心编程 - 原型链 作用域 与 执行上下文
开发语言·javascript·原型模式
帅帅哥的兜兜2 小时前
CSS:导航栏三角箭头
javascript·css3
渗透测试老鸟-九青2 小时前
通过投毒Bingbot索引挖掘必应中的存储型XSS
服务器·前端·javascript·安全·web安全·缓存·xss
龙猫蓝图2 小时前
vue el-date-picker 日期选择器禁用失效问题
前端·javascript·vue.js
夜色呦2 小时前
掌握ECMAScript模块化:构建高效JavaScript应用
前端·javascript·ecmascript
peachSoda72 小时前
随手记:简单实现纯前端文件导出(XLSX)
前端·javascript·vue.js