前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >级联下拉单选框的实现

级联下拉单选框的实现

作者头像
不爱吃糖的程序媛
发布2024-01-18 21:42:47
1670
发布2024-01-18 21:42:47
举报
文章被收录于专栏:夏天的前端笔记

需求背景

选择某个城市,所在区县与所选城市要一一对应。

在这里插入图片描述
在这里插入图片描述

开发思路

因为单选框使用的是公共组件,获取的val是“深圳市”,而不是索引,那么可以 先判断出选择的城市的索引值;由此所在区县的下拉框的数据直接由所获的索引值去取。 数据结构如下:

在这里插入图片描述
在这里插入图片描述

源代码

代码语言:javascript
复制
//公共组件里的message
  message:{
        {
          key: "city",
          label: "城市",
          placeholder: "请选择",
          type: 1,
          selects: this.cityArr,
          defaults: "",
          change: val => this.getIndex(val)//选择下拉框的值是从而确定索引
        },
        {
          key: "county",
          label: "医药机构所在区县",
          placeholder: "请选择",
          type: 1,
          defaults: "",
          noClearable: this.ifNoClearable,
          selects: this.countyArr,
          filterable: true
        },
 },
 //获取城市下拉框选项
    getCity() {
      this.cityMy = [];
      List.cityAndCounty().then(res => {
        if (res.code === 0) {
          const data = res.result || [];
          for (let i = 0; i < data.length; i++) {
            this.cityMy.push(data[i].city);
          }
          this.cityArr = this.cityMy.map(item => {
            return { label: item, value: item };
          }); //拿到的是城市的列表
          this.message.forEach(it => {
            if (it.key === "city") {
              this.$set(it, "selects", this.cityArr);
            }
          });
          this.defaultAjax += 1;
        }
      });
    },
    //获取没选城市的医药机构所在区县下拉框选项
    getCountyName() {
      List.countyName().then(res => {
        if (res.code === 0) {
          this.countyNameArr = res.result.map(item => item.county);
        }
      });
    },
    //获取选择城市的索引值
    getIndex(val) {
      const cityIndex = this.cityMy.indexOf(val);
      if (cityIndex > -1) {
        this.getCounty(cityIndex);
      }
    },
    //获取所选城市的对应医药机构所在区县下拉框
    getCounty(cityIndex) {
      ScheduleList.isHospitalArea().then(res => {
        if (res.result === "已配置医院范围") {
          this.ifNoClearable = true;
          List.cityAndCounty().then(res => {
            if (res.code === 0) {
              const data = res.result || [];
              let countyMy = [];
              countyMy =cityIndex || cityIndex === 0 ? data[cityIndex].countyList : this.countyNameArr;
              this.countyArr = countyMy.map(item => {
                return { label: item, value: item };
              });
              this.message.forEach(it => {
                if (it.key === "county") {
                  this.$set(it, "selects", this.countyArr);
                }
              });
              this.defaultAjax += 1;
            }
          });
        } else if (res.result === "未配置医院范围") {
          this.ifNoClearable = false;
          List.cityAndCounty().then(res => {
            if (res.code === 0) {
              const data = res.result || [];
              let countyMy = [];
              countyMy = cityIndex || cityIndex === 0 ? data[cityIndex].countyList : this.countyNameArr;
              this.countyArr = countyMy.map(item => {
                return { label: item, value: item };
              });
              this.message.forEach(it => {
                if (it.key === "county") {
                  this.$set(it, "selects", this.countyArr);
                }
              });
              this.defaultAjax += 1;
            }
          });
        }
      });
    },

级联下拉单选框

其实像级联下拉单选框还有一些框架就有现成的,可以参考一下这些框架。

1.elementUI: https://element.eleme.cn/#/zh-CN/component/cascader

在这里插入图片描述
在这里插入图片描述

  1. Ant Design: https://3x.ant.design/components/cascader-cn/
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求背景
  • 开发思路
  • 源代码
  • 级联下拉单选框
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档