tki-tree 树组件控制默认展开第几层数据

树形选择器增强版,支持多选、单选、父级选择,Picker形式 - DCloud 插件市场
tki-tree没有配置项控制展示到第几层数据,所以需要我们自己扩展
我在组件里扩展了两个配置项
1.openRank 控制展开到树的第几层

showAllFlod: {
// 是否展开所有节点
	type: Boolean,
	default: false
}

2.showAllFlod 全部展开

openRank: {
	type: [Number, String],
}

然后处理方法中的数据

//扁平化树结构
_renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
	let openRank = this.openRank ? (rank < this.openRank) : false; // 如果这两个参数都没设置的话就全都收起来
	let showRank = this.openRank ? (rank < this.openRank - 1) : false;// 如果设置了层级,那么需要让层级的上一级箭头是收起状态,否则需要通过点击触发的事件才能把数据展示出来
	if (this.showAllFlod) {
		openRank = showRank = true;
	}
	list.forEach(item => {
		this.treeList.push({
			id: item[this.idKey],
			name: item[this.rangeKey],
			source: item,
			disabled: item.disabled,
			parentId, // 父级id数组
			parents, // 父级id数组
			rank, // 层级
			showChild: Boolean(showRank), //箭头怎么展示
			open: Boolean(showRank), //是否打开
			show: Boolean(openRank || rank === 0), // 自身是否显示
			hideArr: [],
			orChecked: item.checked ? item.checked : false,
			checked: item.checked ? item.checked : false,
			childNum: 0
		})
		
		if (Array.isArray(item.children) && item.children.length > 0) {
			let parentid = [...parentId],
				parentArr = [...parents];
			delete parentArr.children
			parentid.push(item[this.idKey]);
			parentArr.push({
				[this.idKey]: item[this.idKey],
				[this.rangeKey]: item[this.rangeKey]
			})
			// lazy
			if(!this.lazy) {
				this._renderTreeList(item.children, rank + 1, parentid, parentArr)
			}
		} else {
			this.treeList[this.treeList.length - 1].lastRank = true;
		}
	})
},

猜你喜欢

转载自blog.csdn.net/m0_46693606/article/details/127067063