【ElementUI】Table增加操作按钮例如编辑和删除

一、前言,实际还是参考官方文档,官方文档链接

https://element.eleme.cn/#/zh-CN/component/table

二、正文

1.找到Table表格这里点击查看相关内容,找到自定义列模板,这个模板比较适合做增删改查

在这里插入图片描述

2.分析模板

①模板效果图

在这里插入图片描述

②模板代码

<template>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
      label="日期"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{ scope.row.date }}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="姓名"
      width="180">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
          <p>姓名: {{ scope.row.name }}</p>
          <p>住址: {{ scope.row.address }}</p>
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.name }}</el-tag>
          </div>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }]
      }
    },
    methods: {
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
    }
  }
</script>

③找到编辑删除按钮的核心代码并关注此模板给出的提示

<el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>

tips:通过 Scoped slot 可以获取到 row, column, $index 和 store(table 内部的状态管理)的数据,用法参考 demo。

我从其他文章中看到说

<template slot-scope="scope"></template>

是固定写法,关键关注点在scope,scope的话具体使用规则就在上面的tips中,
我们通过scope就可以获取到row行信息,然后传递给按钮事件(例如单击事件click)触发的自定义函数中,然后传递给后台,实现处理相关行数据的目标。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 ElementUItable 中添加单选按钮,你需要使用 table 的 selection 属性来启用单选模式,并且使用 scoped slot 的方式在 table-column 中添加单选按钮。 下面是一个示例代码: ``` <template> <el-table :data="tableData" v-model="selectedRow" :selectable="row => row.status !== 'disabled'"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="status" label="Status"></el-table-column> <el-table-column label="Action"> <template slot-scope="scope"> <el-button v-if="scope.row.status === 'enabled'" @click="handleEdit(scope.$index)">Edit</el-button> <el-button v-else disabled>Edit</el-button> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', status: 'enabled' }, { name: 'Alice', status: 'enabled' }, { name: 'Bob', status: 'disabled' } ], selectedRow: null } }, methods: { handleEdit(index) { const row = this.tableData[index] // handle edit action } } } </script> ``` 在这个示例中,我们使用了 table 的 selection 属性来启用单选模式,并且使用了 selectable 属性来指定只有 status 为 "enabled" 的行才可以被选中。在 table-column 中,我们使用了 type="selection" 来显示单选按钮,并且使用 scoped slot 的方式在 table-column 中添加了其它的操作按钮。 同时,我们也使用了 v-model 来绑定选中的行数据,这样在 handleEdit 方法中就可以方便地获取到选中的行数据并进行操作

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值