Tree 样式多元化

文章详细描述了如何在Vue中使用Tree组件进行递归渲染,包括不同层级的节点样式处理,以及如何添加和删除子节点的方法实现。
摘要由CSDN通过智能技术生成

在这里插入图片描述

<template>
    <Tree :data="data" :render="renderContent" class="demo-tree-render"></Tree>
</template>
<script>
    import { resolveComponent } from 'vue'
    export default {
        data () {
            return {
                data: [
                    {
                        title: 'parent 1',
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h(resolveComponent('Icon'), {
                                        type: 'ios-folder-outline',
                                      
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title +'(59/62)')
                                ]),
                               
                            ]);
                        },
                        children: [
                            {
                                title: 'child 1-1',
                                expand: true,
                              	levle:2,
                                children: [
                                    {
                                        title: 'leaf 1-1-1',
                                        expand: true,
                              	levle:3
                                    },
                                    {
                                        title: 'leaf 1-1-2',
                                        expand: true,
                              	levle:3
                                    }
                                ]
                            },
                            {
                                title: 'child 1-2',
                                expand: true,
                              	levle:2,
                                children: [
                                    {
                                        title: 'leaf 1-2-1',
                                        expand: true,
                              	levle:3
                                    },
                                    {
                                        title: 'leaf 1-2-1',
                                        expand: true,
                              	levle:3
                                    }
                                ]
                            }
                        ]
                    }
                ],
                buttonProps: {
                    type: 'default',
                    size: 'small',
                }
            }
        },
        methods: {
            renderContent (h, { root, node, data }) {              
               if(data.levle == 2){
                      return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h(resolveComponent('Icon'), {
                                         type: 'ios-pin',  
                                        style: {
                                            marginRight: '8px',
                                          color:'blue'||'gray'
                                        }
                                    }), 
                                       h('span', {},data.title ), 
                                       h('div', { style: {
                                            display: 'inline-block',
                                            width: '100%'
                                        }},[
                                         h('span', {},'('),
                                           h('span', { style: {
                                             color:'#f00' 
                                          }},56), 
                                         h('span', {},'/'),
                                         h('span', {},89), 
                                         h('span', {},')'),
                                       ] ),
                                ]),
                               
                            ]);
              }else{
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h(resolveComponent('Icon'), {
                            type: 'ios-paper-outline',
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title),
                      	h('span', { style: {
														background:'#0f0',                     
                          color:'#fff',
                          padding:'1px 2PX',
                          borderRadius:'4px',
                            marginLeft: '4px'
                        }},'验'), 
                      h('span', { style: {
														background:'#0f0',                     
                          color:'#fff',
                          padding:'1px 2PX',
                          borderRadius:'4px',
                            marginLeft: '4px'
                        }},'考'), 
                        h('span', { style: {
														background:'#0f0',                     
                          color:'#fff',
                          padding:'1px 2PX',
                          borderRadius:'4px',
                            marginLeft: '4px'
                        }},'国'), 
                    ]),
                    
                ]);
              }
         
            },
            append (data) {
                const children = data.children || [];
                children.push({
                    title: 'appended node',
                    expand: true
                });
                data.children = children
            },
            remove (root, node, data) {
                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>
<style>
    .demo-tree-render .ivu-tree-title{
        width: 100%;
    }
</style>



  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*阿漓*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值