123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <template>
- <div class="mpvue-picker">
- <div :class="{'pickerMask':showPicker}" @click="maskClick" catchtouchmove="true"></div>
- <div class="mpvue-picker-content " :class="{'mpvue-picker-view-show':showPicker}">
- <div class="mpvue-picker__hd" catchtouchmove="true">
- <div class="mpvue-picker__action" @click="pickerCancel">取消</div>
- <div class="mpvue-picker__action" :style="{color:themeColor}" @click="pickerConfirm">确定</div>
- </div>
- <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChange">
- <block>
- <picker-view-column>
- <view class="picker-item" v-for="(item,index) in years" :key="index">{{item.label}}年</view>
- </picker-view-column>
- <picker-view-column>
- <view class="picker-item" v-for="(item,index) in months" :key="index">{{item.label}}月</view>
- </picker-view-column>
- <picker-view-column>
- <view class="picker-item" v-for="(item,index) in days" :key="index">{{item.label}}日</view>
- </picker-view-column>
- </block>
- </picker-view>
- </div>
- </div>
- </template>
- <script>
- //import provinceData from './city-data/province.js';
- //import cityData from './city-data/city.js';
- //import areaData from './city-data/area.js';
- var addMonth=function(date, months) {
- let y = date.getFullYear();
- let m = date.getMonth();
- var d = date.getDate();
- // console.log('add month y' + y + ' m:' + m + ' d: ' + d + ' month: ' + months);
- y += Math.floor((m + 1 + months) / 12); //计算年
- m = Math.floor((m + 1 + months) % 12) - 1; //计算月
- let _date = new Date(y,m,d);
- // console.log('result y:' + y + ' m: ' + m + '_date:' + _date);
- return _date;
- };
- var addDay=function(date, days) {
- var _date = new Date(date.getTime() + 24*60*60*1000*days)
- return _date;
- };
- export default {
- data() {
- const date = new Date()
- const years = []
- const year = date.getFullYear()
- const months = []
- const month = date.getMonth() + 1
- const days = []
- const day = date.getDate()
-
- let index = 0;
- for (let i = 1990; i <= date.getFullYear(); i++) {
- let data = { value: index++,label:i};
- years.push(data)
- }
- index = 0;
- for (let i = 1; i <= 12; i++) {
- let data = { value: index++,label:i};
- months.push(data)
- }
- index = 0;
- for (let i = 1; i <= 31; i++) {
- let data = { value: index++,label:i};
- days.push(data)
- }
- return {
- pickerValue: [0, 0, 0],
- years,
- year,
- months,
- month,
- days,
- day,
- /* 是否显示控件 */
- showPicker: false,
- };
- },
- created() {
- // console.log('mpvueCityPicker created')
-
- this.init()
- },
- props: {
- /* 默认值 */
- pickerValueDefault: {
- type: Array,
- default(){
- return [0, 0 , 0]
- }
- },
- /* 主题色 */
- themeColor: String
- },
- watch:{
- pickerValueDefault(){
- this.init();
- }
- },
- methods: {
- init() {
- // console.log('mpvueCityPicker init')
- if(this.pickerValueDefault.length == 0)
- this.pickerValueDefault = [0,0, 0];
-
- //console.log('ageDownLimit'+JSON.stringify(this.ageDownLimitDataList))
- //console.log('ageUpLimit'+JSON.stringify(this.ageUpLimitDataList))
- //this.handPickValueDefault(); // 对 pickerValueDefault 做兼容处理
-
-
- this.pickerValue = this.pickerValueDefault;
- },
- show() {
- setTimeout(() => {
- this.showPicker = true;
- }, 0);
- },
- maskClick() {
- this.pickerCancel();
- },
- pickerCancel() {
- this.showPicker = false;
- this._$emit('onCancel');
- },
- pickerConfirm(e) {
- this.showPicker = false;
- this._$emit('onConfirm');
- },
- showPickerView() {
- this.showPicker = true;
- },
- handPickValueDefault() {
-
- //console.log('mpvueCityPicker handPickValueDefault')
- console.log('默认树'+JSON.stringify(this.pickerValueDefault))
- //console.log('省'+JSON.stringify(this.pickerValueDefault))
-
-
- if (this.pickerValueDefault[0] > this.years.length - 1) {
- this.pickerValueDefault[0] = this.years.length - 1;
-
- }
- if (this.pickerValueDefault[1] > this.months.length - 1) {
- this.pickerValueDefault[1] = this.months.length - 1;
-
- }
- if (this.pickerValueDefault[2] > this.days.length - 1) {
- this.pickerValueDefault[2] = this.days.length - 1;
- }
- },
- pickerChange(e) {
- let changePickerValue = e.mp.detail.value;
- if (this.pickerValue[0] !== changePickerValue[0]) {
- // 第一级发生滚动
-
- //this.cityDataList = cityData[changePickerValue[0]];
- //this.areaDataList = areaData[changePickerValue[0]][0];
- changePickerValue[1] = 0;
- console.log('第一级滚动')
- console.log('date'+JSON.stringify(this.years[changePickerValue[0]]) +'-'+JSON.stringify(this.months[changePickerValue[1]]))
- }
- else if (this.pickerValue[1] !== changePickerValue[1]) {
- // 第二级滚动
- console.log('第二级滚动')
- let year = this.years[changePickerValue[0]].label;
- let month = this.months[changePickerValue[1]].label;
- console.log('date'+JSON.stringify(this.years[changePickerValue[0]]) +'-'+JSON.stringify(this.months[changePickerValue[1]]))
- let date = new Date(year,month-1,1);
- let nextMonth = addMonth(date,1);
- let currentMonth = addDay(nextMonth,-1);
-
- console.log('下月日期'+JSON.stringify(nextMonth))
- console.log('当前日期'+JSON.stringify(currentMonth))
- const day = currentMonth.getDate()
- console.log('day'+JSON.stringify(day))
- let index = 0;
- this.days =[];
- for (let i = 1; i <= day; i++) {
- let data = { value: index++,label:i};
- this.days.push(data)
- }
- changePickerValue[2] = 0;
- }
- this.pickerValue = changePickerValue;
- this._$emit('onChange');
- },
- _$emit(emitName) {
- let pickObj = {
- label: this._getLabel(),
- value: this.pickerValue
- };
- this.$emit(emitName, pickObj);
- },
- _getLabel() {
- let pcikerLabel =
- this.years[this.pickerValue[0]].label +
- '-' +
- this.months[this.pickerValue[1]].label+
- '-'+
- this.days[this.pickerValue[2]].label;
- return pcikerLabel;
- }
- }
- };
- </script>
- <style>
- .pickerMask {
- position: fixed;
- z-index: 1000;
- top: 0;
- right: 0;
- left: 0;
- bottom: 0;
- background: rgba(0, 0, 0, 0.6);
- }
- .mpvue-picker-content {
- position: fixed;
- bottom: 0;
- left: 0;
- width: 100%;
- transition: all 0.3s ease;
- transform: translateY(100%);
- z-index: 3000;
- }
- .mpvue-picker-view-show {
- transform: translateY(0);
- }
- .mpvue-picker__hd {
- display: flex;
- padding: 9px 15px;
- background-color: #fff;
- position: relative;
- text-align: center;
- font-size: 17px;
- }
- .mpvue-picker__hd:after {
- content: ' ';
- position: absolute;
- left: 0;
- bottom: 0;
- right: 0;
- height: 1px;
- border-bottom: 1px solid #e5e5e5;
- color: #e5e5e5;
- transform-origin: 0 100%;
- transform: scaleY(0.5);
- }
- .mpvue-picker__action {
- display: block;
- flex: 1;
- color: #1aad19;
- }
- .mpvue-picker__action:first-child {
- text-align: left;
- color: #888;
- }
- .mpvue-picker__action:last-child {
- text-align: right;
- }
- .picker-item {
- text-align: center;
- line-height: 40px;
- text-overflow: ellipsis;
- white-space: nowrap;
- font-size: 16px;
- }
- .mpvue-picker-view {
- position: relative;
- bottom: 0;
- left: 0;
- width: 100%;
- height: 238px;
- background-color: rgba(255, 255, 255, 1);
- }
- </style>
|