vue下拉el-select二级联动

 <el-select v-model="departmentid" placeholder="请选择部门" @change="changeset" >
	 <el-option v-for="item in departmentlist" :key="item.value" 
	 	:label="item.name"  :value="item.id">
	 </el-option>
 </el-select>
 <el-select v-model="personid" placeholder="请选择人员" :disabled="disabled">
	 <el-option v-for="item in personlist" :key="item.value"
	   :label="item.name" :value="item.id">
	 </el-option>
 </el-select>
data() {
    return {
      departmentlist: [], //部门list
      departmentid: null, //部门id
      
      personlist: [],  //人员list
      personid: null,  //人员id
      
      disabled: true,  //禁用
    };
  },

  mounted() {
    this.getdepartmentlist();  //获取部门list
  },

  methods: {
    getdepartmentlist() {
      //模拟部门数据
      let data = [
        { id: 1, name: "研发部" },
        { id: 2, name: "项目部" },
        { id: 3, name: "市场部" },
        { id: 4, name: "行政部" },
      ];
      //赋给部门下拉框
      this.departmentlist = data;
    },
    //切换部门id==>根据部门id获取人员list
    changeset() {
      //模拟人员数据
      let data = [
        {
          id: 1,             //人员id
          name: "张三",
          departmentid: 1,   //部门id
        },
        { id: 2, name: "李四", departmentid: 2 },
        { id: 3, name: "王五", departmentid: 1 },
        { id: 4, name: "赵六", departmentid: 3 },
        { id: 5, name: "小红", departmentid: 4 },
        { id: 6, name: "小明", departmentid: 4 },
        { id: 7, name: "小李", departmentid: 4 },
        { id: 8, name: "小张", departmentid: 4 },
      ];
      //如果部门id不为空
      if (!!this.departmentid) {
        //切换的时候人员清空,防止bug
        this.personlist = [];
        //禁用取消
        this.disabled = false;
        //遍历人员里面的数据
        data.forEach((e) => {
          console.log(e);
          //如果data里面部门id等于this.departmentid
          if (e.departmentid == this.departmentid) {
            //将符合的数据push进人员数组
            this.personlist.push(e);
          }
        });
        //选中部门,人员默认选中第一个
        if (this.personlist.length > 0) {
          this.personid = this.personlist[0].id;
        }
      } else {
		//选中的部门为空,则清空人员列表
        this.personlist = [];
        this.disabled = true;
      }
    },
  },
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐