基于Bootstrap Ace模板+bootstrap.addtabs.js的菜单

这几天研究了基于bootstrap Ace模板+bootstra.addtabs.js实现菜单的效果

参考了这个人的博客 https://www.cnblogs.com/landeanfen/p/7601880.html

但是由于我没能找到bootstrap.tab.js(我不太确定bootstrap.addtabs.js和bootstrap.tabs.js是不是一个JS)也没太看明白他写的文章,所以我自己在这里研究了下写了一个

(没有他写的好,但是可以做个参照)

这里先发一个简单版本,后期在发布优化版

先看一下效果

基于Bootstrap Ace模板+bootstrap.addtabs.js的菜单基于Bootstrap Ace模板+bootstrap.addtabs.js的菜单

可以看到存在一些问题,标签页附带了三角形 ,当前页没能更新active...问题先记录

先看看要引入的CSS以及JS文件

   <link rel="stylesheet" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/ace-skins.min.css">
<link rel="stylesheet" href="assets/css/ace-rtl.min.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.addtabs.css">
<link rel="stylesheet" href="assets/css/ace.min.css"> <script type="text/javascript" src="assets/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/ace-extra.min.js"></script>
<script type="text/javascript" src="assets/js/ace.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.addtabs.js"></script>

再看看页面布局

 <body class="no-skin">
<div id="navbar" class="navbar navbar-default ace-save-state">
<div class="navbar-container ace-save-state" id="navbar-container"> <button type="button" class="navbar-toggle menu-toggle pull-left" id="menu-toggle"
data-target="#sidebar">
<span class="sr-only">Toggle sidebar</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <div class="navbar-header pull-left">
<a href="index.html" class="navbar-brand">
<small><i class="fa fa-leaf"></i>Ace Admin</small>
</a>
</div> <div class="navbar-buttons navbar-header pull-right">
<ul class="navbar ace-nav">
<li class="light-blue dropdown-modal">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<img src="" alt="Jason's Photo" class="nav-user-photo" />
<span class="user-info">
<small>Welcom,</small>Jason
</span>
<i class="ace-con fa fa-caret-down"></i>
</a> <ul class="user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close">
<li>
<a href="#"><i class="ace-icon fa fa-cog"></i>Settings</a>
</li>
<li><a href="#"><i class="ace-icon fa fa-user"></i>Profile</a></li>
<li class="divider"></li>
<li><a href="#"><i class="ace-icon fa fa-power-off"></i>Logut</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="main-container ace-save-state" id="main-container">
<div id="sidebar" class="sidebar">
<ul id="menu" class="nav nav-list">
</ul><!-- /.nav-list -->
<!-- 菜单伸缩 -->
<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
<i id="sidebar-toggle-icon"
class="ace-icon fa fa-angle-double-left ace-save-state"
data-icon1="ace-icon fa fa-angle-double-left"
data-icon2="ace-icon fa fa-angle-double-right"></i>
</div>
</div>
<!-- content start-->
<div class="main-content">
<div class="page-content">
<div class="row">
<div class="col-xs-12">
<ul class="nav nav-tabs" role="tablist">
<li class="active">
<a href="#index" data-toggle="tab">首页</a>
</li>
</ul>
<div class="tab-content" style="height: 100%;width: 100%">
<div class="tab-pane actvie" id="index">
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content end-->
</div>
</body>

由于菜单是从数据库中动态获取并填充的 所以这里我使用了ajax去请求

  $(function(){
$.ajax({
url:"<%=request.getContextPath()%>/menu/menus.do",
type:'post',
data:{},
success:function(data,status){
console.log(data=data.substring(1,data.length-1));
$("#menu").append(data);
},
error:function(e){
console.log("请求失败"+e);
}
})
})

后台是这么写的

    //返回这颗树的字符串形式
public String getMenuTree(){
List<SyasuoMenu> menus = menuDAO.getMenus();
//定义一个一级菜单集合
List<StringBuffer> result = new ArrayList<StringBuffer>(); StringBuffer sb = new StringBuffer();
for (SyasuoMenu menu : menus) {
if(menu.getSparentid().equals("0")){
sb.append("<li><a href='#' class='dropdown-toggle'><i class='"+menu.getSicon()+"'></i>"+menu.getSname()+"<b class='arrow fa fa-angle-down'></b></a>");
if(isChilds(menus,menu.getSid())){
StringBuffer sbs = new StringBuffer();
sbs = getChilds(menus,menu.getSid(),sbs);
sb.append(sbs);
}
}
}
result.add(sb);
System.out.println(result.toString());
return result.toString();
}
//寻找节点下的子节点
private StringBuffer getChilds(List<SyasuoMenu> menus, String sid,StringBuffer sbs) {
StringBuffer sb = new StringBuffer("<ul class='submenu'>");
for (SyasuoMenu menu : menus) {
if(menu.getSparentid().equals(sid)){
if(isChilds(menus, menu.getSid())){
sb.append("<li><a class='dropdown-toggle' data-addtab='"+menu.getSid()+"' data-url='"+menu.getSdataurl()+"'><i class='menu-icon fa fa-caret-right'></i>"+menu.getSname()+"<b class='arrow fa fa-angle-down'></b></a>");
System.out.println(":"+menu.getSdataurl());
getChilds(menus,menu.getSid(),sb);
}else{
System.out.println("="+menu.getSdataurl());
sb.append("<li><a data-addtab='"+menu.getSid()+"' data-url='"+menu.getSdataurl()+"'><i class='menu-icon fa fa-caret-right'></i>"+menu.getSname()+"</a></li>");
}
}
}
sb.append("</ul></li>");
sbs.append(sb);
return sbs;
}
    //是否存在子节点
private boolean isChilds(List<SyasuoMenu> menus, String sid) {
boolean flag = false;
for (SyasuoMenu menu : menus) {
if(menu.getSparentid().equals(sid)){
flag = true;
break;
}
}
return flag;
}

这里是数据库sql

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
) alter table syasuo_menu add column s_icon varchar(64)
alter table syasuo_menu add column s_dataurl varchar(64);  //这一句是最后加的 是点击菜单子节点触发的路径 insert into syasuo_menu values('','阿里巴巴','','','menu-icon fa fa-list');
insert into syasuo_menu values('','电商平台','','');
insert into syasuo_menu values('','淘宝商城','','');
insert into syasuo_menu values('','天猫超市','','');
insert into syasuo_menu values('','个人店铺','','');
insert into syasuo_menu values('','支付平台','','');
insert into syasuo_menu values('','支付宝','','');
insert into syasuo_menu values('','音乐平台','','');
insert into syasuo_menu values('','虾米音乐','','');
insert into syasuo_menu values('','腾讯科技','','');
insert into syasuo_menu values('','游戏平台','','');
insert into syasuo_menu values('','英雄联盟','','');
insert into syasuo_menu values('','地下城与勇士','','');
insert into syasuo_menu values('','穿越火线','','');
insert into syasuo_menu values('','聊天平台','','');
insert into syasuo_menu values('','腾讯QQ','','');
insert into syasuo_menu values('','微信','','');
insert into syasuo_menu values('','个人微信','','');
insert into syasuo_menu values('','企业微信','','');
insert into syasuo_menu values('','音乐平台','','');
insert into syasuo_menu values('','QQ音乐','','');
上一篇:Centos7安装配置Apache+PHP+Mysql+phpmyadmin


下一篇:基于BootStrap,FortAweSome,Ajax的学生管理系统