1.展示方式概述与效果呈现
点击左边的小三角,会显示出来权限。
通过设置 type=“expand” 和 Scoped slot 可以开启展开行功能,el-table-column 的模板会被渲染成为展开行的内容,展开行可访问的属性与使用自定义列模板时的 Scoped slot 相同。
2.基本流程
先获取到等级1,然后再从等级1中循环得到等级2,并且要以树的形式输出。
<el-table :data="roleList" border style="width:100%">
<!-- 展开行的使用 -->
<el-table-column type="expand">
<template slot-scope="scope">
<el-row v-for="m in scope.row.menu" :key="m.id">
<el-col :span="10">{{m.name}}</el-col>
<el-col :span="14"><span v-for="sm in m.children" :key="sm.id">{{sm.name}}</span></el-col>
</el-row>
</template>
</el-table-column>
3.完整代码
<!-- components/power/Role.vue -->
<template>
<div>
<!-- 面包屑 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>权限管理</el-breadcrumb-item>
<el-breadcrumb-item>角色列表</el-breadcrumb-item>
</el-breadcrumb>
<el-card>
<!-- 新增角色按钮 -->
<el-row :gutter="20">
<el-col :span="2">
<el-button type="primary" icon="el-icon-circle-plus-outline">新增角色</el-button>
</el-col>
</el-row>
<!-- 表格显示 -->
<el-row>
<el-col>
<el-table :data="roleList" border style="width:100%">
<!-- 展开行的使用 -->
<el-table-column type="expand">
<template slot-scope="scope">
<el-row v-for="m in scope.row.menu" :key="m.id">
<el-col :span="10">{{m.name}}</el-col>
<el-col :span="14"><span v-for="sm in m.children" :key="sm.id">{{sm.name}}</span></el-col>
</el-row>
</template>
</el-table-column>
<el-table-column type="index"></el-table-column>
<el-table-column prop="id" label="ID"></el-table-column>
<el-table-column prop="name" label="角色名称"></el-table-column>
<el-table-column prop="desc" label="角色详情"></el-table-column>
<el-table-column prop="level" label="操作">
<template slot-scope="scope">
<el-button size="small" type="primary" icon="el-icon-edit" circle @click="showEdit(scope.row)"></el-button>
<el-button size="small" type="warning" icon="el-icon-refresh" circle @click="showReset(scope.row)"></el-button>
<el-button size="small" type="danger" icon="el-icon-delete" circle @click="showDel(scope.row)"></el-button>
</template>
</el-table-column>
</el-table>
</el-col>
</el-row>
</el-card>
</div>
</template>
<script>
export default {
data () {
return {
roleList: []
}
},
created(){
this.getRoleList()
},
methods: {
async getRoleList() {
const { data: res } = await this.$axios.get('/api/role')
if (res.status !== 200) return this.$msg.error(res.msg)
console.log(res.data);
this.roleList = res.data
}
}
}
</script>
4.效果展示
效果的美化,我放在下一篇博客继续哈!!!!