elementui table 增删改查

 

<el-button
        @click="addRow()"
        type="success"
        icon="el-icon-plus"
        style="margin-bottom:10px;"
        size="mini"
      >添加勾选项</el-button>
      <el-table :data="tableData" style="width: 100%" border size="small">
        <el-table-column label="名称">
          <template slot-scope="scope">
            <span v-if="scope.row.statusBtn===false">{{scope.row.name}}</span>
            <el-input size="mini" v-else-if="scope.row.statusBtn===true" v-model="tableName"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="分数">
          <template slot-scope="scope">
            <span v-if="scope.row.statusBtn===false">{{scope.row.score}}</span>
            <el-input
              type="number"
              v-else-if="scope.row.statusBtn===true"
              size="mini"
              v-model="tableScore"
            ></el-input>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="editCheck(scope.row)"
              v-if="scope.row.statusBtn===false"
              icon="el-icon-edit"
            >编 辑</el-button>
            <el-button
              type="success"
              size="mini"
              @click="sureCheck(scope.row)"
              v-else-if="scope.row.statusBtn===true"
              icon="el-icon-check"
            >保 存</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteFun(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

js部分:
//获得表格数据,增加一个参数statusBtn
if (res.data.success === true) {
            let arr = res.data.data
            for (let item of arr) {
              item['statusBtn'] = false
            }
            this.tableData = arr
}
// 判断值内容是否全部填写
    flagFun () {
      const flag = this.tableData.every(item => {
        if (item.name !== '' && item.score !== '') {
          return true
        } else {
          return false
        }
      })
      return flag
    },
//勾选弹框表格添加一行
    addRow () {
      if (this.tableData.length === 0) {
        this.tableData.push({ name: '', score: '', statusBtn: true })
      } else {
        for (let i of this.tableData) {
          if (i.statusBtn) return this.$message.warning("请先保存当前编辑项");
        }
        if (this.flagFun()) {
          this.tableData.push({ name: '', score: '', statusBtn: true })
        } else {
          this.$message({
            message: '名称和分数不能为空',
            type: 'warning'
          })
        }
      }
},
editCheck (row) {
      //点击修改 判断是否已经保存所有操作
      for (let i of this.tableData) {
        if (i.statusBtn && i.id != row.id) {
          this.$message.warning("请先保存当前编辑项");
          return false;
        }
      }
      row.statusBtn = true
      this.tableName = row.name
      this.tableScore = row.score
},
// 勾选弹框保存按钮
    sureCheck (row) {
      if (row.id !== '' && row.id !== undefined) {
        this.$http
          .post('', {
            name: this.tableName,
            score: this.tableScore,
            indicationId: this.keyId,
            id: row.id
          })
          .then(res => {
            if (res.data.success === true) {
              this.$message.success('编辑成功')
              this.tableName = ''
              this.tableScore = ''
              this.checkTable()
            } else {
              this.$confirm(res.data.msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
              }).then(() => {

              }).catch(() => {
                return false
              })
            }
          })
          .catch(function (error) {
            console.log(error)
          })
      } else {
        if (this.tableName === '' && this.tableScore === '') {
          Message.warning('请补全数据')
          return
        }
        this.$http
          .post('', {
            name: this.tableName,
            score: this.tableScore,
            indicationId: this.keyId
          })
          .then(res => {
            if (res.data.success === true) {
              row.statusBtn = false
              this.$message.success('保存成功')
              this.tableName = ''
              this.tableScore = ''
              this.checkTable()
            } 
          })
          .catch(function (error) {
            console.log(error)
          })
      }
},
// 删除一行
    deleteFun (scope) {
      if (!scope.row.id) {
        this.tableData.splice(scope.$index, 1)
      } else {
        this.$confirm('确认是否删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        })
          .then(() => {
            this.$http
              .post('', {
                id: scope.row.id
              })
              .then(res => {
                if (res.data.success === true) {
                  this.$message.success('删除成功')
                  this.checkTable()
                }
              })
              .catch(function (error) {
                console.log(error)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值