当前位置:首页 > VUE

vue实现selected组件

2026-02-18 14:29:02VUE

实现基础下拉选择组件

使用Vue的v-modelv-for指令创建基础下拉选择框。通过select元素绑定值,option元素动态渲染选项列表。

<template>
  <select v-model="selectedValue">
    <option 
      v-for="option in options" 
      :key="option.value" 
      :value="option.value"
    >
      {{ option.label }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedValue: '',
      options: [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ]
    }
  }
}
</script>

添加自定义样式和功能

使用div模拟下拉框实现更灵活的样式控制。通过v-show控制下拉列表显示状态,添加点击事件处理展开/收起逻辑。

<template>
  <div class="custom-select">
    <div 
      class="selected-option"
      @click="toggleDropdown"
    >
      {{ selectedLabel }}
      <span class="arrow">▼</span>
    </div>
    <div 
      class="options-list"
      v-show="isOpen"
    >
      <div
        v-for="option in options"
        :key="option.value"
        @click="selectOption(option)"
      >
        {{ option.label }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isOpen: false,
      selectedValue: '',
      selectedLabel: '请选择',
      options: [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ]
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen
    },
    selectOption(option) {
      this.selectedValue = option.value
      this.selectedLabel = option.label
      this.isOpen = false
    }
  }
}
</script>

<style>
.custom-select {
  position: relative;
  width: 200px;
}
.selected-option {
  padding: 8px;
  border: 1px solid #ccc;
  cursor: pointer;
}
.options-list {
  position: absolute;
  width: 100%;
  border: 1px solid #ccc;
  max-height: 200px;
  overflow-y: auto;
}
.options-list div {
  padding: 8px;
  cursor: pointer;
}
.options-list div:hover {
  background-color: #f0f0f0;
}
.arrow {
  float: right;
}
</style>

封装为可复用组件

将选择器封装为独立组件,通过props接收选项列表,通过emit事件传递选择结果。

<!-- Select.vue -->
<template>
  <div class="custom-select">
    <div class="selected-option" @click="toggleDropdown">
      {{ selectedLabel || placeholder }}
      <span class="arrow">▼</span>
    </div>
    <div class="options-list" v-show="isOpen">
      <div
        v-for="option in options"
        :key="option.value"
        @click="selectOption(option)"
      >
        {{ option.label }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Array,
      required: true
    },
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      isOpen: false,
      selectedValue: this.value,
      selectedLabel: ''
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen
    },
    selectOption(option) {
      this.selectedValue = option.value
      this.selectedLabel = option.label
      this.$emit('input', option.value)
      this.$emit('change', option)
      this.isOpen = false
    }
  },
  watch: {
    value(newVal) {
      this.selectedValue = newVal
      const selectedOption = this.options.find(opt => opt.value === newVal)
      this.selectedLabel = selectedOption ? selectedOption.label : ''
    }
  }
}
</style>

在父组件中使用

通过v-model绑定选择器值,监听change事件获取完整选项对象。

<template>
  <div>
    <Select 
      v-model="selectedValue"
      :options="options"
      placeholder="选择城市"
      @change="handleChange"
    />
    <p>当前选择的值: {{ selectedValue }}</p>
  </div>
</template>

<script>
import Select from './Select.vue'

export default {
  components: { Select },
  data() {
    return {
      selectedValue: '',
      options: [
        { value: 'bj', label: '北京' },
        { value: 'sh', label: '上海' }
      ]
    }
  },
  methods: {
    handleChange(option) {
      console.log('选择的完整对象:', option)
    }
  }
}
</script>

添加搜索过滤功能

扩展选择器组件,增加搜索框过滤选项功能。

vue实现selected组件

<template>
  <div class="custom-select">
    <div class="selected-option" @click="toggleDropdown">
      {{ selectedLabel || placeholder }}
      <span class="arrow">▼</span>
    </div>
    <div class="options-list" v-show="isOpen">
      <input 
        v-model="searchText"
        class="search-input"
        placeholder="搜索..."
        @click.stop
      />
      <div
        v-for="option in filteredOptions"
        :key="option.value"
        @click="selectOption(option)"
      >
        {{ option.label }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: Array,
    value: [String, Number],
    placeholder: String
  },
  data() {
    return {
      isOpen: false,
      selectedValue: this.value,
      selectedLabel: '',
      searchText: ''
    }
  },
  computed: {
    filteredOptions() {
      return this.options.filter(option => 
        option.label.toLowerCase().includes(this.searchText.toLowerCase())
      )
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen
      if (this.isOpen) {
        this.searchText = ''
      }
    },
    selectOption(option) {
      this.selectedValue = option.value
      this.selectedLabel = option.label
      this.$emit('input', option.value)
      this.$emit('change', option)
      this.isOpen = false
    }
  },
  watch: {
    value(newVal) {
      this.selectedValue = newVal
      const selectedOption = this.options.find(opt => opt.value === newVal)
      this.selectedLabel = selectedOption ? selectedOption.label : ''
    }
  }
}
</script>

<style>
.search-input {
  width: 100%;
  padding: 8px;
  box-sizing: border-box;
}
</style>

标签: 组件vue
分享给朋友:

相关文章

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现抽奖轮盘

vue实现抽奖轮盘

实现抽奖轮盘的基本思路 使用Vue实现抽奖轮盘的核心在于动态旋转动画和结果判定。通常需要结合CSS动画或JavaScript动画库(如GSAP)控制轮盘的旋转角度,并通过随机数或后端接口确定最终奖项。…

vue实现rem适配

vue实现rem适配

Vue 项目中实现 REM 适配 REM(Root EM)是一种基于根元素(html)字体大小的相对单位,常用于移动端适配。以下是在 Vue 项目中实现 REM 适配的详细方法。 安装 postcs…