element-ui表格table增删改查时多选/选择当前行

本文介绍如何在使用Element-UI的表格组件时,实现表单数据的增删改查功能,特别是如何判断并处理选择的当前行,以满足删除多行及查看行详情的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表单数据的增删改查
如何判断选择到的是哪一行?
需求:删除多行,查看一行的详情。

<el-button type="primary" @click="delButton">删除</el-button>
<el-button type="primary" @click="modifyAndDetail('查看详细', false)">查看详细</el-button>

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    tooltip-effect="dark"
    style="width: 100%"
    @selection-change="selected">
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      label="日期"
      width="120">
      <template slot-scope="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址"
      show-overflow-tooltip>
    </el-table-column>
  </el-table>
</template>

<script>
  export default {
    data() {
      return {
      	batchCancelId = [],  // 得到该数组就可以对一行或者多行数据进行增删改查了
        tableData: [{
          id:  0,
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id:  1,
          date: '2016-05-02',
          name: '王大虎',
          address: '上海市普陀区金沙江路 3333 弄'
        }]
      }
    },
    methods: {
      selected(select) {   //这里的select就是选择到的当行所有数据的数组集合
      	this.batchCancelId = [] // 定义一个数组来存储选择到的id
      	this.selectionIndex = []
      	select.map((item) => {  // 一个item就是选择到的一行数据对象
// {id:1, date:'2016-05-02', name:'王大虎', address:'上海市普陀区金沙江路 3333 弄'}
        	this.batchCancelId.push(item.id)  // 取出对象的id加入到数组中
        	this.selectionIndex.push(item.index)
      	})
      }
    }
  }
</script>

在这里插入图片描述
数据来自 element-ui: http://element-cn.eleme.io/#/zh-CN/component/table

Element UI Table 提供了一些 API 和事件,可以方便地实现增删改查操作。下面是一个简单的示例: ```vue <template> <div> <el-table :data="tableData" style="width:100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="handleEdit(scope.row)">编辑</el-button> <el-button @click="handleDelete(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> <el-dialog :visible.sync="dialogVisible"> <el-form :model="currentRow"> <el-form-item label="姓名"> <el-input v-model="currentRow.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model.number="currentRow.age"></el-input> </el-form-item> </el-form> <div slot="footer"> <el-button @click="dialogVisible = false">取消</el-button> <el-button type="primary" @click="handleSave">保存</el-button> </div> </el-dialog> <el-button type="primary" @click="handleAdd">新增</el-button> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18 }, { name: '李四', age: 20 }, { name: '王五', age: 22 } ], dialogVisible: false, currentRow: {} } }, methods: { handleAdd() { this.currentRow = {} this.dialogVisible = true }, handleEdit(row) { this.currentRow = Object.assign({}, row) this.dialogVisible = true }, handleDelete(index) { this.tableData.splice(index, 1) }, handleSave() { if (this.currentRow.name && this.currentRow.age) { const index = this.tableData.indexOf(this.currentRow) if (index > -1) { Object.assign(this.tableData[index], this.currentRow) } else { this.tableData.push(this.currentRow) } this.dialogVisible = false } } } } </script> ``` 上面的代码中,我们定义了一个 `tableData` 数组,用于存储表格数据。通过 `<el-table>` 组件和 `<el-table-column>` 组件,我们可以将数据呈现为表格形式,并显示操作按钮。 当用户点击“新增”按钮,我们将 `currentRow` 清空,并打开弹窗。当用户点击“编辑”按钮,我们将 `currentRow` 设置为当前行数据,并打开弹窗。当用户点击“删除”按钮,我们从 `tableData` 中删除对应的行数据。 弹窗中包含一个表单,用于编辑行数据。当用户点击“保存”按钮,我们根据 `currentRow` 中的数据,更新 `tableData` 中对应的行数据,或者将 `currentRow` 添加到 `tableData` 中。如果 `currentRow` 的数据不完整,我们不执行任何操作,并保持弹窗打开状态。 通过这种方式,我们可以方便地实现基本的增删改查功能。当然,如果需要更复杂的操作,可以根据具体需求进行扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值