地址选择

<template>
    <view class="address-container">
        <view class="select-address">
            <view :class="{
                'select-address-item': true,
                'select--active': currentIndex == idx
            }" v-for="(item, idx) in addressData" :key="idx" @click="changeCurrentIndex(idx)">{{item}}</view>
        </view>
        <view class="address-list-label">{{addressLabel}}</view>
        <scroll-view scroll-y class="address-list">
            <view v-for="item in addressTree" :key="item.id" @click="changeAddress(item)"
                :class="{'address-item':true,'select--active':item.label == addressData[currentIndex]}">
                {{item.label}}
            </view>
        </scroll-view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                addressTreeHistory: [],
                addressTree: [],
                addressData: ['请选择省份/地区'],
                currentIndex: 0,
            }
        },
        onLoad() {
            uni.request({
                url: 'https://static.yipintemian.com/json/region2.json',
                success: (res) => {
                    this.addressTree = res.data;
                    this.addressTreeHistory.push(this.addressTree)
                }
            })
        },
        computed: {
            addressLabel() {
                switch (this.currentIndex) {
                    case 0:
                        return '请选择省份/地区'
                    case 1:
                        return '请选择城市'
                    case 2:
                        return '请选择区/县'
                    default:
                        return ''
                }
            }
        },
        methods: {
            changeCurrentIndex(idx) {
                this.currentIndex = idx;
                this.addressTree = this.addressTreeHistory[idx];
            },
            changeAddress(value) {
                this.addressData.splice(this.currentIndex, this.addressData.length - this.currentIndex, value.label);
                if (this.currentIndex < 2) {
                    this.currentIndex++;
                    this.addressTree = value.children;
                    this.addressTreeHistory.splice(this.currentIndex, this.addressTreeHistory.length - this.currentIndex, value.children); // 清空当前级以及之后的历史栈
                    this.addressData.push(this.addressLabel);
                }
            }
        }
    }
</script>

<style scoped>
    .address-container {
        width: 100%;
        height: 100vh;
        display: flex;
        flex-direction: column;
    }

    .select-address {
        margin-bottom: 30rpx;
    }

    .select-address-item {
        padding: 10rpx 20rpx;
        color: #333;
        font-size: 26rpx;
    }

    .select--active {
        color: #2F5FC8 !important;
    }

    .address-list-label {
        color: #ccc;
        font-size: 24rpx;
        padding: 20rpx;
    }

    .address-list {
        flex: 1;
        overflow: hidden;
    }

    .address-item {
        padding: 20rpx;
        color: #333;
        font-size: 26rpx;
    }
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容