el-table选中一行数据进行移除

首先先上效果图

 选择一行,点击移除操作,

 

 选中的一行被移除。

 接下来就是全部代码

<template>
  <div class="app-container">
    <div class="form-container">
      <el-form
        ref="searchForm"
        label-width="100px"
        :inline="true"
        :model="searchForm"
      >
        <el-form-item class="search-button">
          <div class="form-button">
            <el-button type="primary" icon="el-icon-delete" @click="del()">移除(R)</el-button>
          </div>
        </el-form-item>

      </el-form>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
      height="240"
      @cell-click="chooseItem"
    >

      <el-table-column v-for="(item,index) in tableList" :key="index" :label="item.label" :prop="item.name" align="center" />
    </el-table>
  </div>
</template>

<script>
  import waves from '@/directive/waves'
  export default {
    name: 'ExcelTransferTable',
    components: { },
    directives: { waves },
    data() {
      return {
        tableList: [
          {
            label: '姓名',
            name: 'name'
          }, {
            label: '性别',
            name: 'sex'
          }, {
            label: '班级',
            name: 'className'
          }],
        tableData: [], // 表格里面的数据
        Id: ''
      }
    },
    created() {
      this.tableData = [{
                          Id: '1',
                          name: '孙梅梅',
                          sex: '女',
                          className: '一年级一班'
                        },
                        {
                          Id: '2',
                          name: '张丽丽',
                          sex: '女',
                          className: '一年级二班'
                        },
                        {
                          Id: '3',
                          name: '王帅',
                          sex: '男',
                          className: '一年级一班'
                        },
                        {
                          Id: '4',
                          name: '刘强',
                          sex: '男',
                          className: '一年级二班'
                        }]
    },
    methods: {
      del() {
        // 判断有没有选中一行数据
        if (!this.Id) return this.$message.warning('请先选择一条数据进行移除')
        this.$confirm('您确定要删除当前选择的记录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
   //定义一个新的数组
          const tempArr = []
   //遍历table表的数组,如果遍历到的table中的id不等于选中的那一行id,那么将数
 据push到新的数组里面,也就是说,新的数组里面没有选中的那一行的值
          this.tableData.forEach(item => {
            if (item.Id !== this.Id) {

              tempArr.push(item)
            }
          })
//将新的数组赋值给table表数组,此时选中那一行删除成功
          this.tableData = tempArr
//将选中的一行id置空。
          this.Id = ''
        })
      },
//row是选中这一行的全部数据,如果table的数组是从后端请求回来的,可以将id替换成每一行唯一的那个标志,比如序号等等。
      chooseItem(row) {
        this.Id = row.Id
      }
    }
  }
</script>

<style lang="scss" scoped>

.chargeType{
  /deep/ .el-select{
    width: 90%;
  }
}
</style>

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值