<Tree ref="tree" :data="treeContents" :render="renderContent"></Tree>
1. 先来说说数据
iview的Tree组件的data数据是树结构的,例如下面代码所示:
export default {
data () {
return {
treeContents: [
{
title: '一级目录',
expand: true,
children: [
{
title: '二级目录1',
expand: true,
children: [
{
title: '三级目录1'
},
{
title: '三级目录2'
}
]
},
{
title: '二级目录2',
expand: true,
children: [
{
title: '三级目录1'
},
{
title: '三级目录2'
}
]
}
]
}
]
}
}
但是,如果后端提供的数据,不是上面的树型结构数据时,就需要前端对后端提供的数据进行“树型处理”。用到数据结构中树的遍历相关知识。this.convertTree(data.root, this.map);对后端提供的数据进行“树型处理”。比如要构造处下面这种数据形式的tree目录:
export default {
data () {
return {
treeContents: [
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '一级目录',
expand: true,
children: [
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '二级目录1',
expand: true,
children: [
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '三级目录1',
expand: true,
children: []
},
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '三级目录2',
expand: true,
children: []
}
]
},
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '二级目录2',
expand: true,
children: [
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '三级目录1',
expand: true,
children: []
},
{
id: 'id',
parentId: 'parentId',
code: 'code',
name: '三级目录2',
expand: true,
children: []
}
]
},
]
}
]
}
}
构造代码如下:
export default {
data() {
return {
treeContents: [],
map: {
id: 'id',
parentId: 'parentId',
code: 'code',
name: 'name',
children: 'subSort'
},
expand: true,
}
},
methods: {
...mapActions('serviceSort', ['getTreeContents']),
// 获取树形目录
doTreeContents() {
this.getTreeContents({username: this.user.username}).then((data) => {
if (data.success) {
this.treeContents = this.convertTree(data.root, this.map);
} else {
this.treeContents = []
}
}).catch((error) => {
console.log('error', error);
});
},
// 树形数据转换
convertTree(tree, map) {
const result = [];
if (tree) {
tree.forEach((item) => {
const id = item[map.id];
const parentId = item[map.parentId];
const code = item[map.code];
const name = item[map.name];
let children = item[map.children];
const expand = this.expand;
if (children) {
children = this.convertTree(children, map)
}
result.push({id, parentId, code, name, children, expand})
});
} else {
return []
}
return result
},
},
mounted() {
this.doTreeContents();
}
}
如果你希望一级目录是自己手动构造(静态),而是不是后端提供的,你可以这样:
treeContents() {
let result = [{
nameame: '我管理的目录',
expand: true,
children: []
}];
if (!_.isEmpty(this.contentsData)) {
this.contentsData.forEach((item) => {
const id = item.id;
const name = item.name;
const code= item.code;
result[0].children.push({id, name, code});
});
} else {
return result = []
}
return result
}
2. 再来说说样式
通常,iview官网提供的样式不适用于具体业务需求,这时就需要用render函数自定义,官网也提供了一个render自定义的例子,但是样式单一,比如没用图标,点击不了数据等等。下面这个例子就简单的解决了这些,点击目录查询,点击时有选中效果,如果您想默认选中第一个二级目录,请浏览最后一个例子:
// 点击目录查询
renderContent(h, {root, node, data}) {
return h('Tooltip', {
props: {
content: data.name,
maxWidth: '20px',
delay: 500
},
}, [
h('span', [
h('Icon', {
props: {
type: 'md-compass',
color: node.node.selected ? '#ff9900' : 'inherit'
},
style: {
marginRight: '4px',
},
}),
h('span', {
style: {
color: node.node.selected ? '#2d8cf0' : '#1a2846',
fontWeight: node.node.selected ? 'bold' : 'normal',
},
on: {
click: () => {
if (!node.node.selected) {
this.$refs.tree.handleSelect(node.nodeKey);
this.searchText(data.name);
}
},
}
}, data.name)]),
]);
},
如果一级目录是我们自己静态手动写的,不需要点击,鼠标放上面不是‘手型’,你可以这样:下面的例子当鼠标悬停在一级目录是,鼠标不是‘手型’,而是“文本型”:cursor: node.nodeKey !== 0 ? 'pointer' : 'text',。此外还实现初始时选中第一个二级目录的效果。
// 点击目录查询
renderContent(h, {root, node, data}) {
//这条if代码块设置flag,只执行一次,实现初始时选中第一个二级目录
if (node.nodeKey === 1 && this.flag) {
this.flag = false;
this.$refs.tree.handleSelect(node.nodeKey);
this.searchText(data.name)
}
return h('Tooltip', {
props: {
content: data.name,
maxWidth: '20px',
delay: 500
},
}, [
h('span', [
h('Icon', {
props: {
type: 'md-compass',
color: node.node.selected ? '#ff9900' : 'inherit'
},
style: {
marginRight: '4px',
},
}),
h('span', {
style: {
cursor: node.nodeKey !== 0 ? 'pointer' : 'text',
color: node.node.selected ? '#2d8cf0' : '#1a2846',
fontWeight: node.node.selected ? 'bold' : 'normal',
},
on: {
click: () => {
if (!node.node.selected && node.nodeKey !== 0) {
this.$refs.tree.handleSelect(node.nodeKey);
this.searchText(data.name)
}
},
}
}, data.name)]),
]);
},