该组件已更新,查看具体请移步个人主页(2023/11/23)
1.最近在uniapp上做了一款类似elementui的多功能选择器的组件,可下拉可搜索可情况,支持异步请求渲染下拉列表
废话不多说,先看视频!!!!!
(1)因为组件基于uView二次封装,所以请安装uView组件,安装步骤请看uView官方文档
说明:@select方法可以在父组件拿到当前选择项的对象数据
<template>
<view class="uni-combox" :class="border ? '' : 'uni-combox__no-border'" :style="{position:selectPosition}">
<view class="uni-combox__input-box">
<view v-if='!isShowInput' @click="isShowInput = true" class="wraper-inpu-text" >
<text v-if="inputValCopy" class="wrper-text-vlaue">{{inputValCopy}}</text>
<text v-else class="wrper-text-placeholder" :style='placeholderStyle'>{{inputValPlaceholder}}</text>
</view>
<u-input v-else :placeholderStyle='placeholderStyle' class="uni-combox__input" focus type="text"
:placeholder=" inputValPlaceholder" ref="input" placeholder-class="uni-combox__input-plac"
v-model="inputVal" @input="onChange" @focus="onFocus" @blur="onBlur" />
</view>
<view class="uni-combox__selector" v-show="showSelector && dataList.length !== 0">
<view class="uni-popper__arrow"></view>
<u-list @scrolltolower="scrolltolower">
<u-list-item v-for="(item, index) in dataList" :key="index">
<u-cell :title="item[valueKey]" @click="onSelectorClick(index)"></u-cell>
</u-list-item>
</u-list>
<!-- <view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0 && isLoading == false">
<text>{{emptyTips}}</text>
</view> -->
</view>
</view>
</template>
<script>
export default {
name: 'uniCombox',
emits: ['input'],
props: {
placeholderStyle: { //输入框placeholder状态的样式
type: String,
default: 'font-size:15px;color:#C0C4CC'
},
selectPosition: { //是否在组件父元素添加定位 默认relative
type: String,
default: 'relative'
},
valueKey: { //父组件data数据需要在列表中渲染的key值
type: String,
default: 'companyName'
},
border: {
type: Boolean, //默认显示下边框
default: true
},
label: {
type: String, //选择器label 不建议使用
default: ''
},
labelWidth: { //选择器label宽度 不建议使用
type: String,
default: 'auto'
},
placeholder: { //选择器placeholder文本
type: String,
default: ''
},
candidates: { //选择器下拉框数据 格式为[{}]
type: Array,
default () {
return []
}
},
emptyTips: { //选择器下拉框数据为空文本 ---已注释掉 需要的可以手动打开
type: String,
default: '无匹配项'
},
value: {
type: [String, Number],
default: ''
},
myMessage: { //父组件的值
type: [String, Number],
default: ''
},
},
data() {
return {
dataList:[],
showSelector: false,
placeholderCopy: '',
inputVal: '',
inputValCopy:'',
isShowInput:false,
inputValPlaceholder:"",
input:""
}
},
computed: {
labelStyle() {
if (this.labelWidth === 'auto') {
return ""
}
return `width: ${this.labelWidth}`
},
filterCandidates() {
return this.candidates
},
filterCandidatesLength() {
return this.filterCandidates.length
}
},
watch: {
myMessage: {
handler(newVal) {
if (newVal) {
this.inputValCopy = newVal
this.inputValPlaceholder = newVal
}else{
this.inputValCopy = newVal
this.inputValPlaceholder = this.placeholder
}
},
immediate: true,
deep: true
},
candidates:{
handler($value){
this.dataList = $value
},
deep:true,
immediate:true
}
},
methods: {
toggleSelector() {
this.showSelector = !this.showSelector
},
onFocus() {
if(this.inputValCopy){
this.placeholderCopy = this.inputValCopy
}else{
this.placeholderCopy = this.placeholder
}
this.showSelector = true
},
onBlur() {
setTimeout(() => {
this.showSelector = false
this.isShowInput = false
this.inputVal = ''
}, 153)
},
onSelectorClick(index) {
this.showSelector = false
this.isShowInput = false
this.$emit('update:myMessage', this.dataList[index][this.valueKey])
this.$emit('select', this.dataList[index])
},
as() {
this.$emit('input', this.inputVal)
},
scrolltolower() {
this.$emit('srolltolower')
},
onChange(e) {
// this.input = e
this.dataList = []
uni.$u.debounce(this.as, 200)
}
}
}
</script>
<style lang="scss" scoped>
.wraper-inpu-text{
display: flex;
align-items: center;
// justify-content: center;
height: 44rpx;
width: 100%;
flex: 1;
& text{
line-height: 26px;
}
}
.wrper-text-vlaue{
font-size: 15px;
}
.uni-combox {
font-size: 14px;
// border: 1px solid #DCDFE6;
border-radius: 4px;
// padding: 6px 10px;
position: relative;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
// height: 40px;
flex-direction: row;
align-items: center;
width: 100% !important;
// border-bottom: solid 1px #DDDDDD;
}
.uni-combox__label {
font-size: 16px;
line-height: 22px;
padding-right: 10px;
color: #999999;
}
.uni-combox__input-box {
position: relative;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex: 1;
flex-direction: row;
align-items: center;
}
.uni-combox__input {
flex: 1;
font-size: 14px;
height: 22px;
line-height: 22px;
}
.uni-combox__input-plac {
font-size: 28rpx;
text-align: left;
color: rgb(180, 180, 180);
}
.uni-combox__selector {
/* #ifndef APP-NVUE */
box-sizing: border-box;
/* #endif */
position: absolute;
top: calc(100% + 12px);
left: 0;
width: 100%;
background-color: #FFFFFF;
border: 1px solid #EBEEF5;
border-radius: 6px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
z-index: 2;
padding: 4px 0;
}
.uni-combox__selector-scroll {
/* #ifndef APP-NVUE */
max-height: 200px;
box-sizing: border-box;
/* #endif */
}
.uni-combox__selector-empty,
.uni-combox__selector-item {
/* #ifndef APP-NVUE */
display: flex;
cursor: pointer;
/* #endif */
line-height: 36px;
font-size: 14px;
text-align: center;
// border-bottom: solid 1px #DDDDDD;
padding: 0px 10px;
}
.uni-combox__selector-item:hover {
background-color: #f9f9f9;
}
.uni-combox__selector-empty:last-child,
.uni-combox__selector-item:last-child {
/* #ifndef APP-NVUE */
border-bottom: none;
/* #endif */
}
// picker 弹出层通用的指示小三角
.uni-popper__arrow,
.uni-popper__arrow::after {
position: absolute;
display: block;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 6px;
}
.uni-popper__arrow {
filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
top: -6px;
left: 10%;
margin-right: 3px;
border-top-width: 0;
border-bottom-color: #EBEEF5;
}
.uni-popper__arrow::after {
content: " ";
top: 1px;
margin-left: -6px;
border-top-width: 0;
border-bottom-color: #fff;
}
.uni-combox__no-border {
border: none;
}
::v-deep .u-list {
min-height: 0 !important;
max-height: 350rpx !important;
height: auto !important;
}
</style>
4.使用
(1)引入该组件
(2)父组件写法
原文地址:https://blog.csdn.net/qq_32058147/article/details/131092483
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_28336.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。