1. 程式人生 > >基於Bootstrap的後臺管理介面

基於Bootstrap的後臺管理介面

由於專案需求一個後臺介面,所以找了下跟Bootstrap相關的後臺管理介面,最後選擇了SB Admin。但看了SB Admin的程式碼都是寫到一起的,而且專案這邊需要跟伺服器配合所以自己修改了下。特此記錄一下,方便以後使用。

效果

這裡寫圖片描述
效果跟SB Admin的一樣沒什麼變化,只是將選單部分程式碼抽到一個公用的HTML裡面。

目錄結構

這裡寫圖片描述
主要是menu.html、header.js、theme.css。lib下面的根據自己需求版本替換。目錄結構什麼的都可以根據自己的喜好調整。下面貼出主要程式碼。

<%@ page language="java"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- Navigation --> <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0"> <!-- 左上角標題 --> <div class="navbar-header"> <a class="navbar-brand" href="index.html"
>
Admin Template</a> </div> <!-- /.navbar-header --> <ul class="nav navbar-top-links navbar-right"> <li class="dropdown"> <!-- 右上角信封 --> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i
class="fa fa-envelope fa-fw">
</i> <i class="fa fa-caret-down"></i> </a> <ul class="dropdown-menu dropdown-messages"> <li> <a href="#"> <div> <strong>Admin</strong> <span class="pull-right text-muted"> <em>2017-01-12 12:06:12</em> </span> </div> <div>訊息1</div> </a> </li> <li class="divider"></li> <li> <a href="#"> <div> <strong>Administrator</strong> <span class="pull-right text-muted"> <em>2017-01-12 12:07:12</em> </span> </div> <div>訊息2</div> </a> </li> <li class="divider"></li> <li> <a class="text-center" href="#"> <strong>全部</strong> <i class="fa fa-angle-right"></i> </a> </li> </ul> <!-- /.dropdown-messages --> </li> <!-- /.dropdown --> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class="fa fa-bell fa-fw"></i> <i class="fa fa-caret-down"></i> </a> <ul class="dropdown-menu dropdown-alerts"> <li> <a href="#"> <div> <i class="fa fa-comment fa-fw"></i> New Comment <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> </li> <li class="divider"></li> <li> <a href="#"> <div> <i class="fa fa-twitter fa-fw"></i> 3 New Followers <span class="pull-right text-muted small">12 minutes ago</span> </div> </a> </li> <li class="divider"></li> <li> <a href="#"> <div> <i class="fa fa-envelope fa-fw"></i> Message Sent <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> </li> <li class="divider"></li> <li> <a href="#"> <div> <i class="fa fa-tasks fa-fw"></i> New Task <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> </li> <li class="divider"></li> <li> <a href="#"> <div> <i class="fa fa-upload fa-fw"></i> Server Rebooted <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> </li> <li class="divider"></li> <li> <a class="text-center" href="#"> <strong>See All Alerts</strong> <i class="fa fa-angle-right"></i> </a> </li> </ul> <!-- /.dropdown-alerts --> </li> <!-- /.dropdown --> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class="fa fa-user fa-fw"></i> &nbsp;Administrator&nbsp; <i class="fa fa-caret-down"></i> </a> <ul class="dropdown-menu dropdown-user"> <li> <a href="#"><i class="fa fa-user fa-fw"></i>User Profile</a> </li> <li> <a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a> </li> <li class="divider"></li> <li> <a href="login.html"><i class="fa fa-sign-out fa-fw"></i> Logout</a> </li> </ul> <!-- /.dropdown-user --> </li> <!-- /.dropdown --> </ul> <!-- /.navbar-top-links --> <div class="navbar-default sidebar" role="navigation"> <div class="sidebar-nav navbar-collapse"> <ul class="nav" id="side-menu"> <li> <a href="index.jsp" class="action-url active"> <i class="fa fa-fw fa-dashboard"></i>&nbsp;Index </a> </li> <li> <a href="index2.jsp" class="action-url"> <i class="fa fa-fw fa-bar-chart-o"></i>&nbsp;Index2 </a> </li> <li> <a href="index3.jsp" class="action-url"> <i class="fa fa-fw fa-bar-chart-o"></i>&nbsp;Index3 </a> </li> <li> <a href="index4.jsp" class="action-url"> <i class="fa fa-fw fa-bar-chart-o"></i>&nbsp;Index4 </a> </li> <li> <a href="index5.jsp" class="action-url"> <i class="fa fa-fw fa-bar-chart-o"></i>&nbsp;Index5 </a> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#dropdown"> <i class="fa fa-fw fa-arrows-v"></i> Dropdown <i class="fa fa-fw fa-caret-down"></i> </a> <ul id="dropdown" class="nav nav-second-level collapse"> <li> <a href="index6.jsp" class="action-url">Dropdown Item</a> </li> <li> <a href="#" class="action-url">Dropdown Item</a> </li> </ul> </li> </ul> </div> <!-- /.sidebar-collapse --> </div> <!-- /.navbar-static-side --> </nav>

這裡程式碼基本都是從SB Admin抽取出來作為公共部分的,部分內容需要從伺服器取資料的找到對應位置修改即可。

header.js

if (typeof jQuery === "undefined") {
    throw new Error("Js requires jQuery");
};
(function(window, $) {
    /**
     * 處理左邊選單欄的選中問題
     */
    var header = {
        init : function() {
            this.removeActive();
            this.addActive();
        },
        removeActive : function() {
            $('.action-url').removeClass("active");
        },
        addActive : function() {
            $('.action-url').each(function() {
                if ($(this).prop('href') == window.location.href) {
                    $(this).addClass("active");
                }
            });
        }

    };

    $(document).ready(function() {
        header.init();
    });

})(window, jQuery);

由於頁面通過伺服器進行跳轉,所以這個JS用來處理跳轉後頁面的左邊選單的選中問題。

JSP

<!DOCTYPE html>
<html lang="zh_cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Bootstrap Admin Template</title>
<%@ include file="inc/main-css.html"%>
</head>
<body>
    <div id="wrapper" class="container-fluid">
        <%@ include file="inc/menu.html"%>
        <div id="page-wrapper">
            <div class="container-fluid">Index1</div>
            <!-- /.container-fluid -->
        </div>
    </div>
</body>
<%@ include file="inc/main-js.html"%>
</html>

page-wrapper中為主體程式碼。

main-css.html和main-js.html

main-css.html

<!-- Bootstrap Core CSS -->
<link rel="stylesheet" href="../static/lib/bootstrap/css/bootstrap.min.css">
<!-- font-awesome Core CSS -->
<link rel="stylesheet" href="../static/lib/font-awesome/css/font-awesome.min.css">
<!-- custom theme CSS -->
<link rel="stylesheet" href="../static/css/theme.css">

main-js.html

<!-- jQuery -->
<script  type="text/javascript" src="static/lib/jquery/jquery.min.js" /></script>
<!-- Bootstrap Core JavaScript -->
<script  type="text/javascript" src="static/lib/bootstrap/js/bootstrap.min.js"></script>
<!-- Header JavaScript -->
<script  type="text/javascript" src="static/js/header.js"></script>

用來匯入CSS和JS的

theme.css

直接使用的SB Admin的CSS,就不貼出程式碼了。