最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實(shí)現(xiàn)下拉多選、可搜索、全選功能(示例代碼)

 更新時(shí)間:2025年01月01日 08:32:35   作者:emoji111111  
本文介紹了如何在Vue中實(shí)現(xiàn)一個(gè)樹形結(jié)構(gòu)的下拉多選組件,支持任意一級選項(xiàng)的選擇,全選功能,以及搜索功能,通過在mounted生命周期中獲取數(shù)據(jù),并使用handleTree函數(shù)將接口返回的數(shù)據(jù)整理成樹形結(jié)構(gòu),實(shí)現(xiàn)了這一功能,感興趣的朋友一起看看吧

最后的效果就是樹形的下拉多選,可選擇任意一級選項(xiàng),下拉框中有一個(gè)按鈕可以實(shí)現(xiàn)全選,也支持搜索功能。

在mounted生命周期里面獲取全部部門的數(shù)據(jù),handleTree是講接口返回的數(shù)據(jù)整理成樹形結(jié)構(gòu),可以自行解決

             <div class="LeftText">
                <span style="color: red; margin-right: 4px">*</span>部門:
              </div>
              <el-select
                v-model="executiveDepartName"
                filterable
                :filter-method="selectChange"
                multiple
                @visible-change="visibleChange"
                @remove-tag="seleRemoveTag"
                style="width: 80%"
              >
                <el-option style="display: none" value=""></el-option>
                <el-checkbox
                  style="
                    width: 100%;
                    height: 40px;
                    line-height: 40px;
                    padding-left: 20px;
                    border-bottom: 1px solid #dcdfe6;
                  "
                  class="allselect"
                  :indeterminate="isIndeterminate"
                  v-model="allSelectModule"
                  @change="allselect"
                  >全選</el-checkbox
                >
                <el-cascader-panel
                  ref="cascaderModule"
                  :key="deptList.length"
                  :options="deptList"
                  @change="cascaderChange"
                  style="width: 80%"
                  :props="props"
                  filterable
                  :border="false"
                  :show-all-levels="false"
                  v-model="executiveDepartment"
                >
                </el-cascader-panel>
              </el-select>
            </div>
     props: {
        multiple: true,
        value: "deptId",
        label: "deptName",
        checkStrictly: true,
        emitPath: false,
      },   
    allDeptList:[];//所有的部門信息,內(nèi)部結(jié)構(gòu)為:{deptId:1,deptName:"一級部門"}
    isSeach:false;//是否搜索狀態(tài)
    tempExecutive:[];// 搜索前已選中的數(shù)據(jù)
    //搜索查詢事件--是因?yàn)樵赾ascaderChange事件中,對v-model的值重新賦值,導(dǎo)致下拉選時(shí),會觸發(fā)el-select的搜索事件,所以加了一個(gè)isFilter判斷
    selectChange(val) {
      if (val !== "") {
        this.deptList = [];
        this.deptList = this.allDeptList.filter((item) => {
          return item.deptName.toLowerCase().indexOf(val.toLowerCase()) > -1;
        });
        this.isSeach = true;
        this.tempExecutive = this.executiveDepartment;
      } else {
        if (!this.isFilter) {
          this.deptList = this.handleTree(this.allDeptList, "deptId");
          this.isFilter = !this.isFilter;
        }
      }
    },
    visibleChange(e) {
      if (e) {
        this.isSeach = false;
        this.isFilter = false;
        this.deptList = this.handleTree(this.allDeptList, "deptId");
        this.initStatus();
      }
    },
    對全選狀態(tài)進(jìn)行重新賦值
    initStatus() {
      if (this.executiveDepartment.length == this.allDeptList.length) {
        this.allSelectModule = true;
        this.isIndeterminate = false;
      } else if (this.executiveDepartment.length == 0) {
        this.allSelectModule = false;
        this.isIndeterminate = false;
      } else {
        this.allSelectModule = false;
        this.isIndeterminate = true;
      }
    },
    //select框里回顯的是選中部門的名稱
    getDeptName() {
      const result = [];
      this.executiveDepartment.filter((item) => {
        this.allDeptList.map((i) => {
          if (item == i.deptId) {
            result.push(i.deptName);
          }
        });
      });
      return result;
    },
    seleRemoveTag(val) {
      if (val) {
        const result = this.allDeptList.find((item) => {
          if (item.deptName == val) {
            return item;
          }
        });
        this.executiveDepartment = this.executiveDepartment.filter(
          (item) => item !== result.deptId
        );
      }
    },
    // 下拉多選選中時(shí)觸發(fā)的事件
    cascaderChange() {
      this.isFilter = true;
      //如果是搜索狀態(tài),講之前選中的值和搜素狀態(tài)下的值進(jìn)行合并和去重,否則,之前選中的值會被清空
      if (this.isSeach) {
        this.executiveDepartment = [
          ...new Set([...this.tempExecutive, ...this.executiveDepartment]),
        ];
      }
      this.executiveDepartName = this.getDeptName();
      this.initStatus();
    },
    //全選事件
    allselect() {
      if (this.allSelectModule) {
        this.isIndeterminate = false;
        if (this.isSeach) {
          this.executiveDepartment = this.deptList.map((item) => item.deptId);
          this.executiveDepartName = this.getDeptName();
        } else {
          this.executiveDepartment = this.getAllIds(this.deptList);
          this.executiveDepartName = this.getDeptName();
        }
      } else {
        this.executiveDepartment = [];
        this.executiveDepartName = [];
      }
    },
    getAllIds(nodes) {
      let ids = [];
      (function getIds(nodes) {
        nodes.forEach((node) => {
          ids.push(node.deptId);
          if (node.children && node.children.length) {
            getIds(node.children);
          }
        });
      })(nodes);
      return ids;
    },

到此這篇關(guān)于vue實(shí)現(xiàn)下拉多選、可搜索、全選功能的文章就介紹到這了,更多相關(guān)vue下拉框多選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

临城县| 尉犁县| 湖口县| 永吉县| 江北区| 雷山县| 青河县| 庆城县| 武山县| 吐鲁番市| 井陉县| 偏关县| 若羌县| 新野县| 金川县| 淮阳县| 安达市| 五家渠市| 色达县| 海盐县| 城市| 蒲江县| 邳州市| 盐津县| 苏尼特右旗| 祁门县| 旬邑县| 邵阳市| 高邑县| 新郑市| 巍山| 吉林省| 华容县| 温宿县| 沈阳市| 章丘市| 延川县| 梨树县| 南平市| 哈巴河县| 赤峰市|