分类列表展示
# 一、前端实现
# 1、参考 views/tree/index.vue
# 2、创建api
api/edu/subject.js
import request from '@/utils/request'
const api_name = '/admin/edu/subject'
export default {
getNestedTreeList() {
return request({
url: `${api_name}`,
method: 'get'
})
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3、list.vue
<template>
<div class="app-container">
<el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;" />
<el-tree
ref="subjectTree"
:data="subjectList"
:props="defaultProps"
:filter-node-method="filterNode"
class="filter-tree"
default-expand-all
/>
</div>
</template>
<script>
import subject from '@/api/edu/subject'
export default {
data() {
return {
filterText: '',
subjectList: [],
defaultProps: {
children: 'children',
label: 'title'
}
}
},
watch: {
filterText(val) {
this.$refs.subjectTree.filter(val)
}
},
created() {
this.fetchNodeList()
},
methods: {
fetchNodeList() {
subject.getNestedTreeList().then(response => {
if (response.success === true) {
this.subjectList = response.data.items
}
})
},
filterNode(value, data) {
if (!value) return true
return data.title.indexOf(value) !== -1
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 二、后端实现
# 1、创建vo
package com.guli.edu.vo;
@Data
public class SubjectVo {
private String id;
private String title;
}
package com.guli.edu.vo;
@Data
public class SubjectNestedVo {
private String id;
private String title;
private List<SubjectVo> children = new ArrayList<>();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2、创建controller
@ApiOperation(value = "嵌套数据列表")
@GetMapping("")
public R nestedList(){
List<SubjectNestedVo> subjectNestedVoList = subjectService.nestedList();
return R.ok().data("items", subjectNestedVoList);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3、创建service
接口
List<SubjectNestedVo> nestedList();
1
实现v1:获取一级分类列表
@Override
public List<SubjectNestedVo> nestedList() {
//最终要的到的数据列表
ArrayList<SubjectNestedVo> subjectNestedVoArrayList = new ArrayList<>();
//获取一级分类数据记录
QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("parent_id", 0);
queryWrapper.orderByAsc("sort", "id");
List<Subject> subjects = baseMapper.selectList(queryWrapper);
//获取二级分类数据记录
//TODO
//填充一级分类vo数据
int count = subjects.size();
for (int i = 0; i < count; i++) {
Subject subject = subjects.get(i);
//创建一级类别vo对象
SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
BeanUtils.copyProperties(subject, subjectNestedVo);
subjectNestedVoArrayList.add(subjectNestedVo);
//填充二级分类vo数据
//TODO
}
return subjectNestedVoArrayList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
实现v2:完善二级分类记录获取和填充
//获取二级分类数据记录
QueryWrapper<Subject> queryWrapper2 = new QueryWrapper<>();
queryWrapper2.ne("parent_id", 0);
queryWrapper2.orderByAsc("sort", "id");
List<Subject> subSubjects = baseMapper.selectList(queryWrapper2);
//填充二级分类vo数据
ArrayList<SubjectVo> subjectVoArrayList = new ArrayList<>();
int count2 = subSubjects.size();
for (int j = 0; j < count2; j++) {
Subject subSubject = subSubjects.get(j);
if(subject.getId().equals(subSubject.getParentId())){
//创建二级类别vo对象
SubjectVo subjectVo = new SubjectVo();
BeanUtils.copyProperties(subSubject, subjectVo);
subjectVoArrayList.add(subjectVo);
}
}
subjectNestedVo.setChildren(subjectVoArrayList);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
实现Final
@Override
public List<SubjectNestedVo> nestedList() {
//最终要的到的数据列表
ArrayList<SubjectNestedVo> subjectNestedVoArrayList = new ArrayList<>();
//获取一级分类数据记录
QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("parent_id", 0);
queryWrapper.orderByAsc("sort", "id");
List<Subject> subjects = baseMapper.selectList(queryWrapper);
//获取二级分类数据记录
QueryWrapper<Subject> queryWrapper2 = new QueryWrapper<>();
queryWrapper2.ne("parent_id", 0);
queryWrapper2.orderByAsc("sort", "id");
List<Subject> subSubjects = baseMapper.selectList(queryWrapper2);
//填充一级分类vo数据
int count = subjects.size();
for (int i = 0; i < count; i++) {
Subject subject = subjects.get(i);
//创建一级类别vo对象
SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
BeanUtils.copyProperties(subject, subjectNestedVo);
subjectNestedVoArrayList.add(subjectNestedVo);
//填充二级分类vo数据
ArrayList<SubjectVo> subjectVoArrayList = new ArrayList<>();
int count2 = subSubjects.size();
for (int j = 0; j < count2; j++) {
Subject subSubject = subSubjects.get(j);
if(subject.getId().equals(subSubject.getParentId())){
//创建二级类别vo对象
SubjectVo subjectVo = new SubjectVo();
BeanUtils.copyProperties(subSubject, subjectVo);
subjectVoArrayList.add(subjectVo);
}
}
subjectNestedVo.setChildren(subjectVoArrayList);
}
return subjectNestedVoArrayList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 三、优化前端过滤功能
filterNode(value, data) {
if (!value) return true
return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
}
1
2
3
4
2
3
4
编辑 (opens new window)
上次更新: 2024/06/15, 15:12:25