表格中多选框,选中一个就往数组里面添加这一条的数据,但是再点击不选中,也会添加,如何解决

问题描述

表格中多选框,选中一个就往数组里面添加这一条的数据,但是再点击不选中,也会添加,如何解决

问题出现的环境背景及自己尝试过哪些方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

<el-checkbox @change="getRow(scope.row)" v-if="scope.row.platformUsername"></el-checkbox>

getRow:function(row){

   this.nextDataArr.push({"platfromLogo":row.platfromLogo,"platformZid":row.platformZid,"userPlat formZid":row.userPlatnameWithZid[0].userPlatformZid})

   },

你期待的结果是什么?实际看到的错误信息又是什么?

错误:我点一下,选中,添加数据,再次点击不选中数组里面也会添加数据

阅读 5.1k
3 个回答

用的js的判断数组 由于我的数组里面是一个对象所有就定义了一个新的数组
getRow:function(row){

if (this.nextDataArrry.indexOf(row.userPlatnameWithZid[0].userPlatformZid) === -1) {
     this.nextDataArrry.push(row.userPlatnameWithZid[0].userPlatformZid)this.nextDataArr.push({"platfromLogo":row.platfromLogo,"platformZid":row.platformZid,"userPlatformZid":row.userPlatnameWithZid[0].userPlatformZid})
        } else {
            this.nextDataArrry.splice(this.nextDataArrry.indexOf(row.userPlatnameWithZid[0].userPlatformZid), 1)
            this.nextDataArr.splice(this.nextDataArrry.indexOf(row.userPlatnameWithZid[0].userPlatformZid), 1)
        }
        console.log(this.nextDataArrry)
        console.log(this.nextDataArr)
   },

这样算是解决了。

v-model 会帮你维护一个 list,不会重复 push...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题