Element UI 多级表格渲染

// TableColumn.js
function renderColumn(h, column){
    return h{
        'el-table-column',
        {
            props:{
                align:'center',
                ...column
            }
        },
        column&&column.children&&column.children.map(c=> renderColumn(h, c))
    }
}
export default {
    functional: true,
    props: {
        columns: {
            type:Array,
            default: ()=>[]
        }
    },
    render(h, ctx) {
        return ctx.props.columns.map(column => renderColumn(h, column ))
    }

}
// Table.vue
<template>
    <el-table
        ref="elTable"
        :data="list"
        stripe
        border
        height="100%"
    >
        
    </el-table>
</template>
<script>
    import TableColumn from './TableColumn.js'
    const columns = [
        {
            label:'一级',
            children:[
                {
                    label:'二级一',
                    align:'center',
                    prop:'name1'

                },
                {
                    label:'二级二',
                    children:[
                        {    
                            label:'三级一',
                            align:'center',
                            prop:'name2'

                        },
                        {
                            label:'三级二',
                            align:'center',
                            prop:'name3'
                    
                        }
                    ]
                    
                }
            ]
        }
        
    ]
    export default {
        name: "Table",
        data() {
            return {
                list:[]
            }
        }
    }
</script>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
渲染多级菜单,可以使用 Element UI 的 Menu 组件和 Submenu 组件。首先,你需要定义一个菜单数据源,包含多级菜单的信息。例如: ```javascript menuData: [ { name: 'Dashboard', icon: 'el-icon-s-home', children: [ { name: 'Dashboard 1', path: '/dashboard/1' }, { name: 'Dashboard 2', path: '/dashboard/2' } ] }, { name: 'Products', icon: 'el-icon-menu', children: [ { name: 'Product 1', path: '/product/1' }, { name: 'Product 2', path: '/product/2', children: [ { name: 'Sub Product 1', path: '/product/2/sub/1' }, { name: 'Sub Product 2', path: '/product/2/sub/2' } ] } ] } ] ``` 然后,在你的侧边栏组件中,使用 Menu 和 Submenu 组件来渲染菜单: ```html <el-menu :default-active="$route.path" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"> <template v-for="item in menuData"> <el-submenu v-if="item.children" :index="item.name"> <template slot="title"> <i :class="item.icon"></i> <span slot="title">{{ item.name }}</span> </template> <el-menu-item-group> <template v-for="child in item.children"> <el-menu-item :index="child.path">{{ child.name }}</el-menu-item> <el-submenu v-if="child.children" :index="child.path"> <template slot="title">{{ child.name }}</template> <el-menu-item-group> <template v-for="subChild in child.children"> <el-menu-item :index="subChild.path">{{ subChild.name }}</el-menu-item> </template> </el-menu-item-group> </el-submenu> </template> </el-menu-item-group> </el-submenu> <el-menu-item v-else :index="item.path"> <i :class="item.icon"></i> <span slot="title">{{ item.name }}</span> </el-menu-item> </template> </el-menu> ``` 这样,就可以渲染多级菜单了。注意,在模板中使用了 v-for 来遍历菜单数据源,并使用 v-if 判断是否存在子菜单。如果存在子菜单,则使用 el-submenu 组件来渲染。如果还存在子菜单,则继续使用 el-submenu 组件递归渲染

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值