Vue.js做select下拉列表的实例(ul-li标签仿select标签)

yizhihongxing

下面是详细讲解“Vue.js做select下拉列表的实例(ul-li标签仿select标签)”的完整攻略。

思路

在使用Vue.js做select下拉列表的时候,我们的思路是实现一个ul-li标签的下拉列表,通过Vue.js的指令动态地生成并管理列表,实现与原生select标签同样的效果。

实现步骤

1. 确定数据模型

在Vue.js中,我们需要为ul-li下拉列表确定一个数据模型。在这个数据模型中,我们需要包含以下信息:

  • 下拉列表的选项列表;
  • 当前选中的选项。
data: {
  selectList: [
    {value: '1', label: '选项1'},
    {value: '2', label: '选项2'},
    {value: '3', label: '选项3'},
    {value: '4', label: '选项4'},
  ],
  selectedValue: '1'
}

2. 渲染下拉列表

接下来,我们需要在页面中渲染这个下拉列表。我们可以通过v-for指令来循环渲染每个选项,并通过v-bind指令进行数据绑定。

<div class="select-container">
  <div class="select-header" v-on:click="showList = !showList">{{selectedLabel}}</div>
  <ul class="select-list" v-if="showList">
    <li v-for="(item,index) in selectList" v-on:click="setSelected(item.value)">
      {{item.label}}
    </li>
  </ul>
</div>

3. 实现选择下拉列表的功能

为了使得下拉列表具有选择选项的功能,我们需要在Vue.js中实现以下两个功能:

  • 显示选中的选项;
  • 点击选项后更新选中的选项。
computed: {
  selectedLabel: function() {
    return this.selectList.filter(item => item.value === this.selectedValue)[0].label
  }
},
methods: {
  setSelected: function(val) {
    this.selectedValue = val
    this.showList = false
  }
}

4. 添加样式

最后,我们需要为下拉列表添加一些CSS样式。

.select-container {
  position: relative;
  display: inline-block;
  width: 120px;
  height: 32px;
}

.select-header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 0 20px;
  line-height: 32px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
}

.select-list {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 10;
  width: 100%;
  max-height: 128px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  border: 1px solid #ccc;
  border-top: none;
  border-radius: 0 0 4px 4px;
  list-style: none;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,.2);
}

.select-list li {
  box-sizing: border-box;
  width: 100%;
  height: 32px;
  padding: 0 20px;
  line-height: 32px;
  cursor: pointer;
  color: #333;
}

.select-list li:hover {
  background-color: #f0f0f0;
}

示例

下面是两个Vue.js做select下拉列表的实例,其中一个使用了静态数据,另一个使用了动态数据。

示例1: 静态数据

<div id="app">
  <div class="select-container">
    <div class="select-header" v-on:click="showList = !showList">{{selectedLabel}}</div>
    <ul class="select-list" v-if="showList">
      <li v-for="(item,index) in selectList" v-on:click="setSelected(item.value)">
        {{item.label}}
      </li>
    </ul>
  </div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    selectList: [
      {value: '1', label: '选项1'},
      {value: '2', label: '选项2'},
      {value: '3', label: '选项3'},
      {value: '4', label: '选项4'},
    ],
    selectedValue: '1',
    showList: false
  },
  computed: {
    selectedLabel: function() {
      return this.selectList.filter(item => item.value === this.selectedValue)[0].label
    }
  },
  methods: {
    setSelected: function(val) {
      this.selectedValue = val
      this.showList = false
    }
  }
})
</script>

<style>
.select-container {
  position: relative;
  display: inline-block;
  width: 120px;
  height: 32px;
}

.select-header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 0 20px;
  line-height: 32px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
}

.select-list {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 10;
  width: 100%;
  max-height: 128px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  border: 1px solid #ccc;
  border-top: none;
  border-radius: 0 0 4px 4px;
  list-style: none;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,.2);
}

.select-list li {
  box-sizing: border-box;
  width: 100%;
  height: 32px;
  padding: 0 20px;
  line-height: 32px;
  cursor: pointer;
  color: #333;
}

.select-list li:hover {
  background-color: #f0f0f0;
}
</style>

示例2: 动态数据

<div id="app">
  <div class="select-container">
    <div class="select-header" v-on:click="showList = !showList">{{selectedLabel}}</div>
    <ul class="select-list" v-if="showList">
      <li v-for="(item,index) in selectList" v-on:click="setSelected(item.value)">
        {{item.label}}
      </li>
    </ul>
  </div>
  <button v-on:click="addOption">添加选项</button>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    selectList: [
      {value: '1', label: '选项1'},
      {value: '2', label: '选项2'},
      {value: '3', label: '选项3'},
      {value: '4', label: '选项4'},
    ],
    selectedValue: '1',
    showList: false
  },
  computed: {
    selectedLabel: function() {
      return this.selectList.filter(item => item.value === this.selectedValue)[0].label
    }
  },
  methods: {
    setSelected: function(val) {
      this.selectedValue = val
      this.showList = false
    },
    addOption: function() {
      this.selectList.push({
        value: '5',
        label: '选项5'
      })
    }
  }
})
</script>

<style>
.select-container {
  position: relative;
  display: inline-block;
  width: 120px;
  height: 32px;
}

.select-header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 0 20px;
  line-height: 32px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
}

.select-list {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 10;
  width: 100%;
  max-height: 128px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  border: 1px solid #ccc;
  border-top: none;
  border-radius: 0 0 4px 4px;
  list-style: none;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,.2);
}

.select-list li {
  box-sizing: border-box;
  width: 100%;
  height: 32px;
  padding: 0 20px;
  line-height: 32px;
  cursor: pointer;
  color: #333;
}

.select-list li:hover {
  background-color: #f0f0f0;
}
</style>

以上就是使用Vue.js做select下拉列表的实例(ul-li标签仿select标签)的完整攻略,希望可以对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue.js做select下拉列表的实例(ul-li标签仿select标签) - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • 级联样式文件共通样式整理

    以下是“级联样式文件共通样式整理”的完整攻略: 什么是级联样式文件共通样式整理 级联样式表(Cascading Style Sheets,简称CSS)是一种用于网页样式设计的语言,可以为网页增加丰富的视觉效果。在大型网站中,为了便于维护和管理,需要对不同页面中的相同样式进行整理和共通化,这就是级联样式文件共通样式整理。 具体步骤 1. 收集页面中的共通样式 …

    css 2023年6月9日
    00
  • jQuery实现字体颜色渐变效果的方法

    关于“jQuery实现字体颜色渐变效果的方法”的完整攻略,我可以这么说: 一、前言 在网页设计中,颜色渐变(Color Gradient)是一种流行的设计元素,可以使网页更加动态和吸引人。而使用jQuery来实现颜色渐变效果,则可以更加灵活和易用。 二、jQuery实现字体颜色渐变效果的方法 实现字体颜色渐变效果的方法,主要可以通过jQuery的animat…

    css 2023年6月11日
    00
  • 详解CSS3:overflow属性

    详解CSS3:overflow属性 overflow 属性用于控制一个容器中的内容溢出时的显示方式。 值 overflow 属性有以下几个可能的值: visible:默认值,内容可以溢出容器。 hidden:内容溢出容器时隐藏溢出部分,无法滚动查看。 scroll:内容溢出容器时显示滚动条,并且可以通过滚动条查看溢出的内容。 auto:内容溢出容器时,根据需…

    css 2023年6月10日
    00
  • html的基本使用(HTML标签解释)

    下面是关于“html的基本使用(HTML标签解释)”的攻略: HTML的基本使用(HTML标签解释) HTML,全称为“Hyper Text Markup Language”,即超文本标记语言,是Web页面的基础语言,一个页面的总体结构、布局以及内容都是由HTML语言完成的。在HTML中,通过标签来对页面进行组织和定义。 HTML基本基础结构 HTML页面基…

    css 2023年6月9日
    00
  • 第一章之初识Bootstrap

    第一章:初识Bootstrap Bootstrap是一个免费开源的前端框架,可帮助开发者构建漂亮、响应式的网站、应用程序和移动设备应用程序。下面将详细介绍Bootstrap的基础知识。 1.1 下载Bootstrap Bootstrap可以从官方网站 https://getbootstrap.com/下载。我们可以选择下载编译好的CSS和JavaScript…

    css 2023年6月11日
    00
  • ThinkPHP Mobile使用方法简明教程

    ThinkPHP Mobile使用方法简明教程 什么是ThinkPHP Mobile ThinkPHP Mobile是ThinkPHP团队开发的一款基于移动Web开发框架,支持响应式设计,适配不同屏幕尺寸的手机、平板和PC端设备,同时支持多种常用的JS框架,如jQuery、MUI等。 如何安装ThinkPHP Mobile 安装ThinkPHP Mobile…

    css 2023年6月10日
    00
  • Vue中设置背景图片和透明度的简单方法

    下面是详细的攻略: Vue中设置背景图片和透明度的简单方法 设置背景图片 在Vue中设置背景图片可以使用CSS。有几种方法可以实现背景图片的设置,以下是其中的两种方法: 1. 设置全局样式 你可以在全局样式表中定义一个 body 选择器,然后在其中编写 background-image 属性。 /* 在全局样式表中定义背景图片 */ body { backg…

    css 2023年6月9日
    00
  • 基于Arcgis for javascript实现百度地图ABCD marker的效果

    接下来我会为您详细讲解基于ArcGIS for JavaScript实现百度地图ABCD Marker的效果的攻略,并提供两个示例说明。 1. 确定需求 首先需要明确我们的需求。本文中我们的目标是在ArcGIS for JavaScript中实现和百度地图类似的ABCD Marker效果。所谓ABCD Marker,是指可以根据指定的数值和颜色,显示不同的M…

    css 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部