elementui 之el-table通过v-if 控制按钮显示与隐藏

前端效果演示:

后端代码:

 <el-row>
            <el-table :data="tableData" style="width: 100%;" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column prop="privilegeName" label="权限名称" >
                        </el-table-column>
                        <el-table-column prop="privilegeCode" label="权限编码" >
                        </el-table-column>
                        <el-table-column prop="privilegeType" label="权限类别" :formatter="formatPrivilegeType" >
                        </el-table-column>

                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button  v-if="scope.row.privilegeType === '1' || scope.row.privilegeType === '2'" type="primary" size="mini" @click="toAdd(scope)">新增</el-button>
                                <el-button type="primary" size="mini" @click="toEdit(scope)">编辑</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <br>
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.pageIndex"
                        :page-sizes="[5, 10, 20, 30, 40]"
                        :page-size=pagination.pageSize
                        layout="total, prev, pager, next"
                        :total=pagination.total>
                    </el-pagination>
        </el-row>

 

以下是一个基本的 Vue3 封装 ElementUIel-table 的示例代码,已经加上了注释,希望能够帮到你: ```html <template> <!-- 使用封装的表格组件 --> <my-table :data="tableData" :columns="tableColumns"></my-table> </template> <script> // 引入 ElementUIel-table 组件 import { Table, TableColumn } from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; export default { components: { // 注册封装的表格组件 MyTable: { components: { Table, TableColumn }, props: { // 表格数据 data: { type: Array, required: true, }, // 表格列头配置 columns: { type: Array, required: true, }, }, template: ` <el-table :data="data"> <!-- 遍历列头配置,生成表格列 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"> <!-- 如果列头配置中有指定 slot-scope 属性,则使用插槽方式渲染 --> <template v-if="column.slotScope" slot-scope="scope"> <slot :name="column.slotScope" :row="scope.row" :column="column"></slot> </template> </el-table-column> </el-table> `, }, }, data() { return { tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 22, gender: '女' }, { name: '王五', age: 30, gender: '男' }, ], tableColumns: [ // 普通列头 { label: '姓名', prop: 'name' }, { label: '年龄', prop: 'age' }, { label: '性别', prop: 'gender' }, // 带有插槽的列头 { label: '操作', slotScope: 'default', // 插槽内容可以是任意的 HTML,这里以简单的按钮为例 template: '<el-button size="small">编辑</el-button>', }, ], }; }, }; ``` 上述代码中,我们首先引入了 ElementUIel-tableel-table-column 组件,然后定义了一个 MyTable 组件,它包含了一个 el-table 组件和若干个 el-table-column 组件,其中表格数据和列头配置通过 props 传入。 在组件模板中,我们使用了 v-for 遍历列头配置,生成对应的 el-table-column 组件,并通过 prop 和 label 属性绑定列头的内容和对应数据的属性名。如果列头配置中有指定 slot-scope 属性,则说明这一列需要自定义渲染,我们在该列的 template 中定义了一个简单的按钮,并通过插槽的方式将其渲染到表格中。 最后,在父组件中使用 MyTable 组件,并将表格数据和列头配置传入即可。 希望以上代码能够帮助到你,如果有需要进一步的解释或其他问题,欢迎追问!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值