123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <template>
- <m-tree
- :data="list"
- node-key="id"
- show-checkbox
- default-expand-all
- :render-content="renderContent">
- </m-tree>
- </template>
- <script setup>
- import { ref } from 'vue-demi';
- let id = 1000;
- const list = ref([
- {
- id: 1,
- label: '一级 1',
- children: [
- {
- id: 4,
- label: '二级 1-1',
- children: [
- {
- id: 10,
- label: '三级 1-1-1'
- },
- {
- id: 11,
- label: '三级 1-1-2'
- }
- ]
- },
- {
- id: 5,
- label: '二级 1-2'
- }
- ]
- },
- {
- id: 2,
- label: '一级 2',
- children: [
- {
- id: 6,
- label: '二级 2-1',
- children: [
- {
- id: 12,
- label: '三级 2-1-1'
- }
- ]
- },
- {
- id: 7,
- label: '二级 2-2'
- }
- ]
- },
- {
- id: 3,
- label: '一级 3',
- children: [
- {
- id: 8,
- label: '二级 3-1'
- },
- {
- id: 9,
- label: '二级 3-2'
- }
- ]
- }
- ])
- const renderContent = (h, { data, parentData }) => {
- return h(
- 'div',
- {
- class: 'custom-tree__item'
- },
- [
- h(
- 'span',
- {
- class: 'custom-tree__label'
- },
- data.label
- ),
- h(
- 'div',
- {
- style: {
- color: '#409eff',
- fontSize: '14px'
- }
- },
- [
- h(
- 'span',
- {
- style: {
- margin: '0 10px'
- },
- onClick: (e) => append(data,e)
- },
- '追加'
- ),
- h(
- 'span',
- {
- onClick: (e) => remove(data,parentData,e)
- },
- '删除'
- )
- ]
- )
- ]
- )
- }
- const append = (data,e) => {
- e.stopPropagation()
- let children = data.children || []
- children.push({
- id: id++,
- label: 'children-node'
- })
- list.value = [...list.value]
- }
- const remove = (data,parentData,e) => {
- e.stopPropagation()
- const index = parentData.indexOf(data)
- if (index > -1) {
- parentData.splice(index, 1)
- }
- }
- </script>
- <style>
- .custom-tree__item{
- display: flex;
- flex: 1;
- justify-content: space-between;
- }
- </style>
|