Elementui 三级下拉联动

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
    <div id="app">
        <el-select v-model="linkAge.provinceName" @change="choseProvince" placeholder="省级地区">
            <el-option v-for="(item,$index) in province" :key="$index" :label="item.value" :value="item.id">
            </el-option>
        </el-select>
        <el-select v-model="linkAge.cityName" @change="choseCity" placeholder="市级地区">
            <el-option v-for="(item,$index) in city" :key="$index" :label="item.value" :value="item.id">
            </el-option>
        </el-select>
        <el-select v-model="linkAge.blockName" @change="choseBlock" placeholder="区级地区">
            <el-option v-for="(item,$index) in block" :key="$index" :label="item.value" :value="item.value">
            </el-option>
        </el-select>
        <el-button type="primary" @click="getData()">获取对象数据 在控制台查看</el-button>
    </div>
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>

        //模拟数据
        const ChineseDistricts = [{
            code: "001",
            name: "北京",
            cityList: [
                {
                    code: "001001",
                    name: "西城区",
                    areaList: [
                        {
                            code: "001001001",
                            name: "西城区瓜子哥"
                        },
                        {
                            code: "001001002",
                            name: "西城区瓜子哥2"
                        }
                    ]
                },
                {
                    code: "001002",
                    name: "东城区",
                    areaList: [
                        {
                            code: "001002001",
                            name: "东城区瓜子哥"
                        },
                        {
                            code: "001002002",
                            name: "东城区瓜子哥2"
                        }
                    ]
                }
            ]
        },
        {
            code: "002",
            name: "深圳",
            cityList: [
                {
                    code: "002001",
                    name: "龙岗区",
                    areaList: [
                        {
                            code: "002001001",
                            name: "龙岗区瓜子哥"
                        },
                        {
                            code: "002001002",
                            name: "龙岗区瓜子哥2"
                        }
                    ]
                },
                {
                    code: "002002",
                    name: "福田区",
                    areaList: [
                        {
                            code: "002002001",
                            name: "福田区瓜子哥"
                        },
                        {
                            code: "002002002",
                            name: "福田区瓜子哥2"
                        }
                    ]
                }
            ]
        }
        ]

        new Vue({
            el: '#app',
            data() {
                return {
                    ChineseDistricts: ChineseDistricts,
                    //绑定三级联动下拉的模型
                    province: [],
                    city: [],
                    block: [],
                    //临时数据存储
                    cityList: [],
                    blockList: [],
                    //三级联动的对象 最后选中的结果集合
                    linkAge: {
                        provinceName: '',//省的名字
                        cityName: '',//市的名字
                        blockName: ''  //区的名字
                    }
                }
            },
            methods: {
                // 加载china地点数据,三级
                getCityData: function () {
                    let that = this;
                    that.ChineseDistricts.forEach(function (item, index) {
                        //省级数据
                        that.province.push({ id: item.code, value: item.name, children: item.cityList })
                    })
                },
                // 选省
                choseProvince: function (e) {
                    let that = this;
                    //重置数据
                    that.city = [];
                    that.block = [];
                    that.linkAge.cityName = '';
                    that.linkAge.blockName = '';
                    for (var index in that.province) {
                        if (e === that.province[index].id) {
                            that.cityList = that.province[index].children
                            that.linkAge.provinceName = that.province[index].value
                            that.cityList.forEach(function (item, cindex) {
                                that.city.push({ id: item.code, value: item.name, children: item.areaList })
                            })
                        }
                    }
                },
                // 选市
                choseCity: function (e) {
                    let that = this;
                    that.block = [];
                    that.linkAge.blockName = '';
                    for (var index in that.city) {
                        if (e === that.city[index].id) {
                            that.blockList = that.city[index].children
                            that.linkAge.cityName = that.city[index].value
                            that.E = that.blockList[0].id
                            that.blockList.forEach(function (item, bindex) {
                                that.block.push({ id: item.code, value: item.name, children: [] })
                            })
                        }
                    }
                },
                // 选区
                choseBlock: function (e) {
                    this.linkAge.blockName = e;
                },
                getData: function () {
                    console.log(this.linkAge)
                }
            },
            created: function () {
                this.getCityData()
            }
        })
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值