2017-04-18 21 views
0

當我編輯帖子時,我使用vue js傳遞選定多個選項的值。 我使用元素UI來選擇選項。如何在不使用v-for vue js的情況下調用所選多個項目選項的ID?

PostController中

return Post::with('category')->with('tag')->find($id); 

Post.vue

<el-select v-model="form.tag_id" multiple placeholder="Select some"> 
    <el-option v-for="tag in tags" :key="tag.id" :label="tag.name" 
    :value="tag.id"> 
    </el-option> 
    </el-select> 

<script> 
data() { 
return { 
    tags: [], 
    form: new Form({ 
    tag: [], 
    tag_id: [], 
    }), 
} 
// fetch all tags list 
axios.get('/' + this.$route.params.trans + '/adminGetTags') 
.then(({data}) => this.tags = data) 
//fetch post and tag which related to post 
axios.get('/' + this.$route.params.trans + '/editPost/' + 
this.$route.params.id) 
.then(({data}) => { 
//.... 
this.form.tag = data.tag 
this.form.tag_id = data.tag 
}) 

</script> 

我需要調用選擇多個項目的id這樣

this.form.tag_id = data.tag.id 

BUIT它會給出一個錯誤(無法讀取財產'長度'未定義)

但是,如果我使用v-它會工作,不幸的是我不能在選擇標籤中使用v-model和v-for。任何想法如何解決這個問題?

結果 The result of that code

development

+0

你試過'this.form.tag_id = [data.tag.id]'如果我記得沒錯VUE將檢測'multiple'指令,並設置期望類型的數組 –

+0

賈斯汀,它不工作。拋出一個錯誤'[Vue警告]:子項必須被鍵入:'。但是,如果我將el-select更改爲正常的多選,它不會給任何東西。沒有錯誤,它是空的。 – lolagi

+0

檢查您的data.tag.id.在element.io https://jsfiddle.net/deepaksingh/fpjaqcfn/ – Deepak

回答

0

我只是用循環在腳本部分,它的工作很適合我。

 var j = 0; 
     for(var i=1; i<= data.tag.length; i++) 
     { 

      this.form.tag_id[j] = data.tag[j].id 
      j += 1 
     } 
     return this.form.tag_id 
相關問題