bootstrap+spring boot實現(xiàn)面包屑導航功能(前端代碼)
面包屑導航介紹
一般的內(nèi)容型網(wǎng)站,例如CMS都會有這種面包屑導航??偨Y(jié)起來它有以下優(yōu)勢:

讓用戶了解目前所在的位置,以及當前頁面在整個網(wǎng)站中所在的位置;
體現(xiàn)了網(wǎng)站的架構(gòu)層級;提高了用戶體驗;
減少返回到上一級頁面的操作;
實現(xiàn)效果
那我們應(yīng)該如何實現(xiàn)?我看網(wǎng)上多數(shù)都是只提供靜態(tài)實現(xiàn),
這里我結(jié)合bootstrap 和 spring boot以及mysql來做一個完整的例子。

表結(jié)構(gòu)設(shè)計
圖里面的菜單其實是分級維護上下級關(guān)系的。我這里用到了2級,表里有l(wèi)evel字段標記。
點擊第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,'微服務(wù)實戰(zhàn)',1,2); insert into tb_category values(5,'Java視頻',0,1); insert into tb_category values(6,'Java基礎(chǔ)',5,2); insert into tb_category values(7,'Java基礎(chǔ)',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>響應(yīng)式布局</title>
<link 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>
總結(jié)
以上所述是小編給大家介紹的bootstrap+spring boot實現(xiàn)面包屑導航功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- Spring Boot 中application.yml與bootstrap.yml的區(qū)別
- spring boot+thymeleaf+bootstrap實現(xiàn)后臺管理系統(tǒng)界面
- bootstrap-table實現(xiàn)服務(wù)器分頁的示例 (spring 后臺)
- Spring shiro + bootstrap + jquery.validate 實現(xiàn)登錄、注冊功能
- SpringMVC+bootstrap table實例詳解
- spring MVC + bootstrap實現(xiàn)文件上傳示例(帶進度條)
- 基于SpringMVC+Bootstrap+DataTables實現(xiàn)表格服務(wù)端分頁、模糊查詢
- BootStrap學習筆記之nav導航欄和面包屑導航
- Bootstrap CSS組件之面包屑導航(breadcrumb)
- Bootstrap組件學習之導航、標簽、面包屑導航(精品)
相關(guān)文章
jQuery 選擇同時包含兩個class的元素的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨Query 選擇同時包含兩個class的元素的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
jQuery樹形插件jquery.simpleTree.js用法分析
這篇文章主要介紹了jQuery樹形插件jquery.simpleTree.js用法,結(jié)合實例形式分析了jQuery樹形菜單插件jquery.simpleTree.js的功能與基本用法,需要的朋友可以參考下2016-09-09
jQuery常用事件方法mouseenter+mouseleave+hover
這篇文章主要介紹了jQuery常用事件方法mouseenter、mouseleave和hover方法,下文內(nèi)容介紹詳細,需要的小伙伴可以參考一下2022-03-03

