Home > Web Front-end > JS Tutorial > How to implement select drop-down list through Vue.js, the specific operations are as follows

How to implement select drop-down list through Vue.js, the specific operations are as follows

亚连
Release: 2018-06-01 17:02:58
Original
2214 people have browsed it

Below I will share with you an example of making a select drop-down list in Vue.js (ul-li tag imitates the select tag), which has a good reference value and I hope it will be helpful to everyone.

Goal: Use the ul-li tag combined with Vue.js knowledge to make a drop-down option list that imitates the select tag.

Knowledge points:

How to write and use components

Data transfer between components (use of props)

Data transfer between components (use of $emit)

Dynamic data binding (v-bind)

Custom event communication

Rendering:

1. Before any operation is performed, the drop-down list is hidden

##2. Click the input box to display the drop-down list

3. Click the list item, and the value of the input box changes accordingly

# #PS: In order to demonstrate the binding of two sets of data, data1 and data2, two lists are created in the example

html code:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>ul-li模仿select下拉菜单</title>
 <link rel="stylesheet" type="text/css" href="style.css" rel="external nofollow" />
 <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
 <p id="demo">
  <my-select btn-name=&#39;search&#39; v-bind:list=&#39;data1&#39; style=&#39;float: left;margin-right: 2rem;&#39;></my-select>
  <my-select btn-name=&#39;搜索&#39; v-bind:list=&#39;data2&#39; style=&#39;float: left;&#39;></my-select>
 </p>
</body>
</html>
Copy after login

JavaScript code

<script type="text/javascript">
//注册全局组件
//在my-select组件中套用ul-select组件,my-select为父组件ul-select为子组件
Vue.component(&#39;my-select&#39;, {
 //组件中data要写成函数形式
 data() {
  return {
   ulShow: false, //默认ul不显示,单击input改变ul的显示状态
   selectVal: &#39;&#39; //选项值,input的值与选项值动态绑定
  }
 },
 //父组件向子组件通信用props
 props: [&#39;btnName&#39;, &#39;list&#39;],
 template: `
  <p id="selectWrap">
   <p class="searchBox">
    <input type="text" :value="selectVal" @click=&#39;ulShow = !ulShow&#39;/>
    <a href="#" rel="external nofollow" class="search" v-text=&#39;btnName&#39;></a>
   </p>
    <my-ul v-show=&#39;ulShow&#39; v-bind:list=&#39;list&#39; v-on:receive=&#39;changeVal&#39;></my-ul>
   </p>
 `,
 methods: {
  changeVal(value) {
   this.selectVal = value
  }
 }
})
//子组件
Vue.component(&#39;my-ul&#39;, {
 props: [&#39;list&#39;],
 template: `
  <ul class="skill">
   <li v-for=&#39;item of list&#39; v-on:click=&#39;selectLi(item)&#39;>{{item}}</li>
  </ul>
 `,
 methods: {
  selectLi: function(item) {
   //$emit触发当前实例上的自定义事件 receive
   this.$emit(&#39;receive&#39;, item);
  }
 }
})
//创建Vue实例
new Vue({
 el: &#39;#demo&#39;,
 //定义两组数据分别传递到两个组件的li中,两个列表的操作互不影响
 data: {
  data1: [&#39;CSS&#39;, &#39;HTML&#39;, &#39;JavaScript&#39;],
  data2: [&#39;Vue.js&#39;, &#39;Node.js&#39;, &#39;Sass&#39;],
 }
})
</script>
Copy after login

CSS style

ul, li {
 margin: 0;
 padding: 0;
 list-style: none;
}
#selectWrap {
 width: 250px;
 padding: 2rem;
 background: #4682b4;
}
.searchBox input, .searchBox a {
 line-height: 1.5rem;
 height: 1.5rem;
 margin-bottom: 1rem;
 padding: 0 5px;
 vertical-align: middle;
 border: 1px solid #aaa;
 border-radius: 5px;
 outline: none;
}
.searchBox a {
 display: inline-block;
 text-decoration: none;
 background-color: #b1d85c;
}
.skill li {
 font-size: 18px;
 line-height: 2rem;
 height: 2rem;
 padding-left: 5px;
 cursor: pointer;
}
.skill li:hover {
 background-color: #008b45;
}
Copy after login
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Solve the query dynamic parameter passing problem in vue-router

vue data transfer--I have a special Implementation skills

Nodejs connection to mysql database and detailed explanation of basic knowledge points

The above is the detailed content of How to implement select drop-down list through Vue.js, the specific operations are as follows. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template