地址選擇

<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: ['請(qǐng)選擇省份/地區(qū)'],
                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 '請(qǐng)選擇省份/地區(qū)'
                    case 1:
                        return '請(qǐng)選擇城市'
                    case 2:
                        return '請(qǐng)選擇區(qū)/縣'
                    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); // 清空當(dāng)前級(jí)以及之后的歷史棧
                    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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容