vue表格最后加-添加按钮-点击后表格添加一行数据

 

<template>
  <el-table
    ref="singleTable"
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
    style="width: 100%">
    <el-table-column
      type="index"
      width="50">
    </el-table-column>
    <el-table-column
      property="date"
      label="日期"
      width="120">
      <template slot-scope="scope">
        <el-input clearable v-model="scope.row.date" placeholder="请输入日期"></el-input>
      </template>
    </el-table-column>
    <el-table-column
      property="name"
      label="姓名"
      width="120">
      <template slot-scope="scope">
        <el-input clearable v-model="scope.row.name" placeholder="请输入姓名"></el-input>
      </template>
    </el-table-column>
    <el-table-column
      property="address"
      label="地址">
      <template slot-scope="scope">
        <el-input clearable v-model="scope.row.address" placeholder="请输入地址"></el-input>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">确定</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button style="width:100%;height:50px;background:#fff;color:#000" icon="el-icon-plus" @click="add" type="primary">添加信息</el-button>
</template>

<script>
  export default {
    data() {
      return {
        //这里可以设置初始值
        obj:{
          date: '',
          name: '',
          address: ''
        },
        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 弄'
        }],
        currentRow: null
      }
    },
    methods: {
      add(){
        this.tableData.push(JSON.parse(JSON.stringify(this.obj)))
      },
      setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
      handleCurrentChange(val) {
        this.currentRow = val;
      }
    }
  }
</script>

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3 中,可以使用 `v-slot` 和 `@click` 指令来实现点击表头修改表格数据的功能。 首先,在 `<el-table>` 标签中添加 `@header-click` 监听器来捕获表头点击事件,并将其绑定到一个方法中: ```html <el-table :data="tableData" @header-click="handleHeaderClick"> <!-- 表格列 --> </el-table> ``` 然后,在 `handleHeaderClick` 方法中获取到被点击的表头列的字段名,并根据需要修改表格数据。例如,可以按照表头列的升降序对表格数据进行排序,或者筛选出符合某些条件的数据。 ```js methods: { handleHeaderClick(column) { // 获取被点击的表头列的字段名 const prop = column.property; // 根据字段名进行表格数据的修改操作 // 例如,按照升降序对表格数据进行排序 this.tableData.sort((a, b) => a[prop] - b[prop]); } } ``` 最后,可以在需要修改表格数据的表头列上使用 `v-slot` 和 `@click` 指令来添加点击事件。例如,下面的代码演示了如何在“操作”列上添加一个“删除”按钮点击按钮后删除对应的行数据: ```html <el-table :data="tableData" @header-click="handleHeaderClick"> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="操作"> <template v-slot:header> <span>操作</span> <span @click="handleDeleteAll">全部删除</span> </template> <template v-slot="scope"> <el-button @click="handleDelete(scope.row)">删除</el-button> </template> </el-table-column> </el-table> ``` 其中,`handleDeleteAll` 方法可以用来删除所有行数据,而 `handleDelete` 方法则可以用来删除单个行数据。 ```js methods: { handleHeaderClick(column) { // 获取被点击的表头列的字段名 const prop = column.property; // 根据字段名进行表格数据的修改操作 // 例如,按照升降序对表格数据进行排序 this.tableData.sort((a, b) => a[prop] - b[prop]); }, handleDelete(row) { // 从表格数据中删除指定行数据 const index = this.tableData.indexOf(row); if (index !== -1) { this.tableData.splice(index, 1); } }, handleDeleteAll() { // 删除所有行数据 this.tableData = []; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值