springboot+vue树形懒加载开发

后台接口主要代码

  1. 实现逻辑(入参也可是id根据实际情况而定)
 public List<Entity> selectTreeListById(Entity entity) {
        List<Entity > list = mapper.selectEntityByParentId(entity);
        list.forEach(e-> {
            boolean isChild = this.isChildren(e);
            entity.setHasChildren(isChild);
        });
        return list;
    }

    private boolean isChildren(Entity entity){
        boolean flag = false;
        List<Entity> list = mapper.selectEntityByParentId(entity);
        if (aiasSourceTypeList.size()>0){
            flag = true;
        }
        return flag;
    }
  1. 实体代码(其他实体根据实际情况而定,记得写set/get方法)

/**
*主键
**/
private String id;
/**
*名称
**/
private String name;
/**
*父类id
**/
private String parentId;
/**
*是否有子集
**/
private boolean hasChildren;

前台代码

  1. api.js调用后端接口(参数根据实际情况而定)
// 树形表懒加载接口
export function treeList(query) {
  return request({
    url: '/source/type/treeList',
    method: 'get',
    params:query
  })
}
  1. 前端主要代码
<template>
  <el-row :gutter="20">
    <!--树数据-->
    <el-col :span="4" :xs="24">
      <div class="head-container">
        <el-input
          v-model="typeName"
          placeholder="请输入树节点名称"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
      </div>
      <div class="head-container">
        <el-tree
          :load="loadNode"
          :props="defaultProps"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="id"
          lazy
          @node-click="handleNodeClick"
        />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import {
  treeList
} from '@/api/source/type'
import {mapState} from "vuex";


export default {
  data() {
    return {
      // 类型名称
      typeName: '',
      // 数据中的属性
      typeData:{
        type:'1',
        id: '1',
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        hasChildren:'hasChildren'
      },
    }
  },
  watch: {
    // 根据名称筛选树节点 - 前端筛选
    typeName(val) {
      this.$refs.tree.filter(val)
    }
  },

  methods: {
    /** 查询列表 */
    loadNode(node, resolve){
      console.log(node);
      if (node.level === 0){
        treeList(this.typeData).then(res => {
          resolve(res.data)
        })
      }else {
        debugger
        this.typeData.id=node.data.id
        treeList(this.typeData).then(res => {
          resolve(res.data)
        })
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // 树节点单击事件
    handleNodeClick(data) {
      console.log('被点击的树节点', data)
      this.queryParams.parentId = data.id
      this.getList()
    }
  }
}
</script>

实现效果

树形懒加载图片
Element官网链接

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值