Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  
  <link rel="stylesheet" href="https://unpkg.com/select2@4.0.3/dist/css/select2.min.css">
  
  <script src="https://unpkg.com/jquery@3.1.1"></script>
  <script src="https://unpkg.com/vue@2.3.2/dist/vue.js"></script>
  <script src="https://unpkg.com/select2@4.0.3"></script>
    
</head>
<body>
  
  <div id="app"></div>
  <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>
    
      <p>Selected: {{ selected2 }}</p>
      <select2 :options="options" v-model="selected2">
        <option disabled value="0">Select one</option>
      </select2>
      <p>Selected: {{ selected3 }}</p>
      <select2 :options="options" v-model="selected3">
        <option disabled value="0">Select one</option>
      </select2>
    </div>
  </script>
  <script type="text/x-template" id="select2-template">
    <select>
      <slot></slot>
    </select>
  </script>
</body>
</html>
 
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)
      })
  },
  watch: {
    value: function (value) {
      // update value
      $(this.$el).val(value)
    },
    options: function (options) {
      // update options
      $(this.$el).select2({ data: options })
    }
  },
  destroyed: function () {
    $(this.$el).off().select2('destroy')
  }
})
var vm = new Vue({
  el: '#app',
  template: '#demo-template',
  data: {
    selected: 0,
    selected2: 0,
    selected3: 0,
    options: [
      { id: 1, text: 'Hello' },
      { id: 2, text: 'World' }
    ]
  }
})
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers