Vue 可编辑表格 function中更改edit状态,span中不会实时更新

需求:点击编辑按钮,可编辑表格内容,再次点击,可保存
问题:第二次点击edit已取反,但是组件没有相应的跳转变化

原因:
主要参考了这个链接
https://www.cnblogs.com/ckmouse/p/11490604.html

sol:
处理后台数据时,增加edit列

code:

<template>
    <div class="app-container">
        <el-table
          :data="MoleData"
          style="width: 95%"
          border
          highlight-current-row>

            <el-table-column key="1" prop="moleId" label="ID">
              <template scope="scope">
                  <!--通过v-show和对行数据中的编辑状态变量取反来动态切换编辑状态-->
                  <!--v-show相比v-if有更高的初始渲染消耗,适合频繁切换的场合-->
                  <span>{{scope.row.moleId}}</span>
              </template>
            </el-table-column>


            <!-- 只显示当前打分 -->
            <el-table-column key="6" v-if="!readOnly" prop="grade[0]" label="打分">
              <template scope="scope">
                <el-input v-model="scope.row.grade.score" placeholder="请输入内容" v-show="scope.row.showEdit"></el-input> 
                <!-- <span v-show="!scope.row.showEdit" v-if="scope.row.grade.score">{{scope.row.grade.score}}</span> -->
                <span v-show="!scope.row.showEdit">{{scope.row.grade.score}}</span>
                <!-- <span v-else>暂未打分</span> -->

              </template>
            </el-table-column>

            <el-table-column key="7" v-if="!readOnly" label="操作">
              <template scope="scope">
              <!-- scope可以取到行索引(scope.$index)和行数据(scope.row) -->
                  <el-button type="primary" @click="handleEdit(scope.$index, scope.row)">
                      {{scope.row.showEdit?'保存':'修改'}}
                      <!-- {{scope.row.showEdit}} -->
                  </el-button>
              </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>

import {getMoleEva} from '../../request/api.js'
import {updateMoleEva} from '../../request/api.js'
import store from '../../store'

export default {
    data() {
      this.colConfigs = [
      { prop: 'grader', label: 'grader' },
      { prop: 'score', label: 'ranking' },
      ]
        return {
          delVisible: false,//删除提示弹框的状态
          MoleData: [],//已打分mole
          MoleDataUneva: [], //未打分mole数据
          showGrader1: true,
          showGrader2: true,
          showGrader3: true,
          readOnly: true
        }
    },
    mounted() {
      this.getInitMole()
    },
    methods: {
      getInitMole(){
        getMoleEva({
          userName: store.state.user.username
        }).then(res => {
          // 获取数据成功后的其他操作
          for (var mole in res) {
            this.MoleData.push({"moleId": res[mole].moleId, "prop1": res[mole].prop1, "prop2": res[mole].prop2, "prop3": res[mole].prop3, "grade": res[mole].grade, "showEdit": false})
          }
          console.log(this.MoleData)
        }) 
      },
      // 获取操作的一行的数据row,将其中的编辑状态变量showEdit取反,切换状态
      // 也可以去掉,在@click中直接取反
      // 只有保存才需要call后台
      handleEdit(index, row) {
          console.log("index: " + index)
          row.showEdit = !row.showEdit
          console.log("showEdit: " + row.showEdit)
          console.log(row)
      }
    },
}
</script>

<style scoped>
    *{margin:0;padding:0}
    .app-container{font:15px/1.3 'Open Sans', sans-serif;color: #5e5b64;text-align:center;}
    a, a:visited {outline:none;color:#389dc1;}
    a:hover{text-decoration:none;}
    section, footer, header, aside, nav{display: block;}
    /* page.css */
    .order {padding: 20px}
</style>

关键点:
Line 77,为每一行的showEdit赋值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值