iview/examples/routers/tree.vue

95 lines
3.4 KiB
Vue
Raw Normal View History

<template>
2017-10-25 16:45:42 +08:00
<div>
<Tree :data="baseData" :load-data="loadData" multiple :render="renderFunc"></Tree>
<Button @click="handleAdd">add</Button>
<Button @click="handleUpdate">update</Button>
</div>
</template>
<script>
export default {
2017-03-03 10:35:38 +08:00
data () {
2017-02-07 11:34:00 +08:00
return {
2017-10-25 16:45:42 +08:00
baseData: [
2017-03-24 20:50:13 +08:00
{
2017-10-25 14:33:22 +08:00
expand: true,
2017-10-25 16:45:42 +08:00
title: 'parent 1',
2017-04-27 13:56:58 +08:00
children: [
{
2017-10-25 16:45:42 +08:00
title: 'parent 1-0',
expand: false,
children: [],
loading: false
2017-04-27 13:56:58 +08:00
},
{
2017-10-25 16:45:42 +08:00
title: 'parent 1-1',
expand: true,
2017-10-25 16:45:42 +08:00
checked: true,
2017-10-23 11:55:35 +08:00
children: [
{
2017-10-25 16:45:42 +08:00
title: 'leaf',
render: (h, { node }) => {
return h('Button', {
props: {
type: 'primary',
size: 'small'
},
on: {
click: ({target}) => {
this.logger(target.textContent);
}
}
}, node.title + '000');
}
2017-10-23 11:55:35 +08:00
}
]
2017-04-27 13:56:58 +08:00
}
]
2017-03-27 14:28:42 +08:00
}
2017-03-24 20:50:13 +08:00
]
2017-10-25 16:45:42 +08:00
};
},
methods: {
handleAdd () {
this.baseData.push(
{
title: 'test name',
checked: true
}
);
},
handleUpdate () {
const child = this.baseData[0].children[0].children[1];
// console.log(JSON.stringify(this.baseData), '\n', JSON.stringify(child));
if (!child) return this.$Message.error('Node is async and is not loaded yet');
else this.$set(child, 'checked', true);
},
logger (txt) {
console.log(txt);
},
loadData (item, callback) {
setTimeout(() => {
callback([
{
title: 'children-1',
// loading: false,
children: []
},
{
title: 'children-2',
// loading: false,
children: []
}
]);
}, 2000);
},
renderFunc (h, { node }) {
return h('Button', {
props: {
type: 'ghost'
}
}, node.title)
2017-10-23 11:55:35 +08:00
}
2017-02-07 11:34:00 +08:00
}
2017-10-25 16:45:42 +08:00
};
2017-02-07 11:34:00 +08:00
</script>