vue学习之mintui picker选择器实现省市二级联动


Mint UI 使用文档:https://mint-ui.github.io/docs/#/zh-cn2

Popup弹出框介绍:https://mint-ui.github.io/docs/#/zh-cn2/popup

Picker选择器介绍:https://mint-ui.github.io/docs/#/zh-cn2/picker

Datetime picker日期选择器介绍:https://mint-ui.github.io/docs/#/zh-cn2/datetime-picker

代码如下:

[html]  view plain  copy
  1.   
  2. <template>  
  3.   <div>  
  4.       
  5.     <com-header :title="headerData.title" :toLink="headerData.toLink">com-header>  
  6.       
  7.       
  8.     <div class="ybb-yuyue">  
  9.       <div class="yy-item-box mine-me">  
  10.         <a class="mint-cell mint-field">  
  11.           <div class="mint-cell-left">div>  
  12.           <div class="mint-cell-wrapper">  
  13.             <div class="mint-cell-title">  
  14.               <span class="mint-cell-text">头像span>  
  15.             div>  
  16.             <div class="mint-cell-value">  
  17.               <div class="mint-cell-value">div>  
  18.               <img v-bind:src="data.photo" :onerror="headImg" class="img-box5">  
  19.             div>  
  20.           div>  
  21.         a>  
  22.       div>  
  23.       <div class="yy-item-box mine-me">  
  24.         <a class="mint-cell mint-field">  
  25.           <div class="mint-cell-left">div>  
  26.           <div class="mint-cell-wrapper">  
  27.             <div class="mint-cell-title">  
  28.               <span class="mint-cell-text">姓名span>  
  29.             div>  
  30.             <div class="mint-cell-value">  
  31.               <div class="mint-cell-value">  
  32.                 <input placeholder="请输入姓名" type="text" class="mint-field-core text-right" v-model="data.userName">  
  33.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  34.               div>  
  35.             div>  
  36.           div>  
  37.         a>  
  38.         <a class="mint-cell mint-field">  
  39.           <div class="mint-cell-left">div>  
  40.           <div class="mint-cell-wrapper">  
  41.             <div class="mint-cell-title">  
  42.               <span class="mint-cell-text">性别span>  
  43.             div>  
  44.             <div class="mint-cell-value">  
  45.               <div class="mint-cell-value is-link" @click="sexVisible = true">  
  46.                 <input placeholder="请选择性别" type="text" class="mint-field-core text-right" readonly="readonly" v-model="data.sexText">  
  47.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  48.               div>  
  49.             div>  
  50.             <i class="mint-cell-allow-right">i>  
  51.             <mt-actionsheet :actions="sexs" v-model="sexVisible" cancel-text="取消">mt-actionsheet>  
  52.           div>  
  53.         a>  
  54.         <a class="mint-cell mint-field">  
  55.           <div class="mint-cell-left">div>  
  56.           <div class="mint-cell-wrapper">  
  57.             <div class="mint-cell-title">  
  58.               <span class="mint-cell-text">出生日期span>  
  59.             div>  
  60.             <div class="mint-cell-value">  
  61.               <div class="mint-cell-value is-link" @click="open('datePicker')">  
  62.                 <input placeholder="请选择日期" type="text" class="mint-field-core text-right" readonly="readonly" v-model="data.birthday">  
  63.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  64.               div>  
  65.             div>  
  66.             <i class="mint-cell-allow-right">i>  
  67.             <mt-datetime-picker  
  68.               ref="datePicker"  
  69.               type="date"  
  70.               :startDate="startDate"  
  71.               :endDate="endDate"  
  72.               v-model="dateValue"  
  73.               @confirm="handleChange">  
  74.             mt-datetime-picker>  
  75.           div>  
  76.         a>  
  77.       div>  
  78.       <div class="yy-item-box mine-me">  
  79.         <a class="mint-cell mint-field">  
  80.           <div class="mint-cell-left">div>  
  81.           <div class="mint-cell-wrapper">  
  82.             <div class="mint-cell-title">  
  83.               <span class="mint-cell-text">电话号码span>  
  84.             div>  
  85.             <div class="mint-cell-value">  
  86.               <div class="mint-cell-value">  
  87.                 <input placeholder="请输入电话号码" type="text" readonly="readonly" class="mint-field-core text-right gray" v-model="data.mobile">  
  88.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  89.               div>  
  90.             div>  
  91.           div>  
  92.         a>  
  93.         <a class="mint-cell mint-field">  
  94.           <div class="mint-cell-left">div>  
  95.           <div class="mint-cell-wrapper">  
  96.             <div class="mint-cell-title">  
  97.               <span class="mint-cell-text">所在地区span>  
  98.             div>  
  99.             <div class="mint-cell-value">  
  100.               <div class="mint-cell-value is-link" @click="choiceArea">  
  101.                 <input placeholder="请选择省市" type="text" class="mint-field-core text-right" readonly="readonly" v-model="data.areaText">  
  102.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  103.               div>  
  104.             div>  
  105.             <i class="mint-cell-allow-right">i>  
  106.             <mt-popup v-model="popupVisible" position="bottom" class="mint-popup-4">  
  107.               <div class="picker-toolbar">  
  108.                 <span class="mint-datetime-action mint-datetime-cancel" @click="cancleaddress">取消span>  
  109.                 <span class="mint-datetime-action mint-datetime-confirm" @click="selectaddress">确定span>  
  110.               div>  
  111.               <mt-picker :slots="citySlots" @change="onCityChange" :visible-item-count="3">mt-picker>  
  112.             mt-popup>  
  113.           div>  
  114.         a>  
  115.         <a class="mint-cell mint-field">  
  116.           <div class="mint-cell-left">div>  
  117.           <div class="mint-cell-wrapper">  
  118.             <div class="mint-cell-title">  
  119.               <span class="mint-cell-text">详细地址span>  
  120.             div>  
  121.             <div class="mint-cell-value">  
  122.               <div class="mint-cell-value">  
  123.                 <input placeholder="街道、楼牌号等" type="text" class="mint-field-core text-right" v-model="data.address">  
  124.                 <div class="mint-field-clear" style="display: none;"><i class="mintui mintui-field-error">i>div>  
  125.               div>  
  126.             div>  
  127.           div>  
  128.         a>  
  129.       div>  
  130.     div>  
  131.     <div class="yuyue-submit">  
  132.       <button class="mint-button mint-button--default mint-button--large ybb-btn" @click="infoSave"><label class="mint-button-text title-1">保存label>button>  
  133.     div>  
  134.       
  135.   div>  
  136. template>  
  137. <script>  
  138. import {Toast} from 'mint-ui'  
  139. import validators from '../utils/validators'  
  140. import comHeader from 'components/comHeader'  
  141. import mineInfoService from 'SERVICES/mineInfoService'  
  142.   
  143. export default {  
  144.   components: {  
  145.     comHeader  
  146.   },  
  147.   data: () => ({  
  148.     headImg: 'this.src="' + require('../assets/img.png') + '"',  
  149.     headerData: {  
  150.       title: '我的资料',  
  151.       toLink: '/Mine'  
  152.     },  
  153.     popupVisible: false,  
  154.     sexVisible: false,  
  155.     areaPicker: '',  
  156.     areaList: [],  
  157.     data: {  
  158.       photo: '',  
  159.       userName: '',  
  160.       sex: '',  
  161.       sexText: '',  
  162.       mobile: '',  
  163.       birthday: '',  
  164.       privinceName: '',  
  165.       provinceId: '',  
  166.       cityName: '',  
  167.       cityId: '',  
  168.       address: '',  
  169.       areaText: ''  
  170.     },  
  171.     sexs: [],  
  172.     citySlots: [  
  173.       {  
  174.         flex: 1,  
  175.         values: Object.keys(address),  
  176.         className: 'slot1',  
  177.         textAlign: 'center'  
  178.       }, {  
  179.         divider: true,  
  180.         content: '-',  
  181.         className: 'slot2'  
  182.       }, {  
  183.         flex: 1,  
  184.         values: Object.values(address)[0],  
  185.         className: 'slot3',  
  186.         textAlign: 'center'  
  187.       }  
  188.     ],  
  189.     addressProvince: '',  
  190.     addressProvinceId: '',  
  191.     addressCity: '',  
  192.     addressCityId: '',  
  193.     dateValue: new Date(),  
  194.     startDate: new Date('1900-01-01'),  
  195.     endDate: new Date()  
  196.   }),  
  197.   created () {  
  198.     this.loadMineInfo()  
  199.     this.loadAreaList()  
  200.   },  
  201.   mounted () {  
  202.     this.sexs = [{  
  203.       name: '男',  
  204.       method: this.selectMan  
  205.     }, {  
  206.       name: '女',  
  207.       method: this.selectWoman  
  208.     }]  
  209.   },  
  210.   methods: {  
  211.     loadAreaList: function () {  
  212.       mineInfoService.loadAreaList().then(res => {  
  213.         if (res.t) {  
  214.           this.areaList = res.t  
  215.           address = this.areaList.areaList[0]  
  216.           provinceCodeList = this.areaList.provinceCodeList[0]  
  217.           cityCodeList = this.areaList.cityCodeList[0]  
  218.           this.citySlots[0].values = Object.keys(address)  
  219.           this.citySlots[2].values = Object.values(address)[0]  
  220.         } else {  
  221.           Toast('地区数据异常')  
  222.         }  
  223.       })  
  224.     },  
  225.     choiceArea: function () {  
  226.       this.popupVisible = true  
  227.       // 设置默认选中  
  228.       if (this.data.privinceName !== '' && this.data.cityName !== '') {  
  229.         this.areaPicker.setSlotValue(0, this.data.privinceName)  
  230.         this.areaPicker.setSlotValue(1, this.data.cityName)  
  231.         console.log(this.data.privinceName + '-' + this.data.cityName)  
  232.       }  
  233.     },  
  234.     cancleaddress: function () {  
  235.       this.popupVisible = false  
  236.       this.areaPicker.setSlotValue(0, this.data.privinceName)  
  237.       this.areaPicker.setSlotValue(1, this.data.cityName)  
  238.     },  
  239.     selectaddress: function () {  
  240.       this.popupVisible = false  
  241.       this.data.privinceName = this.addressProvince  
  242.       this.data.cityName = this.addressCity  
  243.       this.data.provinceId = this.addressProvinceId  
  244.       this.data.cityId = this.addressCityId  
  245.       this.data.areaText = this.data.privinceName + this.data.cityName  
  246.     },  
  247.     infoSave: function () {  
  248.       if (this.data.userName.trim() === '') {  
  249.         Toast('请输入姓名')  
  250.       } else if (this.data.userName.trim().length > 12) {  
  251.         Toast('姓名不能超过12个字符')  
  252.       } else if (this.data.sex.toString().trim() === '') {  
  253.         Toast('请选择性别')  
  254.       } else if (this.data.birthday.trim() === '') {  
  255.         Toast('请选择出生日期')  
  256.       } else if (this.data.mobile.trim() === '') {  
  257.         Toast('请输入电话号码')  
  258.       } else if (!validators.mobile(this.data.mobile.trim())) {  
  259.         Toast('电话号码不正确')  
  260.       } else if (this.data.areaText.toString().trim() === '') {  
  261.         Toast('请选择所在地区')  
  262.       } else if (this.data.address.trim() === '') {  
  263.         Toast('请输入详细地址')  
  264.       } else if (this.data.address.trim().length > 50) {  
  265.         Toast('详细地址不能超过50个字符')  
  266.       } else {  
  267.         this.doAdd()  
  268.       }  
  269.     },  
  270.     doAdd: function () {  
  271.       mineInfoService.updateAccount(this.data).then(res => {  
  272.         Toast('修改成功')  
  273.         this.$router.push('/Mine')  
  274.       })  
  275.     },  
  276.     loadMineInfo: function () {  
  277.       mineInfoService.loadMineInfo().then(res => {  
  278.         this.data.photo = res.t.member.photo || ''  
  279.         this.data.userName = res.t.member.userName || ''  
  280.         this.data.sex = res.t.member.sex || ''  
  281.         this.data.sexText = res.t.member.sex === '1' ? '男' : (res.t.member.sex === '0' ? '女' : '')  
  282.         this.data.mobile = res.t.member.mobile || ''  
  283.         this.data.birthday = res.t.member.birthday || ''  
  284.         this.data.area = res.t.member.area || ''  
  285.         this.data.address = res.t.member.address || ''  
  286.         this.dateValue = this.data.birthday  
  287.         this.data.privinceName = res.t.member.priviceName || ''  
  288.         this.data.cityName = res.t.member.cityName || ''  
  289.         this.data.provinceId = res.t.member.provinceId || ''  
  290.         this.data.cityId = res.t.member.cityId || ''  
  291.         this.data.areaText = this.data.privinceName + this.data.cityName  
  292.       })  
  293.     },  
  294.     onCityChange: function (picker, values) {  
  295.       this.areaPicker = picker  
  296.       /* 此处不直接使用this.data.privinceName、this.data.cityName、this.data.provinceId、this.data.cityId,由于在加载地区信息之后,我又重新设置了绑定到slots属性的值,此时也会触发@change事件,所以在nCityChange方法里需要用四个变量替代上述四个变量来记录当前选中的值,然后在点击确定之后的方法里将这四个值赋给上述四个变量。如果直接在该方法里使用上述四个变量来获取当前选中的值,可能导致数据错乱 */  
  297.       picker.setSlotValues(1, address[values[0]])  
  298.       this.addressProvince = values[0]  
  299.       this.addressCity = values[1]  
  300.       this.addressProvinceId = provinceCodeList[this.addressProvince] + ''  
  301.       this.addressCityId = cityCodeList[this.addressCity] + ''  
  302.     },  
  303.     open: function (picker) {  
  304.       this.dateValue = this.data.birthday  
  305.       this.$refs[picker].open()  
  306.     },  
  307.     handleChange: function (value) {  
  308.       this.data.birthday = window.moment(value).format('YYYY-MM-DD')  
  309.     },  
  310.     selectMan: function () {  
  311.       this.data.sex = '1'  
  312.       this.data.sexText = '男'  
  313.     },  
  314.     selectWoman: function () {  
  315.       this.data.sex = '0'  
  316.       this.data.sexText = '女'  
  317.     }  
  318.   }  
  319. }  
  320.   
  321. let address = {}  
  322. let provinceCodeList = {}  
  323. let cityCodeList = {}  
  324. script>  
  325. <style scoped>  
  326. .mint-popup-4 {  
  327.     width: 100%;  
  328. }  
  329. .mint-popup-4 .picker-slot-wrapper, .page-popup .mint-popup-4 .picker-item {  
  330.     -webkit-backface-visibility: hidden;  
  331.     backface-visibility: hidden;  
  332. }  
  333. style>  
地区数据格式如下:

[html]  view plain  copy
  1. {  
  2.     "code": 200,  
  3.     "msg": "获取区域信息成功!",  
  4.     "t": {  
  5.         "areaList": [  
  6.             {  
  7.                 "上海": [  
  8.                     "上海"  
  9.                 ],  
  10.                 "北京": [  
  11.                     "北京"  
  12.                 ],  
  13.                 "广东": [  
  14.                     "广州",  
  15.                     "深圳"  
  16.                 ],  
  17.                 "江苏": [  
  18.                     "徐州",  
  19.                     "南京"  
  20.                 ],  
  21.                 "福建": [  
  22.                     "福州"  
  23.                 ]  
  24.             }  
  25.         ],  
  26.         "provinceCodeList": [  
  27.             {  
  28.                 "上海": [  
  29.                     "120001"  
  30.                 ],  
  31.                 "北京": [  
  32.                     "110001"  
  33.                 ],  
  34.                 "广东": [  
  35.                     "130001"  
  36.                 ],  
  37.                 "江苏": [  
  38.                     "130007"  
  39.                 ],  
  40.                 "福建": [  
  41.                     "0100"  
  42.                 ]  
  43.             }  
  44.         ],  
  45.         "cityCodeList": [  
  46.             {  
  47.                 "上海": [  
  48.                     "120002"  
  49.                 ],  
  50.                 "北京": [  
  51.                     "110002"  
  52.                 ],  
  53.                 "广州": [  
  54.                     "130002"  
  55.                 ],  
  56.                 "南京": [  
  57.                     "130006"  
  58.                 ],  
  59.                 "深圳": [  
  60.                     "518000"  
  61.                 ],  
  62.                 "福州": [  
  63.                     "0200"  
  64.                 ],  
  65.                 "徐州": [  
  66.                     "130009"  
  67.                 ]  
  68.             }  
  69.         ]  
  70.     }  
  71. }  

你可能感兴趣的:(H5)