Posted in Javascript onOctober 09, 2019
面包屑导航介绍
一般的内容型网站,例如CMS都会有这种面包屑导航。总结起来它有以下优势:
让用户了解目前所在的位置,以及当前页面在整个网站中所在的位置;
体现了网站的架构层级;提高了用户体验;
减少返回到上一级页面的操作;
实现效果
那我们应该如何实现?我看网上多数都是只提供静态实现,
这里我结合bootstrap 和 spring boot以及mysql来做一个完整的例子。
表结构设计
图里面的菜单其实是分级维护上下级关系的。我这里用到了2级,表里有level字段标记。
点击第1级加载第2级分类,点击第2级分类名称则展示面包屑导航。
CREATE TABLE `tb_category` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `category_name` varchar(100) NOT NULL, `parent_id` bigint(20) DEFAULT NULL, `level` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; insert into tb_category values(1,'Java文档',0,1); insert into tb_category values(2,'Java多线程',1,2); insert into tb_category values(3,'Spring Boot',1,2); insert into tb_category values(4,'微服务实战',1,2); insert into tb_category values(5,'Java视频',0,1); insert into tb_category values(6,'Java基础',5,2); insert into tb_category values(7,'Java基础',1,2); commit;
前端代码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>响应式布局</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}"> <div class="container-fluid"> <!--页头--> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" th:href="@{'/breadCrumb'}" rel="external nofollow" >Java分享</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav" id="navbar"> </ul> </div> </div> </nav> <!--面包屑--> <ol class="breadcrumb"> </ol> <div class="list-group" id="submenu-list"> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.4.0/jquery.min.js"></script> <script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> var ctx=$("#ctx").val(); $(function () { // 获取一级菜单 getMenu(null,1); }); function getMenu(id, level){ var json = {parentId:id,level:level}; $.ajax({ url: ctx+"/myCategory/list", type: "POST", contentType: "application/json", dataType: "json", data: JSON.stringify(json), success: function (result) { var text=''; if (result.success) { if(result.data != null){ // 一级菜单 if(level!=null){ $.each(result.data, function (i, r) { text += '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" οnclick="getMenu('+r.id+')">'+r.categoryName+'</a></li>' }); $("#navbar").empty(); $("#navbar").append(text); } // 子菜单 if(id!=null){ $.each(result.data, function (i, r) { console.log(i); text += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item" οnclick="getBreadCrumb('+r.id+')">'+r.categoryName+'</a>' }); $("#submenu-list").empty(); $("#submenu-list").append(text); } } } else { alert(result.message); } } }); } // 生成面包屑导航 function getBreadCrumb(id) { var param = {id:id}; $.ajax({ url: ctx+"/myCategory/getParentList", type: "GET", data: {"id":id}, success: function (result) { var text=''; if(result.data!=null){ text = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>'; $.each(result.data, function (i, r) { text += '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+r.categoryName+'</a></li>' }); $(".breadcrumb").empty(); $(".breadcrumb").append(text); } } }) } </script> </body> </html>
总结
以上所述是小编给大家介绍的bootstrap+spring boot实现面包屑导航功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
bootstrap+spring boot实现面包屑导航功能(前端代码)
- Author -
罗汉爷声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@