鍍金池/ 問(wèn)答/HTML/ 表格中多選框,選中一個(gè)就往數(shù)組里面添加這一條的數(shù)據(jù),但是再點(diǎn)擊不選中,也會(huì)添加,

表格中多選框,選中一個(gè)就往數(shù)組里面添加這一條的數(shù)據(jù),但是再點(diǎn)擊不選中,也會(huì)添加,如何解決

問(wèn)題描述

表格中多選框,選中一個(gè)就往數(shù)組里面添加這一條的數(shù)據(jù),但是再點(diǎn)擊不選中,也會(huì)添加,如何解決

問(wèn)題出現(xiàn)的環(huán)境背景及自己嘗試過(guò)哪些方法

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)

<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})

   },

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

錯(cuò)誤:我點(diǎn)一下,選中,添加數(shù)據(jù),再次點(diǎn)擊不選中數(shù)組里面也會(huì)添加數(shù)據(jù)

回答
編輯回答
壞脾滊

v-model 會(huì)幫你維護(hù)一個(gè) list,不會(huì)重復(fù) push...

2017年5月14日 03:56
編輯回答
賤人曾

v-model?

2017年8月3日 04:04
編輯回答
萌二代

用的js的判斷數(shù)組 由于我的數(shù)組里面是一個(gè)對(duì)象所有就定義了一個(gè)新的數(shù)組
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)
   },

這樣算是解決了。

2017年6月28日 00:05