Vue组件:省市区地址选择组件

<template>
<div v-show="addressSelectShow" :style="{'left': leftValue + 'px', 'top': topValue + 'px' }" class="content">
<ul class="area-select">
<li v-for="(item, index) in areaMain" :key="index" :class="{'hasSelect':index === hasSelectIndex}" @click="changeArea(index)"> {{ item }} </li>
</ul>
<ul class="address-select">
<div v-show="hasSelectIndex === 0">
<li v-for="(item, index) in province" :key="index" :class="{'select-li-provice': index === pSelectLiIndex}" @click="selectProvince(index)"> {{ item.area_name }}</li>
</div>
<div v-show="hasSelectIndex === 1">
<li v-for="(item, index) in city" :key="index" :class="{'select-li-provice': index === cSelectLiIndex}" @click="selectCity(index)"> {{ item.area_name }}</li>
</div>
<div v-show="hasSelectIndex === 2">
<li v-for="(item, index) in area" :key="index" :class="{'select-li-provice': index === aSelectLiIndex}" @click="selectArea(index)"> {{ item.area_name }}</li>
</div>
<div v-show="hasSelectIndex === 3">
<li v-for="(item, index) in street" :key="index" :class="{'select-li-provice': index === sSelectLiIndex}" @click="selectStreet(index)"> {{ item.area_name }}</li>
</div>
</ul>
</div>
</template> <script>
import { getProvince, getCity } from '@/api/address_select'
export default {
name: 'AddressSelect',
props: {
addressSelectShow: { // 控制是否显示地址选择
type: Boolean,
default: true
},
leftValue: { // 定位的离左边的距离
type: Number,
default: 600
},
topValue: { // 定位的离上边的距离
type: Number,
default: 100
}
},
data() {
return {
areaMain: ['省', '市', '区', '街道'],
hasSelectIndex: 0, // 控制显示第几个
province: [], // 循环的省份的值
city: [], // 循环的城市的值
area: [], // 循环的地区的值
street: [], // 循环的街道的值
pid: '', // 省份id
cid: '', // 城市id
aid: '', // 地区id
sid: '', // 街道id
pSelectLiIndex: '', // 选中的省的下标
cSelectLiIndex: '', // 选中的城市的下标
aSelectLiIndex: '', // 选中的地区的下标
sSelectLiIndex: '', // 选中的街道的下标
addressDetail: [], // 传给父组件的地址名称
addressId: [] // 传给父组件的地址id
}
},
watch: {
isEmpty: function(newV, oldV) {
if (newV) {
this.pSelectLiIndex = ''
this.cSelectLiIndex = ''
this.aSelectLiIndex = ''
this.sSelectLiIndex = ''
this.pid = ''
this.cid = ''
this.aid = ''
this.sid = ''
this.hasSelectIndex = 0
this.addressId = []
this.$emit('getAddressDetail', this.addressDetail, this.addressId) // 此时,父组件需通过addressId的长度是否为0来判断是否将传递过来的isEmpty设置为false ,重置选择框的值
}
}
},
mounted() { this.getAllProvince() }, methods: { changeArea(index) { // 切换模块 switch (index) { // 判断是否上级有值,如果上级没值则不能切换 case 1: if (!this.pid) { return } break case 2: if (!this.pid || !this.cid) { return } break case 3: if (!this.pid || !this.cid || !this.aid) { return } break default: break } this.hasSelectIndex = index }, getAllProvince() { // 获取省份列表 getProvince().then((res) => { this.province = res.data }).catch(() => {}) }, // 选择省份执行的事件 selectProvince(index) { this.addressDetail = [] this.addressId = [] this.pid = this.province[index].id this.pSelectLiIndex = index this.cSelectLiIndex = '' this.aSelectLiIndex = '' this.sSelectLiIndex = '' this.addressId[0] = this.province[index].id this.addressDetail[0] = this.province[index].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) this.pSearchC() }, // 省份查找城市 pSearchC() { getCity(this.pid).then((res) => { // 获取省份下城市的列表 this.city = res.data this.hasSelectIndex = 1 if (this.city.length === 1) { // 如果只有一条数据,则默认选中并执行下一次查找,其他位置同理 this.hasSelectIndex = 2 this.cSelectLiIndex = 0 this.cid = this.city[0].id this.addressId.splice(1, this.addressId.length - 1) this.addressDetail.splice(1, this.addressId.length - 1) this.addressId[1] = this.city[0].id this.addressDetail[1] = this.city[0].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) this.cSearchA() } }).catch(() => {}) }, // 选择市执行的事件 selectCity(index) { this.cid = this.city[index].id this.cSelectLiIndex = index this.aSelectLiIndex = '' this.sSelectLiIndex = '' this.addressId.splice(index, this.addressId.length - 1) this.addressId[1] = this.city[index].id this.addressDetail.splice(1, this.addressDetail.length - 1) this.addressDetail[1] = this.city[index].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) this.cSearchA() }, // 市查找区 cSearchA() { getCity(this.cid).then((res) => { // 获取城市下地区的列表 this.area = res.data this.hasSelectIndex = 2 if (this.area.length === 1) { this.hasSelectIndex = 3 this.aSelectLiIndex = 0 this.aid = this.area[0].id this.addressId.splice(2, this.addressId.length - 1) this.addressDetail.splice(2, this.addressId.length - 1) this.addressId[2] = this.area[0].id this.addressDetail[2] = this.area[0].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) this.aSearchS() } }).catch(() => {}) }, // 选择区执行的事件 selectArea(index) { this.aid = this.area[index].id this.aSelectLiIndex = index this.sSelectLiIndex = '' this.addressId.splice(2, this.addressId.length - 1) this.addressId[2] = this.area[index].id this.addressDetail.splice(2, this.addressId.length - 1) this.addressDetail[2] = this.area[index].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) this.aSearchS() }, // 区查找街道 aSearchS() { getCity(this.aid).then((res) => { // 获取地区下街道的列表 this.street = res.data const obj = { id: '123456', area_name: '暂不选择' } this.street.unshift(obj) this.hasSelectIndex = 3 if (this.street.length === 1) { this.sSelectLiIndex = 0 this.sid = this.street[0].id this.addressId.splice(3, this.addressId.length - 1) this.addressDetail.splice(3, this.addressId.length - 1) this.addressId[3] = this.street[0].id this.addressDetail[3] = this.street[0].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) } }).catch(() => {}) }, // 选择区执行的事件 selectStreet(index) { this.sid = this.street[index].id this.sSelectLiIndex = index this.addressId[3] = this.street[index].id this.addressDetail[3] = this.street[index].area_name this.$emit('getAddressDetail', this.addressDetail, this.addressId) } } } </script> <style scoped rel="stylesheet/scss" lang="scss"> .content{ position: absolute; z-index: 10; background: white; .area-select{ width: 425px; height: 30px; margin: 0; padding: 0; border: 1px solid #CCCCCC; li{ list-style: none; float: left; height: 28px; line-height: 28px; text-align: center; background: #f0f0f0; width: 25%; cursor: pointer; } .hasSelect{ background: #FFFFFF; } } .address-select{ width: 425px; margin: 0; padding: 5px 10px; overflow-y: scroll; overflow-x: scroll; height: 320px; border: 1px solid #CCCCCC; border-top: 0; li{ height: 40px; padding: 10px 5px; line-height: 20px; cursor: pointer; } .select-li-provice{ color: #1470cc; cursor: default; } li:hover{ color: #968CFF; } } } </style>

前端小白一枚,欢迎大家多多交流

vue仿淘宝地址选择组件的更多相关文章

  1. vue仿淘宝订单状态的tab切换效果

    <div class="navigation">  //这里是通过循环遍历出来的数据,你需要根据index的值来判断你现在点击的是第几个tab栏导航,同时在js中写一个 ...

  2. vue仿淘宝结账订单

    <template>  <div class="container">  <div class="checkout-title"& ...

  3. Vue实现仿淘宝商品详情属性选择的功能

    Vue实现仿淘宝商品详情属性选择的功能 先看下效果图:(同个属性内部单选,属性与属性之间可以多选) 主要实现过程: 所使用到的数据类型是(一个大数组里面嵌套了另一个数组)具体格式如下:   attrA ...

  4. jquery仿淘宝规格颜色选择效果

    jquery实现的仿淘宝规格颜色选择效果源代码如下 jquery仿淘宝规格颜色选择效果 -收缩HTML代码 运行代码 [如果运行无效果,请自行将源代码保存为html文件运行] <script t ...

  5. 仿淘宝颜色属性选择展示代码(jQuery)

    模仿淘宝商品选择颜色和尺寸的效果,即选择商品颜色和尺寸的时候,把选择的颜色和尺寸放到一个页面容器里面,不足之处,还望指教. <!DOCTYPE HTML> <html lang=&q ...

  6. 仿淘宝 vue

    最近自己闲着无聊,用vue仿照淘宝打算写个皮囊,顺便把遇到的问题顺便记录下 1.动画问题 (1)单个元素给动画 <transition name="fade">< ...

  7. Android 轻松实现仿淘宝地区选择

    介绍 最近用淘宝客户端的时候,编辑地址的时候有个地区选择的功能.看上面的效果觉得挺酷,滚动的时候,是最后一个从下面飞上来挨着前一个.就自己鼓捣一个出来玩玩. 说了效果可能不太直观,下面上两张图看看效果 ...

  8. 基于Bootstrap仿淘宝分页控件实现

    .header { cursor: pointer } p { margin: 3px 6px } th { background: lightblue; width: 20% } table { t ...

  9. Android仿淘宝头条滚动广告条

    之前我使用TextView+Handler+动画,实现了一个简单的仿淘宝广告条的滚动,https://download.csdn.net/download/qq_35605213/9660825: 无 ...

随机推荐

  1. C语言宏定义时#(井号)和##(双井号)作用

    #的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量 通过替换后在其左右各加上一个双引号. #define example(instr) prin ...

  2. 谈谈加载(Loading)的那点事

    谈谈加载(Loading)的那点事 2013/10/12 | 分类: 设计 | 0 条评论 | 标签: 交互设计, 加载 分享到:33 原文出处: 搜狐焦点 对于加载(loading),想必大家都不陌 ...

  3. 对H.264帧类型判断方法

    背景描述 我们经常在网络直播推流或者客户端拉流的时候,需要对获取到的H.264视频帧进行判断后处理,我们经常获取到各种不同的视频数据0x67 0x68 0x65 0x61,0x27 0x28 0x25 ...

  4. 使用注解来构造IoC容器-转

    新手,对于一些觉得有用的东西,直接转过来用了,自己理解的比较肤浅 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<co ...

  5. BZOJ2759: 一个动态树好题

    BZOJ2759: 一个动态树好题 Description 有N个未知数x[1..n]和N个等式组成的同余方程组:x[i]=k[i]*x[p[i]]+b[i] mod 10007其中,k[i],b[i ...

  6. 重新认识vue之事件阻止冒泡

    冒泡的表现 近期用vue做了一个需求,大概是同一个区域,点击不同位置有不同的响应函数,还有个总的响应函数,好吧,如下图所示: 他们的DOM结构如下: <div v-for="(item ...

  7. 基于BASYS2的VHDL程序——数字钟(最终版)

    转载请注明原地址:http://www.cnblogs.com/connorzx/p/3674178.html 调时电路正常工作.一切正常.发现做FPGA还是得从数电的思路思考,设置一个预置使能端,预 ...

  8. 存储过程系列三:根据表别名方式distinct去重插入

    1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, ...

  9. kali本機安裝openvas的血淚史復盤

    安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...

  10. Intellij IDEA 弹窗License activation 报 this license BIG3CLIK6F has been cancelled 错误的解决。

    this license BIG3CLIK6F has been cancelled 具体如下: 对,没错,这个激活码本来可以使用到2018年的,但是,忽然间就不能用了.经查询吧. 还得修改个系统配置 ...