bootstrap+spring boot實(shí)現(xiàn)面包屑導(dǎo)航功能(前端代碼)
面包屑導(dǎo)航介紹
一般的內(nèi)容型網(wǎng)站,例如CMS都會(huì)有這種面包屑導(dǎo)航。總結(jié)起來(lái)它有以下優(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來(lái)做一個(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(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+CSS實(shí)現(xiàn)懸浮登錄框遮罩
這篇文章主要為大家詳細(xì)介紹了jquery+CSS實(shí)現(xiàn)懸浮登錄框遮罩,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03jquery控制背景音樂(lè)開(kāi)關(guān)與自動(dòng)播放提示音的方法
這篇文章主要介紹了jquery控制背景音樂(lè)開(kāi)關(guān)與自動(dòng)播放提示音的方法,實(shí)例分析了背景音樂(lè)開(kāi)關(guān)的技巧與自動(dòng)播放提示音的常見(jiàn)用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02jquery 實(shí)時(shí)監(jiān)聽(tīng)輸入框值變化的完美方法(必看)
下面小編就為大家?guī)?lái)一篇jquery 實(shí)時(shí)監(jiān)聽(tīng)輸入框值變化的完美方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01jQuery Selectors(選擇器)的使用(二、層次篇)
本系列文章主要講述jQuery框架的選擇器(Selectors)使用方法,我將以實(shí)例方式進(jìn)行講述,以簡(jiǎn)單,全面為基礎(chǔ),不會(huì)涉及很深,我的學(xué)習(xí)方法:先入門,后進(jìn)階!2009-12-12jquery實(shí)現(xiàn)搜索框功能實(shí)例詳解
這篇文章主要介紹了jquery實(shí)現(xiàn)搜索框功能,搜索框?qū)崿F(xiàn)搜索一個(gè)ul列表中的指定關(guān)鍵詞的li。具體實(shí)現(xiàn)代碼大家參考下本文2018-07-07jquery簡(jiǎn)單插件制作(fn.extend)完整實(shí)例
這篇文章主要介紹了jquery簡(jiǎn)單插件制作(fn.extend)方法,結(jié)合完整實(shí)例形式分析了jQuery fn.extend擴(kuò)展插件的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-05-05基于Jquery實(shí)現(xiàn)鍵盤按鍵監(jiān)聽(tīng)
本文介紹下,用jquery實(shí)現(xiàn)的滑動(dòng)效果,以及對(duì)鍵盤按鍵進(jìn)行監(jiān)聽(tīng)的例子,有需要的朋友,可以參考學(xué)習(xí)下2014-05-05