index.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue.js wrapper component example (jquery plugin: select2)</title>
  6. <!-- Delete ".min" for console warnings in development -->
  7. <script src="../../dist/vue.min.js"></script>
  8. <script src="https://unpkg.com/jquery"></script>
  9. <script src="https://unpkg.com/select2@4.0.3"></script>
  10. <link href="https://unpkg.com/select2@4.0.3/dist/css/select2.min.css" rel="stylesheet">
  11. <style>
  12. html, body {
  13. font: 13px/18px sans-serif;
  14. }
  15. select {
  16. min-width: 300px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="el">
  22. </div>
  23. <!-- using string template here to work around HTML <option> placement restriction -->
  24. <script type="text/x-template" id="demo-template">
  25. <div>
  26. <p>Selected: {{ selected }}</p>
  27. <select2 :options="options" v-model="selected">
  28. <option disabled value="0">Select one</option>
  29. </select2>
  30. </div>
  31. </script>
  32. <script type="text/x-template" id="select2-template">
  33. <select>
  34. <slot></slot>
  35. </select>
  36. </script>
  37. <script>
  38. Vue.component('select2', {
  39. props: ['options', 'value'],
  40. template: '#select2-template',
  41. mounted: function () {
  42. var vm = this
  43. $(this.$el)
  44. .val(this.value)
  45. // init select2
  46. .select2({ data: this.options })
  47. // emit event on change.
  48. .on('change', function () {
  49. vm.$emit('input', this.value)
  50. })
  51. },
  52. watch: {
  53. value: function (value) {
  54. // update value
  55. $(this.$el).val(value).trigger('change')
  56. },
  57. options: function (options) {
  58. // update options
  59. $(this.$el).select2({ data: options })
  60. }
  61. },
  62. destroyed: function () {
  63. $(this.$el).off().select2('destroy')
  64. }
  65. })
  66. var vm = new Vue({
  67. el: '#el',
  68. template: '#demo-template',
  69. data: {
  70. selected: 0,
  71. options: [
  72. { id: 1, text: 'Hello' },
  73. { id: 2, text: 'World' }
  74. ]
  75. }
  76. })
  77. </script>
  78. </body>
  79. </html>