vuejs2并选择了选择问题

时间:2017-05-31 10:19:31

标签: vue.js vuejs2

美好的一天,请看this bin。它写成Vue 0.12版本和chosen js。如何使其与vue2版本一起使用。我真的需要这个作为指令而不是作为一个组件。

`<div id='search`-results'>
Vue model value <br>
{{city | json}}
<hr>
Select value:
<!-- note the `v-model` and argument for `v-chosen`  --> 
<select class="cs-select" v-model="city" options="cities" v-chosen="city"></select>

<select v-model="city" options="cities"></select>

Vue.directive('chosen', {
    twoWay: true, // note the two-way binding
    bind: function () {
        $(this.el)
            .chosen({
                inherit_select_classes: true,
                width: '30%',
                disable_search_threshold: 999
            })
            .change(function(ev) {
                this.set(this.el.value);
            }.bind(this));
    },
    update: function(nv, ov) {
        // note that we have to notify chosen about update
        $(this.el).trigger("chosen:updated");
    }
});

var vm = new Vue({
  data: {
      city: 'Toronto',
      cities: [{text: 'Toronto', value: 'Toronto'}, 
               {text: 'Orleans', value: 'Orleans'}]
  }
}).$mount("#search-results");

1 个答案:

答案 0 :(得分:2)

这里它被实现为支持v-model的包装器组件和选项的插槽。这使它成为标准select小部件的替代品,至少就基本功能而言。很高兴updated()会注意到对选项列表和值的更改。

由于Vue2不支持双向指令,我不相信有一种方法可以将其作为指令实现。如果你真的需要,你会想要使用Vue1。

&#13;
&#13;
var vm = new Vue({
  el: '#search-results',
  data: {
    city: 'Toronto',
    cities: [{
      text: 'Toronto',
      value: 'Toronto'
    }, {
      text: 'Orleans',
      value: 'Orleans'
    }]
  },
  components: {
    'chosenSelect': {
      template: '<select class="cs-select" v-model="proxyValue" ><slot></slot></select>',
      props: ['value', 'options'],
      computed: {
        proxyValue: {
          get() {
            return this.value;
          },
          set(newValue) {
            this.$emit('input', newValue);
          }
        }
      },
      mounted() {
        $(this.$el)
          .chosen({
            inherit_select_classes: true,
            width: '30%',
            disable_search_threshold: 999
          })
          .change((ev) => {
            this.proxyValue = ev.target.value;
          });
      },
      updated() {
        $(this.$el).trigger('chosen:updated');
      }
    }
  }
});

setTimeout(() => { vm.cities.push({text: 'Houston', value: 'Worth it'}); }, 1000);
&#13;
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.proto.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script>
<div id='search-results'>
  Vue model value
  <br> {{city | json}}
  <hr> Select value:
  <chosen-select v-model="city">
    <option v-for="item in cities" :value="item.value">{{item.text}}</option>
  </chosen-select>

  <select v-model="city">
    <option v-for="item in cities" :value="item.value">{{item.text}}</option>
  </select>
</div>
&#13;
&#13;
&#13;