2016-12-20 11 views
0

と私はこの使用・セレクトマルチ - vueJS2

https://vuejs.org/v2/examples/select2.html

を見つけVuesJS2

を選択し、複数のフィールドのSelectセレクトプラグインを使用するために何か良い方法はありますが、これは、単一の選択ですが、私ドン選択した項目の配列を出力する方法を知りません。

Vue.component('select2', { 
 
    props: ['options', 'value'], 
 
    template: '#select2-template', 
 
    mounted: function() { 
 
    var vm = this 
 
    $(this.$el) 
 
     .val(this.value) 
 
     // init select2 
 
     .select2({ data: this.options }) 
 
     // emit event on change. 
 
     .on('change', function() { 
 
     //vm.$emit('input', this.value) 
 
     //vm.$emit('input', this.value) 
 
     vm.$emit('input', this.value) 
 
     }) 
 
    }, 
 
    watch: { 
 
    value: function (value) { 
 
     // update value 
 
     $(this.$el).select2('val', value) 
 
    }, 
 
    options: function (options) { 
 
     // update options 
 
     $(this.$el).select2({ data: options }) 
 
    } 
 
    }, 
 
    destroyed: function() { 
 
    $(this.$el).off().select2('destroy') 
 
    } 
 
}) 
 

 
var vm = new Vue({ 
 
    el: '#el', 
 
    template: '#demo-template', 
 
    data: { 
 
    selected: [], 
 
    options: [ 
 
     { id: 1, text: 'Hello' }, 
 
     { id: 2, text: 'World' } 
 
    ] 
 
    } 
 
})
html, body { 
 
    font: 13px/18px sans-serif; 
 
} 
 
select { 
 
    min-width: 300px; 
 
}
<!DOCTYPE html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/dist/css/select2.min.css"> 
 
<script src="https://unpkg.com/[email protected]"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script> 
 

 
<div id="el"></div> 
 

 
<!-- using string template here to work around HTML <option> placement restriction --> 
 
<script type="text/x-template" id="demo-template"> 
 
    <div> 
 
    <p>Selected: {{ selected }}</p> 
 
    <select2 :options="options" v-model="selected"> 
 
     <option disabled value="0">Select one</option> 
 
    </select2> 
 
    </div> 
 
</script> 
 

 
<script type="text/x-template" id="select2-template"> 
 
    <select> 
 
    <slot></slot> 
 
    </select> 
 
</script>

+1

試したコードスニペットを使用しても問題はありませんか? – Saurabh

答えて

0

私は今日、まさにこの問題に出くわしと他の誰かが同じことを疑問に思っている場合に、私は、答えを投稿したかったです。

select2から放射する場合は、select2の結果を選択する適切な方法を使用する必要があります。選択したすべてのid Sを含む配列を持つ:

これは、我々は(selectedあなたの例では)Vモデルを記入しますjquThisを使用することを確認します

vm.$emit('input', this.value) 

vm.$emit('input',  $(this).val()) 

と交換。

関連する問題