ElementUI 省市区多选搜索(前台+后台传参)详细

用到的ElementUi组件

直达链接: https://element.eleme.cn/#/zh-CN/component/select

效果
在这里插入图片描述

在这里插入图片描述
一、前台(element-ui+vue)
1)添加组件
在这里插入图片描述
2)js部分,添加到return里


3)加入查询数组定义
在这里插入图片描述
4)搜索方法内添加

if(this.searchOptions.length>0){

        var cityCode = this.searchOptions;
        var zoneCode = this.searchOptions;

        /*省市区选择处理数据 start*/
        var provinceList = new Array();
        var cityList = new Array();
        var zoneList = new Array();

        cityCode.forEach(cit => {
          if(cit[1] != 'undefined' && cit[1] != undefined && cit[1] != null && cit[1] != ''){
            // cityList.push(CodeToText[cit[1]]);
          }else if(cit[0] != 'undefined' && cit[0] != undefined && cit[0] != null && cit[0] != ''){
            provinceList.push(CodeToText[cit[0]]);
          }
        });
        zoneCode.forEach(zon => {
          if(zon[2] != 'undefined' && zon[2] != undefined && zon[2] != null && zon[2] != ''){
            zoneList.push(CodeToText[zon[2]]);
          }else if(zon[1] != 'undefined' && zon[1] != undefined && zon[1] != null && zon[1] != ''){
            cityList.push(CodeToText[zon[1]]);
          }
        });

        let provinceListSet = new Set(provinceList);
        let cityListSet = new Set(cityList);
        let zoneListSet = new Set(zoneList);

        var proResult = "";
        var citResult = "";
        var zonResult = "";

        provinceListSet.forEach( ptemp =>{
          proResult = proResult + ptemp + ',';
        })
        cityListSet.forEach( ctemp =>{
          citResult = citResult + ctemp + ',';
        })
        zoneListSet.forEach( ztemp =>{
          zonResult = zonResult + ztemp + ',';
        })
        /*end*/

        this.queryParams.province = proResult;
        this.queryParams.city = citResult;
        this.queryParams.zone = zonResult;
      }else{
        this.queryParams.province = null;
        this.queryParams.city = null;
        this.queryParams.zone = null;
      }

二、后台方法
String,接收,sql处理

       if(StrUtil.isEmpty(paperMailInfo.getProvince())){
            paperMailInfo.setProvince(null);
        }
        if(StrUtil.isEmpty(paperMailInfo.getCity())){
            paperMailInfo.setCity(null);
        }
        if(StrUtil.isEmpty(paperMailInfo.getZone())){
            paperMailInfo.setZone(null);
        }

  <if test='zone != null or province != null or  city != null '>
                and (
                <if test='province != null'>
                    a.province in
                    <foreach item="id" collection='province.split(",")' open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </if>
                <if test='city != null'>
                    <if test='province != null'>or</if>
                    a.city in
                    <foreach item="id" collection='city.split(",")' open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </if>
                <if test='zone != null'>
                    <if test='province != null or city != null'>or</if>
                    a.zone in
                    <foreach item="id" collection='zone.split(",")' open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </if>
                )
            </if>

用到的省市区js放到下载了,命名为city.js引入
import { regionData } from “@/api/paper/city-data/city”;
点击下载省市区js
齐活=

### 实现省市区功能 为了实现在前端项目中实现省市区功能,可以基于 Vue 和 Element Plus 的 `el-cascader` 组件进行扩展。通过自定义逻辑处理需求,并利用 JSON 文件作为数据源。 #### 准备工作 确保已经获取并放置好省市区三级联动的数据文件 `pca-code.json` 在项目的合适位置[^1]。此文件应包含所有省份、城市及其下属区县的信息。 #### 自定义 Cascader 组件 创建一个新的 Vue 组件用于封装增强版的级联择器: ```vue <template> <div class="multi-cascader"> <el-cascader v-model="selectedOptions" :options="options" @change="handleChange" multiple clearable placeholder="请择地区" /> </div> </template> <script setup lang="ts"> import { ref, onMounted } from 'vue'; import pCodeData from '@/assets/pca-code.json'; // 假设json放在src/assets目录下 const options = ref([]); const selectedOptions = ref([]); onMounted(() => { formatData(pCodeData); }); function handleChange(value) { console.log('Selected:', value); } // 将原始数据转换成适合 el-cascader 显示的形式 function formatData(data) { const result = []; data.forEach(province => { let provinceObj = { label: province.name, value: province.code, children: [] }; if (province.children && Array.isArray(province.children)) { province.children.forEach(city => { let cityObj = { label: city.name, value: city.code, children: [] }; if (city.children && Array.isArray(city.children)) { city.children.forEach(district => { cityObj.children.push({ label: district.name, value: district.code }); }); } provinceObj.children.push(cityObj); }); } result.push(provinceObj); }); options.value = result; } </script> <style scoped> .multi-cascader { width: 300px; /* 可调整宽度 */ } </style> ``` 上述代码展示了如何构建一个版本的级联择器。这里的关键在于设置了 `multiple` 属性允许用户一次可以个路径;并通过 `formatData()` 方法将原始 json 数据转化为符合 `el-cascader` 需求的对象数组形式[^2]。 对于更复杂的场景比如需要支持半状态或者父子节点关联择等功能,则可能还需要进一步定制化开发,参考一些现有开源库的设计思路或文档说明[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xkng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值