这几天上网找了bootstrap treeview例子 但是看别人写的/贴的代码都是模棱两可思路也说的让人摸不着头脑 只言片语
索性自己写一个 在此整理一下希望可以帮助一些需要使用bootstrap treeview但又不太会处理的同学 也帮助自己更好的记忆
架构用的maven + spring + spring MVC + mybatis
这里不展现架构 只展现部分重要内容
下面展示mybatis的查询语句 很平常的一条语句
注意: 返回的是个hashMap 并且s_id和s_name以及s_parentid三个字段都使用了treeview需要的别名
下面贴出核心方法。。。也就一个方法。
@Autowired
private IMenuDAO menuDAO;
public String getTreeList(){
//取得数据
List> resultMap = menuDAO.getMenuTreeList2();
System.out.println(resultMap);
//定义一个Map集合 存储按指定顺序排列好的菜单
Map>> temp = new HashMap>>();
for (HashMap
@RequestMapping("/getTree.do")
public String getMenuTree(HttpServletRequest request){
String menus = service.getTreeList();
System.out.println(menus);
request.setAttribute("menus", menus);
return "index";
}
前台页面接取值如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Insert title here
这里是拿到的json
[{
"nodes": [{
"nodes": [{
"nodes": [{
"level": "4",
"id": "4",
"text": "天猫超市",
"parentid": "3"
}, {
"level": "4",
"id": "5",
"text": "个人店铺",
"parentid": "3"
}],
"level": "3",
"id": "3",
"text": "淘宝商城",
"parentid": "2"
}],
"level": "2",
"id": "2",
"text": "电商平台",
"parentid": "1"
}, {
"nodes": [{
"level": "3",
"id": "7",
"text": "支付宝",
"parentid": "6"
}],
"level": "2",
"id": "6",
"text": "支付平台",
"parentid": "1"
}, {
"nodes": [{
"level": "3",
"id": "9",
"text": "虾米音乐",
"parentid": "8"
}],
"level": "2",
"id": "8",
"text": "音乐平台",
"parentid": "1"
}],
"level": "1",
"id": "1",
"text": "阿里巴巴",
"parentid": "0"
}, {
"nodes": [{
"nodes": [{
"level": "3",
"id": "12",
"text": "英雄联盟",
"parentid": "11"
}, {
"level": "3",
"id": "13",
"text": "地下城与勇士",
"parentid": "11"
}, {
"level": "3",
"id": "14",
"text": "穿越火线",
"parentid": "11"
}],
"level": "2",
"id": "11",
"text": "游戏平台",
"parentid": "10"
}, {
"nodes": [{
"level": "3",
"id": "16",
"text": "腾讯QQ",
"parentid": "15"
}, {
"nodes": [{
"level": "4",
"id": "18",
"text": "个人微信",
"parentid": "17"
}, {
"level": "4",
"id": "19",
"text": "企业微信",
"parentid": "17"
}],
"level": "3",
"id": "17",
"text": "微信",
"parentid": "15"
}],
"level": "2",
"id": "15",
"text": "聊天平台",
"parentid": "10"
}, {
"nodes": [{
"level": "3",
"id": "21",
"text": "QQ音乐",
"parentid": "20"
}],
"level": "2",
"id": "20",
"text": "音乐平台",
"parentid": "10"
}],
"level": "1",
"id": "10",
"text": "腾讯科技",
"parentid": "0"
}]
下面是treeview的展现结果
最后数据库测试数据脚本:
create table syasuo_menu(
s_id varchar(20) primary key not null,
s_name varchar(60) not null,
s_parentid varchar(20) not null,
s_level varchar(20) not null
)
insert into syasuo_menu values('1','阿里巴巴','0','1');
insert into syasuo_menu values('2','电商平台','1','2');
insert into syasuo_menu values('3','淘宝商城','2','3');
insert into syasuo_menu values('4','天猫超市','3','4');
insert into syasuo_menu values('5','个人店铺','3','4');
insert into syasuo_menu values('6','支付平台','1','2');
insert into syasuo_menu values('7','支付宝','6','3');
insert into syasuo_menu values('8','音乐平台','1','2');
insert into syasuo_menu values('9','虾米音乐','8','3');
insert into syasuo_menu values('10','腾讯科技','0','1');
insert into syasuo_menu values('11','游戏平台','10','2');
insert into syasuo_menu values('12','英雄联盟','11','3');
insert into syasuo_menu values('13','地下城与勇士','11','3');
insert into syasuo_menu values('14','穿越火线','11','3');
insert into syasuo_menu values('15','聊天平台','10','2');
insert into syasuo_menu values('16','腾讯QQ','15','3');
insert into syasuo_menu values('17','微信','15','3');
insert into syasuo_menu values('18','个人微信','17','4');
insert into syasuo_menu values('19','企业微信','17','4');
insert into syasuo_menu values('20','音乐平台','10','2');
insert into syasuo_menu values('21','QQ音乐','20','3');