【问题标题】:data binding to custom dropdown with Vuejs使用 Vuejs 将数据绑定到自定义下拉列表
【发布时间】:2017-04-28 10:38:28
【问题描述】:

我正在创建一个自定义下拉元素,包含以下 HTML 和一些 CSS:

<div id="dd" class="col-md-4 col-lg-4 wrapper-dropdown-3 left-divider" tabindex="1">
    <span>Age</span>
    <ul class="dropdown">\
      <li><a href="#"><i class="icon-envelope icon-large"></i>1 Month Old</a></li>
      <li><a href="#"><i class="icon-truck icon-large"></i>11 Month Old</a></li>
    </ul>
</div>

但是如何将下拉列表选择的值绑定到某个 vue 变量?

我尝试了&lt;span v-bind="dataVar"&gt;Age&lt;/span&gt;,但dataVar 在您更改下拉列表的值时不会更改。我已经定义了dataVar,如下所示

data: function() { 
  return { 
    dataVar: ''
  }
},

如何在dataVar 变量中选择下拉列表的值?

jsfiddle 中的工作下拉列表,Go to Foo 视图中。

【问题讨论】:

  • 为什么不创建一个Vue组件?
  • @GerardReches 看起来很有希望,但我仍然如何将变量绑定为 HTML 中的值正在被 javascript 更改。
  • 您能否在问题中添加您想要实现的目标?我不明白您将这段代码绑定到 vue 变量是什么意思。
  • 你不能只使用一种方法吗?更新小提琴:jsfiddle.net/0xzkv7ko

标签: javascript jquery html vue.js


【解决方案1】:

我重写了你的自定义选择。 您需要使用:v-model。是的,它不仅适用于输入。 See docs

示例用法:

 <h3>Selected: {{ selected.name }}</h3>

 <custom-select :options="options"
                value-key="id" 
                label-key="name"
                v-model="selected"></custom-select>


 <div style="margin-top: 40px">
     <h3>Wihtout labels, for simple values, like numbers</h3>

     <custom-select :options="options"
                    v-model="selected"></custom-select>
 </div>

使用干净的源代码享受DEMO

【讨论】:

  • 我想控制从父组件打开下拉菜单,我该如何使用这种方法来做到这一点,我应该将opened 设为props 还是传递另一个prop 并让其监视?
  • 是的,使用“default: false”将“opened”作为道具。
【解决方案2】:

修改:

1.function DropDown (el,onChange) { this.onChange = onChange

2.obj.opts.on('click', function () { .... obj.onChange(obj.val)

3.&lt;span&gt; this is foo &lt;/span&gt; &lt;span&gt; my value is {{selected}} &lt;/span&gt;\

https://jsfiddle.net/postor/z79b0ksk/2/

这种情况有两种方法:

  1. 我选择使用绑定更改并使用 vm.$set,因为您已经拥有更改句柄

  2. 您可以选择在 Vue 管理中进行选择,例如 data:()=&gt;{selected:null,options:['one month','11 month']} &lt;div v-for="opt in options" v-on:click="selected=opt"&gt;

/* global $:true */
function DropDown (el,onChange) {
  this.dd = el
  this.placeholder = this.dd.children('span')
  this.opts = this.dd.find('ul.dropdown > li')
  this.val = ''
  this.index = -1
  this.initEvents()
  this.onChange = onChange
}

DropDown.prototype = {
  initEvents: function () {
    var obj = this

    obj.dd.on('click', function (event) {
      $(this).toggleClass('active')
      return false
    })

    obj.opts.on('click', function () {
      var opt = $(this)
      obj.val = opt.text()
      obj.index = opt.index()
      obj.placeholder.text(obj.val)
      obj.onChange(obj.val)
    })
  },
  getValue: function () {
    return this.val
  },
  getIndex: function () {
    return this.index
  }
}


// Define some components
const Foo = {
  template: '<div>\
        <div id="dd" class="col-md-4 col-lg-4 wrapper-dropdown-3 left-divider" tabindex="1">\
        <span>Age</span>\
        <ul class="dropdown">\
          <li><a href="#"><i class="icon-envelope icon-large"></i>1 Month Old</a></li>\
          <li><a href="#"><i class="icon-truck icon-large"></i>11 Month Old</a></li>\
        </ul>\
      </div>\
  <div class="card row" >\
  <div class="col-xs-5" style="height: 100px; background-color: red; position: fixed;">\
  <span> this is foo </span>\
    </div>\
      <div class="col-xs-7" style="height: 100px; background-color: Yellow">\
  <span> this is foo </span>\
  <span> my value is {{selected}} </span>\
    </div>\
  </div>\
</div>',
  data: function() { return {  
  	selected:null
  }},
  mounted () {
	 new DropDown($(this.$el.querySelector('#dd')),(val)=>{
   	this.selected = val
   })    
  }
};

const  Bar = {
  template: '<p>This is bar!</p>',
  data: function() { return {  
  }}
};


// Create a router instance.
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({
  mode: 'history',
    scrollBehavior: (to, from, savedPosition) => {
    if (to.hash) {
      return {selector: to.hash}
    } else {
      return {x: 0, y: 0}
    }
    },
    routes: [
    { path: '/foo', component: Foo },
    { path: '/bar', component: Bar },
  ]
})

const app = new Vue({
  router
}).$mount('#app')
猜你喜欢
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
相关资源
最近更新 更多