1:首先需要先安装@riophae/vue-treeselect":"^0.0.37
这是vue官方的中文文档可以参考 https://www.vue-treeselect.cn/#node
在这里插入图片描述
2:需要引入至vue页面使用

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  components: {
    Loadding,
    Treeselect,

  },

3:需要在页面中写入

          <div class="search_row_label">单位管理范围:</div>
              <div class="search_row_content">
                <treeselect
                 noResultsText="暂无结果" 
                 v-model="dwgxfw"
                  clearValueText="删除"     	//“×”按钮的标题
                  :searchable="false"           //是否启用搜索功能
                  :options="szxzList"           //选项数据
                  :load-options="loadOptions"   //用于动态加载选项
                  placeholder="请选择"
                  @select="changeSelect"        //选择一个选项后发出用于选择
                  @input="inputChange"          //输入框值更改后发出触发
                >
                
                </treeselect>

4:这里举例实际应用
首先需要先获取到父节点的值

 getParentLocalityName(){
      this.getRequest(this.Jurisdiction,"s_kj/s_kj_02/s_kj_02_01/loadDiscritctTreeRootNode","").then(res=>{
        // this.Loadding = false;
        //这里先判断状态
        if(res.data.state==1){
          let resData = res.data.data
          let objData = {}  //定义一个空对象
           objData.id = resData.localitycode;
          objData.label = resData.localityname;
          objData.name = resData.localitydesc;
          objData.children = null;
            this.szxzList.push(objData)  //在这里将获取到的数据代入
            console.log(this.szxzList)
        }else{
              this.Loadings = false;
              this.$Message.error(res.data.msg);
        }
      }).catch(error=>{
                this.Loadings = false;

      })

5:当点击时加载子数据

  loadOptions({action,parentNode,callback}){
  //这里有三个参数
  action   获取到的值
  parentNode  加载子选项时显示
  callback		接受error参数的函数
      console.log(parentNode)
      let params={
                parentLocCode: parentNode.id
      }
      this.getRequest(this.Jurisdiction,"s_kj/s_kj_02/s_kj_02_01/loadDiscrictTreeNode",urlPath(params)).then(res=>{
        if(res.data.state==1){
          if(res.data.data.length>0){
            let resData =res.data.data
            let arr = []   //定义空数组
            resData.forEach(item=>{
              let objData={}
              objData.id = item.localitycode;
              objData.label = item.localityname;
              objData.name = item.localitydesc;
              console.log(item.localitylevel)
               if (item.localitylevel <=item.localitylevel+1) {  //这里选择需要获取几个子节点进行判断
                objData.children = null
                   objData.loading=false;
              }
              arr.push(objData)  //将获取的数据代入
            })
           parentNode.children = arr;
            callback();

          }
        }
      })

    },  
    changeSelect(n,i){
      console.log(n)
   if (n.label == "长江流域") {
        this.dwgxfw = "";
      } else {
        console.log(222)
        this.dwgxfw = n.label;
        this.dwglcode = n.id
      }

    },
     inputChange(n, i) {
      if (n == undefined) {
        this.dwgxfw = "";
      }
    },

6:实际的效果图

在这里插入图片描述

Logo

前往低代码交流专区

更多推荐