vue el-table表格数据编辑,取消编辑数据还原,批量提交编辑修改数据

el-table 切换选中,表格数据编辑,编辑/取消按钮切换,取消编辑数据还原,批量提交修改数据

// 表格部分
<el-button type="primary"
                 @click="submit()">提交修改</el-button>
<el-table ref="multipleTable"
          :data="tableData"
          stripe
          @selection-change="handleSelectionChange">
  <el-table-column type="selection"
                   is-checked
                   width="60"
                   align="center" />
  <el-table-column type="index"
                   width="80"
                   label="序号"
                   align="center" />
  <el-table-column prop="name"
                   label="姓名"
                   align="center" />
  <el-table-column prop="address"
                   label="地址"
                   align="center">
    <template slot-scope="{row,$index}">
      <el-form v-show="showEdit[$index]"
               :model="row">
        <el-form-item prop="address">
          <el-input v-model="row.address"
                    size="small"
                    name="value" />
        </el-form-item>
      </el-form>
      <span v-show="!showEdit[$index]">{{ row.address }}</span>
    </template>
  </el-table-column>
  <el-table-column label="操作"
                   align="center">
    <template slot-scope="{ row, $index }">
      <el-button v-if="!showBtn[$index]"
                 type="primary"
                 size="mini"
                 @click.native="handleEdit($index, row)">编辑</el-button>
      <el-button v-if="showBtn[$index]"
                 type="primary"
                 size="mini"
                 @click.native="handleCancel($index, row)">取消</el-button>
    </template>
  </el-table-column>
</el-table>
// script部分
<script>
// 接口api
import { updateValue } from "@/api/update"; 
import Vue from "vue";
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 弄",
        },
      ],
      multipleSelection: [],
      showEdit: [], // 显示编辑框
      showBtn: [], // 显示操作按钮
      submitData:[],
    };
  },

  methods: {
    // 获取选中行的这一条数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 切换选中
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
      this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },

    // 点击编辑
    handleEdit(index, row) {
    // 先将要编辑的原始值赋值给新加的参数originalAddress ,便于编辑时数据还原
      row.originalAddress = row.address;
      this.$refs.multipleTable.toggleRowSelection(row);
      this.showEdit[index] = true;
      this.showBtn[index] = true;
      this.$set(this.showEdit, row, true);
      this.$set(this.showBtn, row, true);
    },

    // 取消编辑
    handleCancel(index, row) {
      row.address = row.originalAddress;
      this.$refs.multipleTable.toggleRowSelection(row);
      this.showEdit[index] = false;
      this.showBtn[index] = false;
      this.$set(this.showEdit, row, false);
      this.$set(this.showBtn, row, false);
    },

    // 从选中的行中取出修改的参数和值
    handleQuery(row) {
      this.multipleSelection.map((i, index) => {
        i.show = false;
        Vue.set(this.multipleSelection, index, i);
        this.submitData.push({
          address: this.multipleSelection[index].address,
        });
      });
      // 取出所有选中修改的参数后还原表格所有操作按钮的状态
      this.tableData.map((i, index) => {
        i.show = false;
        this.showEdit[index] = false;
        this.showBtn[index] = false;
        this.$set(this.showEdit, row, false);
        this.$set(this.showBtn, row, false);
      });
    },
    // 提交修改参数
    submit() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          message: "请至少选择一条数据!",
          type: "warning",
          duration: 3 * 1000,
        });
      } else {
        this.handleQuery();
        updateValue(this.submitData)
          .then((res) => {
            if (res.date.code === 0) {
              this.$message({
                message: "数据修改成功",
                type: "success",
                duration: 5 * 1000,
              });
              this.submitData = [];
            }
          })
          .catch((error) => {
            console.log(error);
          });
         // 清除所有选中
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vueel-table组件提供了多种方式来对表格进行分割。 首先,可以使用固定来分割表格。在el-table组件的columns属性中,通过设置固定的fixed属性为"left"或"right",可以实现将该固定在表格的左侧或右侧。例如: ```html <el-table :data="tableData"> <el-table-column label="姓名" prop="name" fixed="left"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <!-- 其他配置 --> <el-table-column label="地址" prop="address" fixed="right"></el-table-column> </el-table> ``` 其次,还可以通过自定义表头来实现分割。在el-table组件的处理slot中,可以自定义表头内容,通过添加额外的元素或样式来实现的分割效果。例如: ```html <el-table :data="tableData" style="border-collapse: separate; border-spacing: 10px;"> <el-table-column label="姓名" prop="name"> <template slot="header"> <div style="border-bottom: 1px solid #999;">姓名</div> </template> </el-table-column> <el-table-column label="年龄" prop="age"> <template slot="header"> <div style="border-bottom: 1px solid #999;">年龄</div> </template> </el-table-column> <!-- 其他配置 --> </el-table> ``` 除了以上两种方式,还可以通过使用CSS来实现的分割。通过为el-table组件添加自定义的CSS类名,然后在样式中设置border、padding等属性,可以实现之间的分割效果。例如: ```html <el-table :data="tableData" class="my-table"> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <!-- 其他配置 --> </el-table> ``` ```css .my-table td { border-right: 1px dashed #999; padding-right: 10px; } ``` 总之,vueel-table组件可以通过使用固定、自定义表头或CSS样式来实现的分割效果,开发者可以根据需求选择适合的方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值