2017-07-17 117 views
0

我有一個對象,數組列表...如何從返回的數據組件vuejs拼接一個子數組?

enter image description here

..this是一個道具在VUE JS聲明的數據:

data: function(){ 
    return{ 
     listaSelezionati:this.selezionati 
    } 
} 

..wich呈現在v型的

<div class="row" style="margin-bottom:20px;" v-for="(selezionato,index) in listaSelezionati"> 

..inside for循環有一個按鈕誰調用一個函數

<div class="remove pull-right" v-on:click="rimuovi(index)"></div> 

..with這個功能我想拼接對象「listaSelezionati」採用「指數」的子陣列,但我不知道怎麼做吧..

這裏是我曾嘗試:

methods:{ 
    rimuovi : function(index){ 
     alert(index); 
     return{ 
      this.listaSelezionati[index][0].splice(index,1) 
     } 
    } 
} 

但它並沒有,任何人有什麼建議?

EDIT1 我想知道,如果return{this.listaSelezionati應該是編輯組件數據

+0

this.listaSelezionati [index] [0]將指向子數組對象。你正在拼接功能的對象。 你想刪除特定索引的子數組嗎? –

+0

是否有另一個'v-for'的地方? – Bert

+0

@BertEvans否是唯一的一個,但是在v-for之前有一個v-for –

回答

0

如果你想刪除的索引整個子陣列中的所有你需要做的是這樣的:

methods:{ 
    rimuovi : function(index){ 
    this.listaSelezionati.splice(index,1) 
    } 
} 

有沒有必要返回一個值; splice更新它被調用的數組。

+0

哈哈它比我想的更簡單,謝謝你@Bert –

0

看起來你會太深到您的數組一個正確的做法。

this.listaSelezionati[index] 

是一個數組但

this.listaSelezionati[index][0] 

看起來像一個陣列是一個對象的第一個元素。所以現在看起來你正在一個位置索引處拼接一個Object。這應該會產生一個錯誤。 我想你需要:

rimuovi : function(outterIndex, innerIndex){ 
    alert(outterIndex, innerIndex); 
    return{ 
     this.listaSelezionati[outterIndex].splice(innerIndex,1) 
    } 
} 

<div class="remove pull-right" v-on:click="rimuovi(index, innerIndex)"></div> 
// or 
<div class="remove pull-right" v-on:click="rimuovi(outterIndex, innerIndex)"></div> 

我沒有測試它,但至少它應該讓你更接近

+0

嗯...有趣的是,我應該使用索引來調用outterIndex和innerIndex以及? –

+0

是的,在你的HTML你必須通過它們兩個 –