vue 树状结构数据渲染 (java 处理 list ->树状)

树状结构 Element ui

https://element.eleme.cn/#/zh-CN/component/tree

html 复制代码
<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
html 复制代码
<script>
  export default {
    data() {
      return {
        data: [{
          label: '一级 1',
          children: [{
            label: '二级 1-1',
            children: [{
              label: '三级 1-1-1'
            }]
          }]
        }, {
          label: '一级 2',
          children: [{
            label: '二级 2-1',
            children: [{
              label: '三级 2-1-1'
            }]
          }, {
            label: '二级 2-2',
            children: [{
              label: '三级 2-2-1'
            }]
          }]
        }, {
          label: '一级 3',
          children: [{
            label: '二级 3-1',
            children: [{
              label: '三级 3-1-1'
            }]
          }, {
            label: '二级 3-2',
            children: [{
              label: '三级 3-2-1'
            }]
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      }
    }
  };
</script>

加入关键字进行过滤 和 数据渲染

html 复制代码
  <el-input
                    placeholder="输入关键字进行过滤"
                    v-model="filterText">
            </el-input>
            <el-tree
                    class="filter-tree"
                    :data="data"
                    show-checkbox
                    :props="defaultProps"
                    accordion
                    :filter-node-method="filterNode"
                    ref="tree"
                    @node-click="handleNodeClick">
            </el-tree>
js 复制代码
 watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        created() {
            this.load_data()
        },
        mounted(){
        },//钩子函数,一般在初始化页面完成后,再对dom节点进行相关操作
        methods:{
            load_data(){
                const _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8089/tree/list",
                }).then(resp=>{
                    _this.data = resp.data
                });
            },
            handleNodeClick(data) {
                console.log(data.label);
                this.label = data.label

            },
            filterNode(value, data){
                if (!value) return true;
                return data.label.indexOf(value) !== -1;
            }
        },
        data(){
            return {
                label: "你好",
                filterText: '',
                data:[],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                }
            }
        }
    })

java Calss

java 复制代码
package com.zhan;

import cn.hutool.json.JSONUtil;
import com.zhan.pojo.vo.TreeKnowledgePoints;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class Tree {
    private static List<TreeKnowledgePoints> treeKnowledgePoints;

    @Test
    void Test(){
        TreeKnowledgePoints points = new TreeKnowledgePoints(1,0,"操作系统引论");
        TreeKnowledgePoints points1 = new TreeKnowledgePoints(2,1,"操作系统作用和目标");
        TreeKnowledgePoints points2 = new TreeKnowledgePoints(3,2,"操作系统作用");
        TreeKnowledgePoints points3 = new TreeKnowledgePoints(4,2,"操作系统目标");
        treeKnowledgePoints = new ArrayList<TreeKnowledgePoints>();
        treeKnowledgePoints.add(points);
        treeKnowledgePoints.add(points1);
        treeKnowledgePoints.add(points2);
        treeKnowledgePoints.add(points3);
       List<TreeKnowledgePoints> list = buildTree(treeKnowledgePoints,0);
        System.out.println(JSONUtil.toJsonStr(list));
    }

    /**
     * 递归
     * @param deptList
     * @param pid
     * @return
     */
    private static List<TreeKnowledgePoints> buildTree(List<TreeKnowledgePoints> deptList,int pid){
        List<TreeKnowledgePoints> treeList = new ArrayList<TreeKnowledgePoints>();
        for (TreeKnowledgePoints dept : deptList) {
            if (dept.getParentId() == pid) {
                dept.setChildren(buildTree(deptList, dept.getId()));
                treeList.add(dept);
            }
        }
        return treeList;
    }


}
相关推荐
Ch.yang2 分钟前
【Spring】 Bean 注入 HttpServletRequest 能保证线程安全的原理
java·spring·代理模式
web150850966413 分钟前
基于Mysql、JavaScript、PHP、ajax开发的MBTI性格测试网站(前端+后端)
java
昙鱼11 分钟前
springboot创建web项目
java·前端·spring boot·后端·spring·maven
eternal__day11 分钟前
数据结构(哈希表(中)纯概念版)
java·数据结构·算法·哈希算法·推荐算法
天之涯上上16 分钟前
JAVA开发 在 Spring Boot 中集成 Swagger
java·开发语言·spring boot
天天进步201517 分钟前
Vue项目重构实践:如何构建可维护的企业级应用
前端·vue.js·重构
2402_8575834917 分钟前
“协同过滤技术实战”:网上书城系统的设计与实现
java·开发语言·vue.js·科技·mfc
白宇横流学长18 分钟前
基于SpringBoot的停车场管理系统设计与实现【源码+文档+部署讲解】
java·spring boot·后端
小华同学ai20 分钟前
vue-office:Star 4.2k,款支持多种Office文件预览的Vue组件库,一站式Office文件预览方案,真心不错
前端·javascript·vue.js·开源·github·office
APP 肖提莫21 分钟前
MyBatis-Plus分页拦截器,源码的重构(重构total总数的计算逻辑)
java·前端·算法