首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用SelectBoxIt jq插件的Vue.js

不使用SelectBoxIt jq插件的Vue.js
EN

Stack Overflow用户
提问于 2017-04-21 14:37:28
回答 2查看 560关注 0票数 0

一个页面中有许多动态生成的复选框。我想应用jquery selectBoxIt (http://gregfranko.com/jquery.selectBoxIt.js/)插件。我使用的是Vue js。我该把它放哪儿

代码语言:javascript
复制
$('.cl_v').selectBoxIt({ theme: 'default', 'defaultText': 'select', autoWidth: false });

为了将插件与select元素连接起来?应用于复选框的类是cl_v。我已经将上面的代码放置在创建的中:挂载: placed:。但这不起作用。如何在Vue.js中使用这个插件?谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-04-21 16:55:14

您应该创建一个包装组件。这就是你让VueJS和jQuery表现得很好的方式。

如果您的selectBoxIt所需的唯一工作内容是上面的调用,那么您只需要下面的mounted部分:

代码语言:javascript
复制
mounted() {
  $(this.el).selectBoxIt({ theme: 'default', 'defaultText': 'select', autoWidth: false });
}
票数 1
EN

Stack Overflow用户

发布于 2018-03-09 10:26:35

请查看正式文件:包装组件

下面是示例代码:

代码语言:javascript
复制
Vue.component('selectBoxIt', {
  props: ['options', 'value'],
  template: '#selectBoxIt-template',
  mounted: function () {
    var vm = this
    $(this.$el)
      .val(this.value)
      .trigger('change')
      // emit event on change.
      .on('change', function () {
        vm.$emit('input', this.value)
      })
  },
  watch: {
    value: function (value) {
      // update value
      $(this.$el).val(value)
    }
  }
});

var app = new Vue({
  el: '#app',
  template: '#demo-template',
  data: {
    fruits: [{id:1,name:'apple'},{id:2,name:'banana'}],
    selectId: 0
  },
  mounted: function() {
    $('#fruits').selectBoxIt();
  }
});
代码语言:javascript
复制
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" />
 <link type="text/css" rel="stylesheet" href="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.selectboxit/3.8.0/jquery.selectBoxIt.css" />


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.selectboxit/3.8.0/jquery.selectBoxIt.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<div id="app"></div>

<script type="text/x-template" id="demo-template">
<div>
  <p>selectId: {{ selectId }}</p>
  <selectBoxIt id="fruits" :options="fruits" v-model="selectId">
  </selectBoxIt>
</div>
</script>

<script type="text/x-template" id="selectBoxIt-template">
  <select>
    <option v-for="option in options" :value="option.id">{{ option.name }}</option>
  </select>
</script>

下面是JSFiddle:https://jsfiddle.net/5qnqkjr1/131/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43545450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档