el-tree懒加载,并加入搜索功能(后台搜)

完整页面代码

<template>
  <div style="width:100%;">
    <div class="company"></div>
    <div style="position:relative">
      <el-input @keyup.enter.native="submit" placeholder="请输入关键词" v-model="filterText"></el-input>
      <div class="search-icon">
        <img width="100%" height="100%" @click="search" src="@/assets/img/搜索.png" alt />
      </div>
    </div>
    <!-- default-expand-all设置默认全部展开设置缩进 -->
    <el-tree
      class="filter-tree"
      :props="defaultProps"
      accordion
      :load="loadNode"
      :data="treedata"
      lazy
      :indent="0"
      :check-on-click-node="true"
      @check="setSelectedNode"
      :filter-node-method="filterNode"
      ref="tree"
    >
      <!-- <span class="slot-t-node" slot-scope="{node,data}">
    <i :class="{'fa fa-folder':!node.expanded,'fa fa-folder-open':node.expanded,'fa fa-file':node.isLeaf}" style="color:#333" />
    <span style="color:red;font-weight:600" v-show="data.existFD==0&&node.isLeaf">{{node.label}}</span>
    <span v-show="data.existFD!=0">{{node.label}} </span>
      </span>-->
    </el-tree>
    <!-- <div class="buttons">
  <el-button @click="getCheckedNodes">通过 node 获取</el-button>
  <el-button @click="getCheckedKeys">通过 key 获取</el-button>
  <el-button @click="setCheckedNodes">通过 node 设置</el-button>
  <el-button @click="setCheckedKeys">通过 key 设置</el-button>
  <el-button @click="resetChecked">清空</el-button>
    </div>-->
  </div>
</template>
<script>
import { path } from "../../router/path";
import { mapState, mapMutations, mapGetters, mapActions } from "vuex";
import { searchPvtree, SEARCH } from "../../axios/dataCheck";
export default {
  props: {},
  data() {
    return {
      isthis: 0, //标识判断是哪种树
      //tab页面导航路由
      router: [],
      filterText: "",
      treedata: [],
      oid: "", //中转oid,真正的oid存于模块a中
      label: "",
      node: "",
      resolve: "",
      checktype: "",
      documentid: "", //中转documentid,真正的存于模块a中
      historyid: "", //中转historyDocumentid,真正的存于模块a中
      defaultProps: {
        children: "children",
        label: "LABEL"
        // label: 'label'
      }
    };
  },
  beforeCreate() {},
  created() {
    //   path[0].children[0].children[1].children.forEach(item=>{
    //   //设置左侧导航对应的路由
    //    this.router.push({path:path[0].path+'/'+path[0].children[0].path+'/'+path[0].children[0].children[1].path+'/'+item.path})
    // }),
    // path[0].children[0].children[1].children[2].children.forEach(item1=>{
    //   this.router.push({path:path[0].path+'/'+path[0].children[0].path+'/'+path[0].children[0].children[1].path+'/'+path[0].children[0].children[1].children[2].path+'/'+item1.path})
    // })
  },
  mounted() {
    // searchPvtree().then(res=>{
    //   console.log(res.data.data)
    //   this.data=res.data.data
    //   console.log(this.data)

    // })
    //每次tree组件渲染清空参数。
    this.setDkxoid("");
    this.setLabel("");
    this.setchecktype("");
  },
  computed: {},
  watch: {
    gettreeData(val) {}
    //设置输入框输入时自动过滤,无需点击搜索按钮
    // filterText(val) {
    //   this.$refs.tree.filter(val);
    // }
  },
  methods: {
    ...mapActions("a", ["setDkxoid", "setLabel", "setchecktype"]),
    //查询树通用方法,通过isthis标识符判断哪种调用
    commonSearch() {
      if (!this.filterText) {
        this.isthis = 0;
        this.treedata = [];
        searchPvtree({ level: "0" })
          .then(res => {
            console.log(res.data.success);
            if (res.data.success) {
              this.treedata = res.data.data;
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch(() => {
            let data = [];
            return resolve(data);
          });
      } else {
        this.isthis = 1;
        this.treedata = [];
        SEARCH({ searchName: this.filterText }).then(res => {
          console.log(res);
          this.treedata = res.data.data;
        });
      }
    },
    //查询
    search() {
      this.commonSearch();
    },
    //enter
    submit() {
      this.commonSearch();
    },

    commonSearchT(val, resolve) {
      // 查找顶级对象
      searchPvtree(val)
        .then(res => {
          if (res.data.success) {
            return resolve(res.data.data);
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          console.log("错误");
          let data = [];
          return resolve(data);
        });
    },
    // 加载树结点
    loadNode(node, resolve) {
      // isthis为0则调取全searchPvtree
      if (this.isthis == 0) {
        // 如果是顶级的父节点
        if (node.level === 0) {
          let paraobj = { level: "0" };
          this.commonSearchT(paraobj, resolve);
        } else {
          if (node.data.PARENTID) {
            let paraobj = {
              oid: node.data.OID.toString(),
              level: node.level.toString(),
              parentId: node.data.PARENTID
            };
            this.commonSearchT(paraobj, resolve);
          }
          else{
          // 根据父节点id找寻下一级的所有节点
          let paraobj = {
            oid: node.data.OID.toString(),
            level: node.level.toString()
          };
          this.commonSearchT(paraobj, resolve);
          }
        }
      } else {
        // isthis为1则不调取
        let data = [];
        return resolve(data);
      }
    },
    //实现单选
    setSelectedNode(data, obj) {
    

**//如果要控制单选,必须指定nodekey, //this.$refs.tree.setCheckedKeys([data]);**

      console.log(data.LABEL, data);
      var that = this;
      //最下一级有字段ISLEAF
      //加一层判断必须要有checktype才允许校验
      if (data.CHECKTYPE) {
        console.log(99);
        this.oid = data.OID; //用于搜索部门列表
        this.label = data.LABEL;
        this.checktype = data.CHECKTYPE;
        this.setDkxoid(this.oid); //设置修改大块线oid,只有当他不为空是才赋值发生值改变
        this.setLabel(this.label);
        this.setchecktype(this.checktype);
      } else {
        this.setchecktype("");
        this.setDkxoid(""); //设置修改大块线oid,只有当他不为空是才赋值发生值改变
      }
    },
    //树的获取设置操作
    getCheckedNodes() {
      // console.log(this.$refs.tree.getCheckedNodes());
    },
    getCheckedKeys() {
      // console.log(this.$refs.tree.getCheckedKeys());
    },
    setCheckedNodes() {
      this.$refs.tree.setCheckedNodes([
        {
          id: 5,
          label: "二级 2-1"
        },
        {
          id: 8,
          label: "二级 3-2"
        }
      ]);
    },
    setCheckedKeys() {
      this.$refs.tree.setCheckedKeys([13]);
    },
    resetChecked() {
      this.$refs.tree.setCheckedKeys([]);
    },
    //数内容筛选
    filterNode(value, data) {
      if (!value) return true;
      return data.LABEL.indexOf(value) !== -1;
    }
  }
};
</script>
<style lang="scss">
//设置搜索按钮
.search {
  display: inline-block;
  margin-left: 0.2rem;
  padding: 0.03rem 0.15rem;
  background: rgb(0, 112, 107);
  font-size: 0.14rem;
  color: white;
  border-radius: 0.04rem;
  cursor: pointer;
}
//设置搜索图标
.search-icon {
  position: absolute;
  left: 55%;
  top: 0.05rem;
  width: 0.12rem;
  height: 0.12rem;
}
.search-icon img {
  width: 0.12rem;
  height: 0.12rem;
}
//设置搜索框
.main_tree .el-input {
  outline: none;
  width: 60%;
  margin-left: 0.08rem;
  .el-input__inner {
    outline: none;
    display: inline-block;
    width: 100%;
    height: 0.3rem !important;
    line-height: 0.3rem !important;
    font-size: 0.1rem;
    text-indent: 0.01rem;
    border-radius: 0.15rem;
  }
}
//设置公司名称
.company {
  text-indent: 0.08rem;
  font-size: 0.16rem;
  color: #333333;
  font-weight: 800;
  margin-bottom: 0.1rem;
}
//修改Tree样式
.el-tree-node {
  .el-tree-node__expand-icon.is-leaf {
    display: none;
  }
  //设置叶子节点显示checkbox,设置checkbox与文字的间距
  .is-leaf + .el-checkbox .el-checkbox__inner {
    display: inline-block;
    margin: 0 0.05rem;
  } //设置其他节点不显示checkbox
  .el-checkbox .el-checkbox__inner {
    display: none;
  }
  //设置tree文字的大小
  .el-tree-node__label {
    font-size: 0.14rem;
    font-weight: 600;
  }
}
// 设置虚线
.el-tree-node {
  position: relative;
  padding-left: 0.04rem !important;
  margin-bottom: 0.02rem;
}
.el-tree-node__children {
  padding-left: 0.35rem;
}
.el-tree-node :last-child:before {
  height: 0.38rem;
}
.el-tree > .el-tree-node:before {
  border-left: none;
}
.el-tree > .el-tree-node:after {
  border-top: none;
}
.el-tree-node:before,
.el-tree-node:after {
  content: "";
  left: -0.22rem;
  position: absolute;
  right: auto;
  border-width: 0.01rem;
}
.tree :first-child .el-tree-node:before {
  border-left: none;
}
//--start
// //竖直的虚线
.el-tree-node:before {
  border-left: 0.01rem dashed rgb(191, 191, 191);
  bottom: 0.01rem;
  height: 100%;
  top: -0.27rem;
  width: 0.01rem;
}
//水平的虚线
.el-tree-node:after {
  border-top: 0.01rem dashed rgb(191, 191, 191);
  height: 0.2rem;
  top: 0.13rem;
  width: 0.22rem;
}
//修改展开图标
.el-tree .el-icon-caret-right:before {
  background: url("../../assets/img/展开.png") no-repeat 0 0.01rem;
  content: "";
  display: block;
  width: 0.14rem;
  height: 0.14rem;
  font-size: 0.14rem;
  background-size: 0.14rem;
}
//修改收起图标
.el-tree .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
  background: url("../../assets/img/收起.png") no-repeat -0.01rem 0;
  content: "";
  display: block;
  width: 0.14rem;
  height: 0.14rem;
  font-size: 0.14rem;
  background-size: 0.16rem;
} //修改原本点击展开或收起图标会旋转的样式
.el-tree .el-tree-node__expand-icon.expanded {
  -webkit-transform: rotate(0deg);
  transform: rotate(0deg);
}
//-end

.slot-t-node span {
  color: #333;
  font-size: 0.14rem;
  font-family: "Microsoft YaHei" !important;
}
.el-tree-node__expand-icon {
  color: #333 !important;
  font-size: 0.21rem !important;
}

.el-tree-node__content > .el-tree-node__expand-icon {
  padding: 0.06rem !important;
}
//设置对齐图标
.fa {
  padding-left: 0.03rem !important;
}
</style>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现 el-tree懒加载搜索,可以通过监听输入框的值来控制懒加载。具体实现步骤如下: 1. 首先,在 el-tree 标签上添加一个 ref 属性,用于获取 el-tree 的实例,例如 ref="tree" 。 2. 在 data 中定义一个 filterText 变量,用于保存输入框的值。 3. 在 watch 中监听 filterText 变量的变化,当有值时,关闭懒加载后端搜索的方法;当没有值时,开启懒加载并重新加载数据 。 4. 在 el-tree 标签上添加 lazy 属性并设置为 true,表示开启懒加载 。 5. 在 el-tree 标签上添加 load 属性,并绑定一个 loadNode 方法,用于在树节点展开时加载数据 。 以下是代码示例: ``` <template> <div> <input type="text" v-model="filterText" placeholder="输入搜索关键字" /> <el-tree :data="orgList" ref="tree" show-checkbox :props="defaultProps" node-key="id" @node-click="handleNodeClick" @check-change="handleClick" :default-expanded-keys="expandArr" lazy :load="loadNode" > <span class="custom-tree-node" slot-scope="{ node, data }"> <span class="tree-icon"> <img v-if="data.isTop" style="width: 20px; height: 16px" src="@/assets/img/repositoryImg/tree.png" alt />  {{ node.label }} </span> </span> </el-tree> </div> </template> <script> export default { data() { return { filterText: '', // 保存输入框的值 orgList: [], // 树形数据 defaultProps: { children: 'children', label: 'label' } }; }, watch: { filterText(val) { if (val) { this.$refs.tree.$data.store.lazy = false; // 当输入框有值时关闭懒加载 this.getSeachList(); // 获取后端搜索的数据 } else { this.$refs.tree.$data.store.lazy = true; // 开启懒加载 this.getOrgList(); // 加载数据 } } }, methods: { // 加载树节点方法 loadNode(node, resolve) { // 根据业务需求实现加载树节点数据的方法 }, // 获取后端搜索的数据 getSeachList() { // 根据业务需求实现后端搜索的方法 }, // 获取树形数据 getOrgList() { // 根据业务需求实现获取树形数据的方法 }, // 节点点击事件 handleNodeClick(node) { // 根据业务需求实现节点点击事件的方法 }, // 复选框点击事件 handleClick() { // 根据业务需求实现复选框点击事件的方法 } } }; </script> ``` 以上就是实现 el-tree 懒加载搜索的方法。根据输入框的值来控制懒加载开关,并实现后端搜索的数据获取。同时使用 lazy 属性和 load 方法实现 el-tree懒加载功能
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值