美文网首页
vue+mintui(Picker+Popup)城市三联级

vue+mintui(Picker+Popup)城市三联级

作者: jane819 | 来源:发表于2018-12-24 14:45 被阅读0次
<template>
    <div>
        
        <div>
            <input type="text" placeholder="请选择三级地址" v-model="adress" maxlength="80" readonly="readonly" @click="showAddressPicker"/>
            <p>地址3级联动:{{myAddressProvince}} {{myAddressCity}} {{myAddresscounty}}</p>
            <!--<mt-picker :slots="slots" @change="onValuesChange"></mt-picker>-->
            <mt-popup v-model="popupVisible" position="bottom">
              <mt-picker :slots="myAddressSlots" @change="onMyAddressChange"></mt-picker>
              
            </mt-popup>
        </div>
        <tabbar></tabbar>
    </div>
        
</template>


<script>
    import Vue from 'vue'
  import { Picker } from 'mint-ui';
  import myaddress from '../assets/data.json'     //引入省市区数据
  import Tabbar from '@/components/tabbar'
  import { Popup } from 'mint-ui';
  Vue.component(Popup.name, Popup);
  export default {
    name: '',
    components: {
      'mt-picker': Picker
    },
    props: {},
    data () {
      return {
        adress:'',
        popupVisible:false,
       myAddressSlots: [
          {
            flex: 1,
            defaultIndex: 1,    
            values: Object.keys(myaddress),  //省份数组
            className: 'slot1',
            textAlign: 'center'
          }, {
            divider: true,
            content: '-',
            className: 'slot2'
          }, {
            flex: 1,
            values: [],
            className: 'slot3',
            textAlign: 'center'
          },
          {
            divider: true,
            content: '-',
            className: 'slot4'
          },
          {
            flex: 1,
            values: [],
            className: 'slot5',
            textAlign: 'center'
          }
        ],
        myAddressProvince:'省',
        myAddressCity:'市',
        myAddresscounty:'区/县',
      }
    },
    created() {
 
 
    },
    methods: {
        //打开地址选择器
      showAddressPicker(){
        this.popupVisible = true;
      },
     onMyAddressChange(picker, values) {
       if(myaddress[values[0]]){  //这个判断类似于v-if的效果(可以不加,但是vue会报错,很不爽)
          picker.setSlotValues(1,Object.keys(myaddress[values[0]])); // Object.keys()会返回一个数组,当前省的数组
          picker.setSlotValues(2,myaddress[values[0]][values[1]]); // 区/县数据就是一个数组
          this.myAddressProvince = values[0];
          this.myAddressCity = values[1];
          this.myAddresscounty = values[2];
          this.adress=values[0]+'-'+values[1]+'-'+values[2];
        }
      },
    },
    mounted(){
      this.$nextTick(() => { //vue里面全部加载好了再执行的函数  (类似于setTimeout)
        this.myAddressSlots[0].defaultIndex = 0    
        // 这里的值需要和 data里面 defaultIndex 的值不一样才能够初始化
        //因为我没有看过源码(我猜测是因为数据没有改变,不会触发更新)
      });
    },
     components:{
        tabbar:Tabbar
      }
  }
</script>

<style scoped="scoped">
    .mint-popup-bottom{
        
        width: 100%!important;
        z-index: 5555;
    }
</style>

相关文章

网友评论

      本文标题:vue+mintui(Picker+Popup)城市三联级

      本文链接:https://www.haomeiwen.com/subject/dbkzkqtx.html