【vue组件】使用element-ui 实现三级联动下拉选择

实现的思路是第一个下拉选择在选择了选项后将该选项的信息传递到接口请求下一个选项的内容,依次类推
然后在清除了上一级选择的选项后要将其次级和子孙级的选项都清除(包括选择里的列表内容)
下面看具体代码:

<template>
  <div>
    <div>
      <el-form
        :size="'small'"
        :inline="true"
        :model="ruleForm"
        ref="ruleForm"
        label-width="120px"
        label-position="left"
        :rules="addRules"
      >
        <div class="scan_title">展示:</div>
		<!-- 自定义清除方法,注意change方法在clear前执行 -->
        <el-row>
          <el-form-item label="一级选项:" prop="firstId">
          	
            <el-select
              v-model="ruleForm.firstId"
              placeholder="请选择一级选项"
              @change="changeFirst"
              @clear="handleClearFirstId"
              style="width: 220px"
              clearable
            >
              <el-option
                v-for="item in allFirstList"
                :key="item.firstId"
                :label="item.firstNo"
                :value="item.firstId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="次级选项:" prop="secondId">
            <el-select
              style="width: 220px"
              v-model="ruleForm.secondId"
              placeholder="请选择次级选项编号"
              @change="changeSecond"
              @clear="handleClearSecondId"
              clearable
            >
              <el-option
                v-for="item in allSecondList"
                :key="item.secondId"
                :label="item.secondNo"
                :value="item.secondId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="三级选项:" prop="thirdId">
            <el-select
              style="width: 220px"
              v-model="ruleForm.thirdId"
              placeholder="请选择三级选项编号"
              @change="changeThird"
              value-key="thirdId"
              clearable
            >
              <el-option
                v-for="item in allThirdList"
                :key="item.thirdId"
                :label="item.thirdNo"
                :value="item.thirdId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>


export default {
  name: "LinkingSelect",
  props: {},
  data() {
    return {
      selectedLabelList: [],
      filSelectedLabels: [],
      refreshKey: 0,
      ruleForm: {},
      dynamicTags: [],
      tagVisible: false,
      allFirstList: [],
      allSecondList: [],
      allThirdList: [],
      addRules: {
        firstId: [
          {
            required: true,
            trigger: "change,blur",
            message: "请选择一级选项",
          },
        ],
        secondId: [
          {
            required: true,
            trigger: "change,blur",
            message: "请选择次级选项",
          },
        ],
        thirdId: [
          {
            required: true,
            trigger: "change,blur",
            message: "请选择三级选项",
          },
        ],
      },
    };
  },
  created() {
    this.handleGetAllFirst();
  },
  mounted() {
    
  },

  methods: {
    // 清除次级选择以及子孙选择的内容
    handleClearSecondId() {
      this.ruleForm.secondId = "";
      this.ruleForm.thirdId = "";
      this.allThirdList = [];
    },

    // 清除一级选择以及子孙选择的内容
    handleClearFirstId() {
      this.ruleForm.firstId = "";
      this.ruleForm.secondId = "";
      this.ruleForm.thirdId = "";
      this.allThirdList = [];
      this.allSecondList = [];
    },

    // 清除最后一级选择的内容并刷新
    changeThird() {
      this.$forceUpdate();
    },
    changeSecond(secondId) {
      this.ruleForm.thirdId = "";
      // 判断是否为空字符串
      if (secondId && secondId !== "") {
        // 请求接口
        // getThird({
        //   secondId: secondId,
        //   fileId: this.ruleForm.fileId,
        // }).then((res) => {
        //   this.allThirdList = res.data;
        // });

        // 模拟数据
        this.allThirdList = [
          {
            createBy: "admin",
            createTime: "2023-03-27 10:24:30",
            updateBy: "",
            updateTime: null,
            remark: null,
            thirdId: 3,
            thirdNo: "H0",
            leftCapacity: 1,
            totalCapacity: 5,
            secondNo: "X0",
            secondId: 11,
            firstNo: "G0",
            firstId: 11,
            manageBy: "admin",
            delFlag: "0",
            deptId: 100,
            deptName: "若依科技",
            beginTime: null,
            endTime: null,
            userId: null,
          },
        ];
      }
    },

    changeFirst(firstId) {
      this.ruleForm.secondId = "";
      this.ruleForm.thirdId = "";

      if (firstId !== "") {
        // 请求接口  
        // getSecond({
        //   firstId: firstId,
        //   fileId: this.ruleForm.fileId,
        // }).then((res) => {
        //   this.allSecondList = res.data;
        // });
        // 模拟数据
        this.allSecondList = [
          {
            createBy: "admin",
            createTime: "2023-03-27 10:24:10",
            updateBy: "",
            updateTime: null,
            remark: null,
            secondId: 11,
            secondNo: "X0",
            leftCapacity: 4,
            totalCapacity: 5,
            firstId: 11,
            firstNo: "G0",
            manageBy: "admin",
            delFlag: "0",
            deptId: 100,
            deptName: "若依科技",
            userId: null,
            thirdId: null,
          },
        ];
      }
    },
    handleGetAllFirst() {
      // getFirst({
      //   fileId: this.ruleForm.fileId,
      // }).then((res) => {
      //   this.allFirstList = res.data;
      // });

      this.allFirstList = [
        {
          createBy: "admin",
          createTime: "2023-03-27 10:23:57",
          updateBy: "",
          updateTime: null,
          remark: null,
          firstId: 11,
          firstNo: "G0",
          leftCapacity: 5,
          totalCapacity: 5,
          manageBy: "admin",
          delFlag: "0",
          deptId: 100,
          deptName: "若依科技",
          beginTime: null,
          endTime: null,
          userId: null,
          fileCate: null,
          secondId: null,
          thirdId: null,
        },
        {
          createBy: "aduser",
          createTime: "2023-03-27 10:15:12",
          updateBy: "",
          updateTime: null,
          remark: null,
          firstId: 6,
          firstNo: "G1",
          leftCapacity: 0,
          totalCapacity: 5,
          manageBy: "aduser",
          delFlag: "0",
          deptId: 201,
          deptName: "管理部",
          beginTime: null,
          endTime: null,
          userId: null,
          fileCate: null,
          secondId: null,
          thirdId: null,
        },
        {
          createBy: "aduser2",
          createTime: "2023-03-27 10:00:14",
          updateBy: "aduser2",
          updateTime: "2023-03-27 10:14:31",
          remark: null,
          firstId: 1,
          firstNo: "G11",
          leftCapacity: 0,
          totalCapacity: 5,
          manageBy: "aduser2",
          delFlag: "0",
          deptId: 207,
          deptName: "管理一部",
          beginTime: null,
          endTime: null,
          userId: null,
          fileCate: null,
          secondId: null,
          thirdId: null,
        },
      ];
    },
  },
};
</script>
<style scoped>

</style>

效果图:
在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3与Element-UI的结合是一个非常常见的前端开发实践,Element-UI是一个基于Vue.js的开源UI组件库,提供了丰富的预定义UI组件和一套高效的开发工具。以下是使用Element-UIVue3中的一般步骤: 1. **安装依赖**: - 首先,你需要确保已经安装了Vue3,可以通过`npm install vue@next`或`yarn add vue@next`来安装。然后,安装Element-UI,运行`npm install element-plus`或`yarn add element-plus`。 2. **引入并配置**: 在`main.js`或`vite.config.ts`(如果是使用Vite)中引入Element-Plus并配置: ```javascript import { createApp } from 'vue'; import { ElButton, ElContainer } from 'element-plus'; import App from './App.vue'; createApp(App) .use(ElementPlus) .mount('#app'); ``` 或者,如果你更喜欢使用Vue CLI,可以在`.vue`文件里直接使用`<script setup>`标签导入Element-UI组件: ```html <template> <div id="app"> <el-button>Button</el-button> <el-container></el-container> </div> </template> <script setup> import { ElButton, ElContainer } from 'element-plus'; const app = defineApp('App', () => ({ setup() { return () => ( <> <ElButton>Button</ElButton> <ElContainer></ElContainer> </> ); } })); </script> ``` 3. **使用组件**: Element-UI提供了大量的预定义组件,如按钮(`<el-button>`)、容器(`<el-container>`),你可以根据需要将它们添加到你的Vue组件中,按照Element-UI的API文档来使用它们。 4. **CSS主题**: 如果需要更改样式,可以引入Element-UI的主题文件,或者自定义主题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值