diff --git a/Web/src/components/yoTreeLayout/index.js b/Web/src/components/yoTreeLayout/index.js index 9078cc8..b66aa27 100644 --- a/Web/src/components/yoTreeLayout/index.js +++ b/Web/src/components/yoTreeLayout/index.js @@ -1,5 +1,4 @@ import Swiper from 'swiper' -import { Empty } from 'ant-design-vue' let timer, swiper @@ -14,6 +13,17 @@ export default { default: false, type: Boolean }, + + replaceFields: { + default() { + return { + value: 'id', + title: 'title', + children: 'children' + } + }, + type: Object + } }, data() { @@ -68,15 +78,15 @@ export default { for (let i = 0; i < data.length; i++) { const item = data[i] - path[level] = item.title + path[level] = item[this.replaceFields.title] - if (item.id === this.selectedKeys[0]) { + if (item[this.replaceFields.value] === this.selectedKeys[0]) { path.length = level + 1 return true } - if (item.children && item.children.length) { - const found = findPath(item.children, level + 1) + if (item[this.replaceFields.children] && item[this.replaceFields.children].length) { + const found = findPath(item[this.replaceFields.children], level + 1) if (found) { return true } @@ -130,7 +140,7 @@ export default { onSearch(value) { const expandedKeys = this.list .map(p => { - if (p.title.indexOf(value) > -1) { + if (p[this.replaceFields.title].indexOf(value) > -1) { return this.getParentKey(p.key, this.data) } return null @@ -151,7 +161,7 @@ export default { const selectedIds = [] selectedKeys.forEach(p => { const data = this.list.find(m => m.key === p) - selectedIds.push(data.id) + selectedIds.push(data[this.replaceFields.value]) }) this.selectedKeys = selectedIds this.$emit('select', selectedIds) @@ -172,8 +182,8 @@ export default { n[n.length - 1] = i p.key = n.join('-') p.scopedSlots = { title: 'title' } - if (p.children) { - this.generateKey(p.children, Object.assign([], n)) + if (p[this.replaceFields.children]) { + this.generateKey(p[this.replaceFields.children], Object.assign([], n)) } }) return data @@ -182,10 +192,14 @@ export default { generateList(data) { // 这里获取不到Key for (let i = 0; i < data.length; i++) { - const { key, id, title, children } = data[i] - this.list.push({ key, id, title }) - if (children) { - this.generateList(children) + const { key } = data[i] + this.list.push({ + key, + id: data[i][this.replaceFields.value], + title: data[i][this.replaceFields.title] + }) + if (data[i][this.replaceFields.children]) { + this.generateList(data[i][this.replaceFields.children]) } } }, @@ -194,11 +208,11 @@ export default { let parentKey; for (let i = 0; i < tree.length; i++) { const node = tree[i] - if (node.children) { - if (node.children.some(item => item.key === key)) { + if (node[this.replaceFields.children]) { + if (node[this.replaceFields.children].some(item => item.key === key)) { parentKey = node.key - } else if (this.getParentKey(key, node.children)) { - parentKey = this.getParentKey(key, node.children) + } else if (this.getParentKey(key, node[this.replaceFields.children])) { + parentKey = this.getParentKey(key, node[this.replaceFields.children]) } } }