update Tree selected

This commit is contained in:
梁灏 2017-10-25 22:34:58 +08:00
parent 6ff0e34003
commit af7d72b4cc
2 changed files with 117 additions and 70 deletions

View file

@ -94,16 +94,122 @@
<!--</script>-->
<!--<template>-->
<!--<div style="width: 400px;">-->
<!--<Tree :data="data4" :render="renderContent"></Tree>-->
<!--</div>-->
<!--</template>-->
<!--<script>-->
<!--export default {-->
<!--data () {-->
<!--return {-->
<!--data4: [-->
<!--{-->
<!--title: 'parent 1',-->
<!--expand: true,-->
<!--children: [-->
<!--{-->
<!--title: 'parent 1-1',-->
<!--expand: true,-->
<!--children: [-->
<!--{-->
<!--title: 'leaf 1-1-1',-->
<!--expand: true-->
<!--},-->
<!--{-->
<!--title: 'leaf 1-1-2',-->
<!--expand: true-->
<!--}-->
<!--]-->
<!--},-->
<!--{-->
<!--title: 'parent 1-2',-->
<!--expand: true,-->
<!--children: [-->
<!--{-->
<!--title: 'leaf 1-2-1',-->
<!--expand: true-->
<!--},-->
<!--{-->
<!--title: 'leaf 1-2-1',-->
<!--expand: true-->
<!--}-->
<!--]-->
<!--}-->
<!--]-->
<!--}-->
<!--],-->
<!--buttonProps: {-->
<!--type: 'ghost',-->
<!--size: 'small',-->
<!--}-->
<!--}-->
<!--},-->
<!--methods: {-->
<!--renderContent (h, { data, node, root }) {-->
<!--return h('span', {-->
<!--style: {-->
<!--display: 'inline-block',-->
<!--width: '100%'-->
<!--}-->
<!--}, [-->
<!--h('span', data.title),-->
<!--h('span', {-->
<!--style: {-->
<!--display: 'inline-block',-->
<!--float: 'right',-->
<!--marginRight: '32px'-->
<!--}-->
<!--}, [-->
<!--h('Button', {-->
<!--props: Object.assign({}, this.buttonProps, {-->
<!--icon: 'ios-plus-empty'-->
<!--}),-->
<!--style: {-->
<!--marginRight: '8px'-->
<!--},-->
<!--on: {-->
<!--click: () => { this.append(data) }-->
<!--}-->
<!--}),-->
<!--h('Button', {-->
<!--props: Object.assign({}, this.buttonProps, {-->
<!--icon: 'ios-minus-empty'-->
<!--}),-->
<!--on: {-->
<!--click: () => { this.remove(node, data, root) }-->
<!--}-->
<!--})-->
<!--])-->
<!--]);-->
<!--},-->
<!--append (data) {-->
<!--this.$set(data, 'children', [{-->
<!--title: 'appended node',-->
<!--expand: true-->
<!--}]);-->
<!--},-->
<!--remove (node, data, root) {-->
<!--const parentKey = root.find(el => el === node).parent;-->
<!--const parent = root.find(el => el.nodeKey === parentKey).node;-->
<!--const index = parent.children.indexOf(data);-->
<!--parent.children.splice(index, 1);-->
<!--}-->
<!--}-->
<!--}-->
<!--</script>-->
<template>
<div style="width: 400px;">
<Tree :data="data4" :render="renderContent"></Tree>
<div>
{{ data1 }}
<Tree :data="data1"></Tree>
</div>
</template>
<script>
export default {
data () {
return {
data4: [
data1: [
{
title: 'parent 1',
expand: true,
@ -113,12 +219,10 @@
expand: true,
children: [
{
title: 'leaf 1-1-1',
expand: true
title: 'leaf 1-1-1'
},
{
title: 'leaf 1-1-2',
expand: true
title: 'leaf 1-1-2'
}
]
},
@ -127,74 +231,17 @@
expand: true,
children: [
{
title: 'leaf 1-2-1',
expand: true
title: 'leaf 1-2-1'
},
{
title: 'leaf 1-2-1',
expand: true
title: 'leaf 1-2-1'
}
]
}
]
}
],
buttonProps: {
type: 'ghost',
size: 'small',
}
}
},
methods: {
renderContent (h, { data, node, root }) {
return h('span', {
style: {
display: 'inline-block',
width: '100%'
}
}, [
h('span', data.title),
h('span', {
style: {
display: 'inline-block',
float: 'right',
marginRight: '32px'
}
}, [
h('Button', {
props: Object.assign({}, this.buttonProps, {
icon: 'ios-plus-empty'
}),
style: {
marginRight: '8px'
},
on: {
click: () => { this.append(data) }
}
}),
h('Button', {
props: Object.assign({}, this.buttonProps, {
icon: 'ios-minus-empty'
}),
on: {
click: () => { this.remove(node, data, root) }
}
})
])
]);
},
append (data) {
this.$set(data, 'children', [{
title: 'appended node',
expand: true
}]);
},
remove (node, data, root) {
const parentKey = root.find(el => el === node).parent;
const parent = root.find(el => el.nodeKey === parentKey).node;
const index = parent.children.indexOf(data);
parent.children.splice(index, 1);
]
}
}
}
</script>
</script>

View file

@ -147,9 +147,9 @@
},
handleSelect (nodeKey) {
const node = this.flatState[nodeKey].node;
if (!this.multiple){ // reset selected
if (!this.multiple){ // reset previously selected node
const currentSelectedKey = this.flatState.findIndex(obj => obj.node.selected);
if (currentSelectedKey >= 0) this.$set(this.flatState[currentSelectedKey].node, 'selected', false);
if (currentSelectedKey >= 0 && currentSelectedKey !== nodeKey) this.$set(this.flatState[currentSelectedKey].node, 'selected', false);
}
this.$set(node, 'selected', !node.selected);