这几天上网找了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<HashMap<Object, Object>> resultMap = menuDAO.getMenuTreeList2();
		System.out.println(resultMap);
		//定义一个Map集合 存储按指定顺序排列好的菜单
		Map<String, List<Map<Object,Object>>> temp = new HashMap<String, List<Map<Object,Object>>>();
		
		for (HashMap<Object,Object> map : resultMap) {
			//如果temp的键包含这个parentid
			if(temp.containsKey(map.get("parentid").toString())){
				//那么把所有相同parentid的数据全部添加到该parentid键下
				temp.get(map.get("parentid").toString()).add(map);
			}else{
				//初始化temp  第一次用
				List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
				list.add(map);
				temp.put(map.get("parentid").toString(), list);
			}
		}
		//定义一个完整菜单列表
		ArrayList<Map<Object,Object>> array = new ArrayList<Map<Object,Object>>();
		
		for (HashMap<Object, Object> hashMap : resultMap) {
			//如果temp中的键与当前id一致
			if(temp.containsKey(hashMap.get("id").toString())){
				//说明temp是当前id菜单的子菜单
				hashMap.put("nodes", temp.get(hashMap.get("id").toString()));
			}
			//遇到顶级菜单就添加进完整菜单列表
			if(hashMap.get("parentid").toString().equals("0")){
				array.add(hashMap);
			}
			
		}
		
		return JSONArray.toJSONString(array);
	}
@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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
		<link rel="stylesheet" href="bootstrap/css/bootstrap-datetimepicker.css">
		<link rel="stylesheet" href="bootstrap/dist/bootstrap-treeview.min.css">
		<script type="text/javascript" src="bootstrap-devoops/jquery/jquery-2.1.0.min.js"></script>
		<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
		<script rel="stylesheet" src="bootstrap/js/bootstrap-datetimepicker.js"></script>
		<script rel="stylesheet" src="bootstrap/js/bootstrap-datetimepicker.zh-CN.js"></script>
		<script rel="stylesheet" src="bootstrap/dist/bootstrap-treeview.js"></script>
<title>Insert title here</title>
<style type="text/css">
	#div{border:1px solid red;width: 200px; height: 3000px;}
</style>
<script type="text/javascript">
	$(function(){
		var init = function(data){
			return $("#div").treeview({
				data:data,
				color:"#428bca",
				levels:2,
				onNodeSelected:function(event,node){
					console.log("选中了");
				},
				onNodeUnselected:function(event,node){
					console.log("取消了");
				}
			});
		}
		init('${menus}')
	})
</script>
</head>
<body>
	<div id="div">
		
	</div>
</body>
</html>

这里是拿到的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');

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐