1、编写自定义组件myPickerView.vue
<template> <view class="my-picker-view" v-show="value"> <uni-transition mode-class="slide-bottom" :show="value" :styles="{'width':'100%','height':'100vh','position':'fixed','bottom':'0'}"> <view class="empty-box" @click="handleCancel"></view> <view class="picker-box"> <view class="picker-top"> <view class="cancel" @click="handleCancel">取消</view> <view class="title">{{ title }}</view> <view class="submit" @click="handleSubmit">确定</view> </view> <picker-view :value="pickerValue" indicator-class="indicator" @change="handleChange" @pickstart="pickstart" @pickend="pickend" :immediate-change="true"> <picker-view-column class="picker-content"> <view class="picker-item" v-for="(item, index) in options" :key="index">{{ item[rangeKey] }}</view> </picker-view-column> </picker-view> </view> </uni-transition> </view> </template> <script> export default { name: "myPickerView", data() { return { pickerValue: [0], isScroll: false } }, props: { value: Boolean, options: Array, title: { type: String, default: "" }, rangeKey: { type: String, default: "label" } }, methods: { // 确定 handleSubmit() { if(!this.isScroll) { this.$emit('input', false); this.$emit("change", this.options[this.pickerValue[0]]); } }, // 取消 handleCancel() { this.isScroll = false; this.$emit('input', false); }, handleChange(e) { this.pickerValue = e.detail.value; }, pickstart(e) { this.isScroll = true; }, pickend(e) { this.isScroll = false; } } } </script> <style scoped lang='less'> .my-picker-view { width: 100%; height: 100%; position: fixed; z-index: 100; bottom: 0; background-color: rgba(0,0,0,0.5); .empty-box { width: 100%; height: 50%; position: absolute; top: 0; left: 0; } .picker-box { width: 100%; height: 50%; position: absolute; bottom: 0; .picker-top { height: 120rpx; width: 100%; display: flex; justify-content: space-between; align-items: center; background-color: #FFFFFF; border-radius: 40rpx 40rpx 0 0; .cancel, .submit { width: 132rpx; font-size: 28rpx; color: #040405; text-align: center; } .submit { color: #3973B5; } .title { width: calc(~"100% - 300rpx"); text-align: center; color: #040405; font-weight: bold; font-size: 36rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } } /deep/ picker-view { background-color: #FFFFFF; height: calc(~"100% - 120rpx"); .picker-item { height: 96rpx !important; line-height: 96rpx !important; text-align: center; } .indicator { height: 96rpx; } } } } </style>2、组件调用
<template> <view class="container"> <view class="required-item"> <view class="lt"> <text class="icon">*</text>园区 </view> <view class="rt"> <view class="rt-text" @click="isShowPark = true">{{ form.parkLabel || '请选择' }}</view> <uni-icons type="right"></uni-icons> </view> </view> <MyPickerView v-model="isShowPark" :options="cityOptions" range-key="name" title="籍贯" @change="handleChangePark"></MyPickerView> </view> </template> <script> import MyPickerView from './components/myPickerView.vue' export default { name: 'vehicleAppoint', data() { return { form: { parkLabel: "", parkValue: "" }, cityOptions: [ { name: '北京', value: "beijing" }, { name: '上海', value: "shanghai" }, { name: '广州', value: "guangzhou" }, { name: '深圳', value: "shenzhen" }, { name: '成都', value: "chengdu" }, { name: '武汉', value: "wuhan" }, { name: '重庆', value: "chongqing" }, { name: '贵州', value: "guizhou" }, ], isShowPark: false } }, components: { MyPickerView }, methods: { handleChangePark(data) { this.form.parkLabel = data.name; this.form.parkValue = data.value; } } } </script>
网友回复