vue 自定义增加编辑删除表格

<!-- 可新增/删除table表格页面 -->
<template>
  <div>
    <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAddBtn">添加</el-button>
    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBtn">删除</el-button>
 
    <el-table ref="tb" :data="tableData" :header-cell-style="{background:'rgb(113 167 228)',color:'#fff'}" :row-class-name="rowClassName" border style="width: 100%; cursor: pointer;" @selection-change="handleDetailSelectionChange">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column label="序号" align="center" prop="xh" width="50" />
 
      <el-table-column prop="mescode" align="center" :required="true" label="账号">
        <template slot-scope="{row,$index}">
          <span v-if="!showEdit[$index]">{{ row.mescode }}</span>
          <el-input v-if="showEdit[$index]" v-model="tableData[row.xh-1].mescode" placeholder="请输入该用户的账号">
            <i slot="prefix" class="el-input__icon el-icon-search" />
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="password" align="center" :required="true" label="密码">
        <template slot-scope="{row,$index}">
          <span v-if="!showEdit[$index]">{{ row.password }}</span>
          <el-input v-if="showEdit[$index]" v-model="tableData[row.xh-1].password" placeholder="请输入该用户的密码">
            <i slot="prefix" class="el-input__icon el-icon-search" />
          </el-input>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" width="100" label="操作">
        <template slot-scope="{row,$index}">
          <el-button v-if="!showEdit[$index]" type="text" size="small" @click="showUpdate($index,row)">编辑</el-button>
          <el-button v-if="showEdit[$index]" type="text" size="small" style="color: #85ce61;" @click="submit($index,row)">确定</el-button>
          <el-button v-if="showEdit[$index]" type="text" size="small" style="color: red;" @click="cancelUpdate($index)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
 
<script>
export default {
  components: {},
  data() {
    return {
      tableData: [],
      checkedDetail: [],
      showEdit: [] // 控制显示及隐藏
    }
  },
  methods: {
    // 表格的新增
    rowClassName({ row, rowIndex }) {
      row.xh = rowIndex + 1
    },
    // 单选框选中数据
    handleDetailSelectionChange(selection) {
      this.checkedDetail = selection
    },
    // 点击新增更多
    handleAddBtn() {
      const obj = {}
      obj.mescode = ''
      obj.password = ''
      this.tableData.push(obj)
    },
    // 删除
    handleDeleteBtn() {
      if (this.checkedDetail.length === 0) {
        this.$alert('请先选择要删除的数据', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        this.$confirm('请是否确认删除该属性?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: (action) => {
            if (action === 'confirm') {
              const val = this.checkedDetail
              val.forEach((val, index) => {
                this.tableData.forEach((v, i) => {
                  if (val.xh === v.xh) {
                    this.tableData.splice(i, 1)
                  }
                })
              })
              this.$message({
                message: '删除成功,记得保存修改喔!',
                type: 'success'
              })
              this.$refs.tb.clearSelection()
              return
            } else {
              this.$message({
                message: '已取消删除操作',
                type: 'warning'
              })
              return
            }
          }
        })
      }
    },
    // 点击修改
    showUpdate(index, row) {
      console.log('index')
      this.showEdit[index] = true
      this.$set(this.showEdit, index, true) // 这里要用$set方法,否则页面状态不更新
    },
    // 取消修改
    cancelUpdate(index) {
      this.$confirm('取消修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$set(this.showEdit, index, false)
        })
        .catch(() => {})
    },
    // 提交修改
    submit(index, row) {
      // 发送请求,隐藏输入框
      this.$message({
        type: 'success',
        message: '已缓存,记得点击保存提交修改喔!',
        duration: 888,
        onClose: () => {
          this.$set(this.showEdit, index, false) // vue添加属性的方法
        }
      })
    }
  }
}
</script>
<style>
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue数据编辑表格是一种通过Vue框架实现的数据展示和编辑功能的表格组件。它可以方便地将数据以表格的形式展示,并且可以编辑其中的数据。 首先,我们需要使用Vue框架来构建这个数据编辑表格的组件。在Vue的模板中,我们可以使用v-for指令来遍历数据数组,动态地生成表格的行。同时,我们可以使用v-model指令来绑定每个单元的值与数据数组中对应元素的属性值,这样就能实现数据的双向绑定,即表格中的数据发生变化时,对应的数据也会修改。 其次,我们可以在表格中添加一些编辑功能,如增加删除、修改等。通过添加按钮和相应的事件方法,我们可以实现在表格增加一行数据、删除某一行数据,以及修改单元的值。在修改值时,我们可以通过绑定的v-model指令,将修改的值同步到数据数组中的对应属性上,以实现数据的更新。 此外,我们还可以对表格中的数据进行排序、筛选和分页等操作。通过添加相应的事件绑定和方法,我们可以实现根据某一列的值进行升序或降序排序,根据条件筛选出特定的数据,以及将数据分页展示。 最后,我们还可以对表格的样式进行自定义。可以使用CSS样式表来进行样式的设置,例如设置表格的边框、背景色、文字颜色等。 总的来说,通过使用Vue框架,我们可以方便地实现数据编辑表格的功能,包括数据的展示、编辑增加删除、排序、筛选和分页等。这样的表格组件可以帮助我们更好地处理数据,并提升用户的交互体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值