【Vue基础知识总结 5】Vue实现树形结构

private String title;

private List children = new ArrayList<>();

}

二级实体类

@Data

public class TwoSubject {

private String id;

private String title;

}

3、controller

@GetMapping(“getAllSubject”)

public R getAllSubject(){

List list = subjectService.getAllOneTwoSubject();

return R.Ok().data(“list”,list);

}

4、service

//课程分类列表(树形)

@Override

public List getAllOneTwoSubject() {

//1 查询所有一级分类

QueryWrapper wrapperOne = new QueryWrapper<>();

wrapperOne.eq(“parent_id”,“0”);

List oneSubjectList = baseMapper.selectList(wrapperOne);

//2 查询所有2级分类

QueryWrapper wrapperTwo = new QueryWrapper<>();

wrapperTwo.ne(“parent_id”,“0”);

List twoSubjectList = baseMapper.selectList(wrapperTwo);

//创建list集合,用于存储最终封装数据

List finalSubjectList = new ArrayList<>();

//3、封装一级分类

for (int i = 0; i < oneSubjectList.size(); i++) {

EduSubject eduSubject = oneSubjectList.get(i);

OneSubject oneSubject = new OneSubject();

BeanUtils.copyProperties(eduSubject,oneSubject);

//在一级分类循环遍历查询所有的二级分类

//创建list集合封装每一个一级分类的二级分类

List twoFinalSubjectList = new ArrayList<>();

for (int j = 0; j < twoSubjectList.size(); j++) {

EduSubject tSubject = twoSubjectList.get(j);

if(tSubject.getParentId().equals(oneSubject.getId())){

TwoSubject twoSubject = new TwoSubject();

BeanUtils.copyProperties(tSubject,twoSubject);

twoFinalSubjectList.add(twoSubject);

}

}

//把一级下面所有的二级分类放入一级分类中

oneSubject.setChildren(twoFinalSubjectList);

finalSubjectList.add(oneSubject);

}

//4、封装二级分类

return finalSubjectList;

}

5、前端

JavaScript

import request from ‘@/utils/request’

export default {

//1 课程分类列表

getSubjectList() {

return request({

url: ‘/eduservice/subject/getAllSubject’,

method: ‘get’

})

}

}

Vue页面

<el-tree

ref=“tree2”

:data=“data2”

:props=“defaultProps”

:filter-node-method=“filterNode”

class=“filter-tree”

default-expand-all

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值