Vue下拉選擇框Select組件使用詳解(二)
本文實例為大家分享了Vue下拉選擇框Select組件的使用方法,供大家參考,具體內容如下
效果圖如下:

下拉組件寬度可自定義設置以下屬性:
①下拉組件寬度width屬性,默認寬度290
②placeholder屬性
③是否禁用下拉的disabled屬性
已預設下拉列表最多8條,超過時滾動顯示,具體可自定義調整,如果下拉選項過長省略號顯示,鼠標懸浮顯示全稱,由于業(yè)務需求,設置mode屬性,區(qū)別默認name和value 與 dictKey和dictVal
①創(chuàng)建組件Select.vue
<template>
? <div class="m-select-wrap f14" :style="`width: ${width}px;`">
? ? <input
? ? ? :class="['u-select-input', { 'disabled': disabled }]"
? ? ? :style="`width: ${width - 32}px;`"
? ? ? type="text"
? ? ? :disabled="disabled"
? ? ? :placeholder="placeholder"
? ? ? readonly
? ? ? :title="selectName"
? ? ? @click="openSelect"
? ? ? @blur="onBlur"
? ? ? v-model="selectName" />
? ? <div :class="['triangle-down', { 'rotate': rotate, 'disabled': disabled }]" @click="openSelect"></div>
? ? <div :class="['m-options-panel', showOptions ? 'show': 'hidden']" :style="`height: ${selectData.length * 40}px; max-height: 320px; width: ${width - 2}px;`">
? ? ? <p class="u-option" :title="mode==='region' ? item.dictVal : item.name" @mousedown="getValue(mode==='region' ? item.dictVal : item.name, mode==='region' ? item.dictKey : item.value)" v-for="(item, index) in selectData" :key="index">
? ? ? ? {{ mode==='region' ? item.dictVal : item.name }}
? ? ? </p>
? ? </div>
? </div>
</template>
<script>
export default {
? name: 'Select',
? props: {
? ? selectData: {
? ? ? type: Array,
? ? ? default: () => {
? ? ? ? return []
? ? ? }
? ? },
? ? selValue: { // 將該prop值作為selV的初始值
? ? ? default: undefined
? ? },
? ? placeholder: {
? ? ? type: String,
? ? ? default: '請選擇'
? ? },
? ? width: { // 下拉框寬度
? ? ? type: Number,
? ? ? default: 290
? ? },
? ? disabled: {
? ? ? type: Boolean,
? ? ? default: false
? ? },
? ? mode: {
? ? ? type: String,
? ? ? default: 'default'
? ? }
? },
? computed: {
? ? selectName () {
? ? ? let selName
? ? ? this.selectData.forEach(item => {
? ? ? ? if (this.mode === 'region') {
? ? ? ? ? if (item.dictKey === this.selectValue) {
? ? ? ? ? ? selName = item.dictVal
? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? if (item.value === this.selectValue) {
? ? ? ? ? ? selName = item.name
? ? ? ? ? }
? ? ? ? }
? ? ? })
? ? ? return selName
? ? },
? ? selectValue: {
? ? ? get () {
? ? ? ? return this.selV
? ? ? },
? ? ? set (newVal) {
? ? ? ? this.selV = newVal
? ? ? }
? ? }
? },
? data () {
? ? return {
? ? ? selV: this.selValue,
? ? ? rotate: false,
? ? ? showOptions: false
? ? }
? },
? methods: {
? ? openSelect () {
? ? ? this.showOptions = !this.showOptions
? ? ? this.rotate = !this.rotate
? ? },
? ? getValue (name, value) {
? ? ? this.selectValue = value
? ? ? this.$emit('getValue', name, value)
? ? },
? ? onBlur () {
? ? ? this.showOptions = false
? ? ? this.rotate = false
? ? }
? }
}
</script>
<style lang="less" scoped>
.m-select-wrap {
? display: inline-block;
? width: 290px;
? height: 40px;
? line-height: 40px;
? position: relative;
? font-weight: 400;
? color: #444444;
? .u-select-input {
? ? padding-left: 10px;
? ? width: 258px;
? ? height: 38px;
? ? border: 1px solid #d7d7d7;
? ? cursor: pointer;
? ? padding-right: 20px;
? ? overflow: hidden;
? ? text-overflow: ellipsis;
? ? white-space: nowrap;
? ? &:focus {
? ? ? border: 1px solid @mainColor;
? ? }
? }
? .triangle-down { // 下三角
? ? width: 12px;
? ? height: 7px;
? ? font-size: 0;
? ? background: url('~@/assets/images/triangle.png') no-repeat center top;
? ? position: absolute;
? ? top: 17px;
? ? right: 10px;
? ? transition: all 0.3s ease-in-out;
? ? cursor: pointer;
? }
? .rotate {
? ? transform: rotate(180deg);
? ? -webkit-transform: rotate(180deg);
? }
? .disabled {
? ? cursor: default;
? ? pointer-events: none;
? }
? .m-options-panel {
? ? position: absolute;
? ? overflow-y: auto;
? ? background: #FFFFFF;
? ? width: 288px;
? ? border: 1px solid @mainColor;
? ? top: 40px;
? ? left: 0;
? ? color: #444;
? ? .u-option {
? ? ? padding: 0 20px;
? ? ? overflow: hidden;
? ? ? text-overflow: ellipsis;
? ? ? white-space: nowrap; // 溢出顯示省略號
? ? ? cursor: pointer;
? ? }
? ? .u-option:hover {
? ? ? background: #EEEEEE;
? ? }
? }
? .show {
? ? display: block;
? }
? .hidden {
? ? display: none;
? }
}
</style>②在要使用的頁面中引用
<Select
? ? ? :selectData="provinceData"
? ? ? :selValue="address.province"
? ? ? :width="143"
? ? ? placeholder="請選擇省"
? ? ? @getValue="getProvinceCode" />
import Select from '@/components/Select'
components: {
? ? Select
},
data () {
? ? return {
? ? ? provinceData: [],
? ? ? ? address: {
? ? ? ? ? province: ''
? ? ? ? }
? ? }
}
methods: {
? getProvinceCode (name, code) {
? ? ? console.log('province:', name, code)
? ? ? this.address.province = code
? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue3+ElementPlus使用lang="ts"報Unexpected?token錯誤的解決
最近開發(fā)中遇到了些問題,跟大家分享下,這篇文章主要給大家介紹了關于vue3+ElementPlus使用lang="ts"報Unexpected?token錯誤的解決辦法,需要的朋友可以參考下2023-01-01
詳解Vue 數(shù)據(jù)更新了但頁面沒有更新的 7 種情況匯總及延伸總結
這篇文章主要介紹了詳解Vue 數(shù)據(jù)更新了但頁面沒有更新的 7 種情況匯總及延伸總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
Vue 實現(xiàn)v-for循環(huán)的時候更改 class的樣式名稱
這篇文章主要介紹了Vue 實現(xiàn)v-for循環(huán)的時候更改 class的樣式名稱,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

