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

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

表結(jié)構(gòu)設(shè)計(jì)
圖里面的菜單其實(shí)是分級(jí)維護(hù)上下級(jí)關(guān)系的。我這里用到了2級(jí),表里有l(wèi)evel字段標(biāo)記。
點(diǎn)擊第1級(jí)加載第2級(jí)分類,點(diǎn)擊第2級(jí)分類名稱則展示面包屑導(dǎo)航。
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ù)實(shí)戰(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">
<!--頁(yè)頭-->
<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 () {
// 獲取一級(jí)菜單
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){
// 一級(jí)菜單
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);
}
}
});
}
// 生成面包屑導(dǎo)航
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" >首頁(yè)</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實(shí)現(xiàn)面包屑導(dǎo)航功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Spring Boot 中application.yml與bootstrap.yml的區(qū)別
- spring boot+thymeleaf+bootstrap實(shí)現(xiàn)后臺(tái)管理系統(tǒng)界面
- bootstrap-table實(shí)現(xiàn)服務(wù)器分頁(yè)的示例 (spring 后臺(tái))
- Spring shiro + bootstrap + jquery.validate 實(shí)現(xiàn)登錄、注冊(cè)功能
- SpringMVC+bootstrap table實(shí)例詳解
- spring MVC + bootstrap實(shí)現(xiàn)文件上傳示例(帶進(jìn)度條)
- 基于SpringMVC+Bootstrap+DataTables實(shí)現(xiàn)表格服務(wù)端分頁(yè)、模糊查詢
- BootStrap學(xué)習(xí)筆記之nav導(dǎo)航欄和面包屑導(dǎo)航
- Bootstrap CSS組件之面包屑導(dǎo)航(breadcrumb)
- Bootstrap組件學(xué)習(xí)之導(dǎo)航、標(biāo)簽、面包屑導(dǎo)航(精品)
相關(guān)文章
jQuery 選擇同時(shí)包含兩個(gè)class的元素的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨Query 選擇同時(shí)包含兩個(gè)class的元素的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
jQuery學(xué)習(xí)筆記之 Ajax操作篇(三) - 過程處理
對(duì)于 jQuery 通過 Ajax 方式傳遞數(shù)據(jù)時(shí),我們還可以在過程中進(jìn)行一定的處理,以便達(dá)到我們的需求。2014-06-06
jQuery樹形插件jquery.simpleTree.js用法分析
這篇文章主要介紹了jQuery樹形插件jquery.simpleTree.js用法,結(jié)合實(shí)例形式分析了jQuery樹形菜單插件jquery.simpleTree.js的功能與基本用法,需要的朋友可以參考下2016-09-09
JQuery 進(jìn)入頁(yè)面默認(rèn)給已賦值的復(fù)選框打鉤
這篇文章主要介紹了JQuery 進(jìn)入頁(yè)面默認(rèn)給已賦值的復(fù)選框打鉤的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-03-03
基于Jquery的開發(fā)個(gè)代陰影的對(duì)話框效果代碼
基于Jquery的開發(fā)個(gè)代陰影的對(duì)話框效果代碼,需要的朋友可以參考下。2011-07-07
jQuery常用事件方法mouseenter+mouseleave+hover
這篇文章主要介紹了jQuery常用事件方法mouseenter、mouseleave和hover方法,下文內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下2022-03-03

