ivew数控件Tree自定义节点内容示例分析 demo地址:https://run.iviewui.com/plcWlM4H template Tree :data="data5" :render="renderContent"/Tree/templatescript export default { data () { return { data5: [ { title: ‘parent 1‘ ,
ivew数控件Tree自定义节点内容示例分析
demo地址:https://run.iviewui.com/plcWlM4H
<template> <Tree :data="data5" :render="renderContent"></Tree> </template> <script> export default { data () { return { data5: [ { title: ‘parent 1‘, expand: true, children: [ { title: ‘child 1-1‘, expand: true, children: [ { title: ‘leaf 1-1-1‘, expand: true }, { title: ‘leaf 1-1-2‘, expand: true } ] }, { title: ‘child 1-2‘, expand: true, children: [ { title: ‘leaf 1-2-1‘, expand: true }, { title: ‘leaf 1-2-1‘, expand: true } ] } ] } ], buttonProps: { type: ‘default‘, size: ‘small‘, } } }, //<Icon type="ios-create-outline" /> methods: { renderContent (h, { root, node, data }) { //console.log(root, ‘树的根节点‘, node, ‘当前节点‘, data, ‘当前节点数据‘); /* @root <Array> 把整个树 扁平化 后的 数组 @node <Object> 当前节点 @date <Object> 当前节点数据 nodeKey 每个节点 的唯一标识 parent 父级的 nodeKey */ return h(‘span‘, { class: ‘span1‘, style: { display: ‘inline-block‘, width: ‘100%‘ }, }, [ h(‘span‘, [ h(‘Icon‘, { props: { type: ‘ios-paper-outline‘ }, class: ‘wenjiantubiao‘, style: { marginRight: ‘8px‘ } }), h(‘span‘, data.title) // 节点标题 ]), h(‘span‘, { //右侧操作 包裹 class: ‘youcecaozuo‘, style: { display: ‘inline-block‘, float: ‘right‘, marginRight: ‘32px‘ } }, [ h(‘Button‘, { //添加按钮 props: Object.assign({}, this.buttonProps, { icon: ‘ios-add‘ }), style: { marginRight: ‘8px‘ }, on: { click: () => { this.append(data) } } }), h(‘Button‘, { //删除按钮 props: Object.assign({}, this.buttonProps, { icon: ‘ios-remove‘ }), on: { click: () => { this.remove(root, node, data) } } }) ]) ]); }, append (data) { const children = data.children || []; children.push({ title: ‘appended node‘, expand: true }); this.$set(data, ‘children‘, children); }, remove (root, node, data) { //console.log(root, node, data, ‘三个东西‘); console.log(node, ‘确定的node节点‘); /* 因为 参数node 是从 root 上引用 处理的 所以 find 时 可以 判断 el对象 和 node 对象是否相等, 详见:https://www.cnblogs.com/taohuaya/p/11535432.html */ const parentKey = root.find(el => { console.log(el, ‘root每项‘); return el === node; }).parent; //找到删除项的 父级标识 const parent = root.find(el => el.nodeKey === parentKey).node; //通过 删除的 父级标识 找到 父级元素 const index = parent.children.indexOf(data); //在父级的children数组 中 找到当前删除元素 的索引 index parent.children.splice(index, 1); //在父级children中 删除掉 要删除的元素 } } } /* render: (h, { root, node, data }) => { return h(‘span‘, { style: { display: ‘inline-block‘, width: ‘100%‘ } }, [ h(‘span‘, [ h(‘Icon‘, { props: { type: ‘ios-folder-outline‘ }, style: { marginRight: ‘8px‘ } }), h(‘span‘, data.title) ]), h(‘span‘, { style: { display: ‘inline-block‘, float: ‘right‘, marginRight: ‘32px‘ } }, [ h(‘Button‘, { props: Object.assign({}, this.buttonProps, { icon: ‘ios-add‘, type: ‘primary‘ }), style: { width: ‘64px‘ }, on: { click: () => { this.append(data) } } }) ]) ]); }, */ </script>
视图: