如何在React JS中实现多级(三级)导航栏

时间:2018-10-23 05:19:40

标签: reactjs navigationbar react-bootstrap

我正在使用React js并构建一个运行良好的应用程序。我已经使用了react-bootstrap作为导航栏,它的工作原理非常适合两级导航栏。但是,作为我未来发展的一部分,菜单项不断增加,我想实现多级(三级)导航栏。我浏览了很多站点并进行了浏览,但是找不到站点,任何人都可以建议/帮助我。预先感谢。

这是我现在使用的代码:

import React from 'react';
import App from './App.jsx';
import { Link } from 'react-router-dom';

import navStyle from './navbar.css';

class Links extends React.Component {

constructor(props) {
    super(props);    

    this.state = {

    }
}

// on load od component this method will be called.
componentDidMount() {

    this.state.allowedTabs = JSON.parse(sessionStorage.allowedTabs);

    console.log(sessionStorage.allowedTabs.indexOf("Employees"));

    console.log(this.state.allowedTabs);

    console.log(this.state.allowedTabs.indexOf("Employees"));

    console.log("roles", sessionStorage.roleRights);

    console.log("services", sessionStorage.allowedServices);

}

   render() {
      return (
    <div>
        <div className = {navStyle.navbar}>
            <img className = "pull-left" src="cerium-logo-bordered.jpg" alt="Smiley face" style = 

{{"height":"50px","width":"120px","marginRight":"26px","margin": "0px 12px 0px 0px"}}>
                </img>
                <Link to="/home">
                    Home
                </Link>
                {sessionStorage.allowedServices.indexOf("Employee_List") != -1 && 
                    <div className = {navStyle.dropdown}>
                        {sessionStorage.allowedServices.indexOf("Employee_List") != -1 && <button className = {navStyle.dropbtn}>
                            Ceriumites
                            <i className="fa fa-caret-down" style={{"marginLeft":"3px"}}></i>
                        </button>}
                        <div className = {navStyle.dropdown_content}>
                            <Link to="/employee">
                                Ceriumites' List
                            </Link>
                            {(sessionStorage.allowedServices.indexOf("Add_Employee") != -1) && 
                                <Link to="/addNewEmployee">
                                    Add a New Ceriumite
                                </Link>}
                            {(sessionStorage.allowedServices.indexOf("Bulk_Upload") != -1) && 
                                <Link to="/employeeBulkUpload">
                                    Bulk Data Upload
                                </Link>}
                            {(sessionStorage.allowedServices.indexOf("Resignation_Formalities") != -1) && 
                                <Link to="/resignationMainPage">
                                    Leaving Formalities
                                </Link>}
                            {(sessionStorage.allowedServices.indexOf("Exit_Formalities") != -1) && 
                                <Link to="/exitFormalities">
                                    Exit Formalities
                                </Link>}
                        </div>
                    </div>}
                {sessionStorage.allowedServices.indexOf("Project_List") != -1 && 
                    <div className = {navStyle.dropdown}>
                        <button className = {navStyle.dropbtn}>
                            Project
                            <i className="fa fa-caret-down" style={{"marginLeft":"3px"}}></i>
                        </button>
                        <div className = {navStyle.dropdown_content}>
                            {(sessionStorage.allowedServices.indexOf("Project_List") != -1) && <Link to="/project">
                                Project List
                            </Link>}
                            {sessionStorage.allowedServices.indexOf("Add_Project") != -1 && 
                                <Link to="/addNewProject">
                                    Add a New Project
                                </Link>}
                        </div>
                    </div>}
                {sessionStorage.allowedServices.indexOf("Staffing_List") != -1 && 
                    <div className = {navStyle.dropdown}>
                        <button className = {navStyle.dropbtn}>
                            Staffing
                            <i className="fa fa-caret-down" style={{"marginLeft":"3px"}}></i>
                        </button>
                        <div className = {navStyle.dropdown_content}>
                            {sessionStorage.allowedServices.indexOf("Staffing_List") != -1 && <Link to="/staffing">
                                Staffing List
                            </Link>}
                            {sessionStorage.allowedServices.indexOf("Add_Staffing") != -1 && 
                                <Link to="/addNewStaffing">
                                    Staffing into Project
                                </Link>}
                        </div>
                    </div>}
                {(sessionStorage.allowedServices.indexOf("Invoice_List") != -1 || sessionStorage.allowedServices.indexOf("Timesheets") != -1) && 
                    <div className = {navStyle.dropdown}>
                        <button className = {navStyle.dropbtn}>
                            Finance
                            <i className="fa fa-caret-down" style={{"marginLeft":"3px"}}></i>
                        </button>
                        <div className = {navStyle.dropdown_content}>
                            {sessionStorage.allowedServices.indexOf("Invoice_List") != -1 && 
                                <Link to="/invoice">
                                    Invoices
                                </Link>}
                            {sessionStorage.allowedServices.indexOf("Raise_Invoice") != -1 && 
                                <Link to="/raiseInvoice">
                                    Raise Invoice
                                </Link>}
                            {sessionStorage.allowedServices.indexOf("Timesheets") != -1 && 
                                <Link to="/timesheets">
                                    Timesheets
                                </Link>}
                        </div>
                    </div>}


    {(sessionStorage.allowedServices.indexOf("Monthly_Employee_Project_Report") != -1 || (sessionStorage.allowedServices.indexOf("Bench_Report") != -1) || (sessionStorage.allowedServices.indexOf("Staffing_Report") != -1)) && 
                    <div className = {navStyle.dropdown}>
                        <button className = {navStyle.dropbtn}>
                            Reports
                            <i className="fa fa-caret-down" style={{"marginLeft":"3px"}}></i>
                        </button>
                        <div className = {navStyle.dropdown_content}>           
                            {(sessionStorage.allowedServices.indexOf("Monthly_Employee_Project_Report") != -1) && <Link to="/monthlyEmployeeReports">
                                Project Wise Report
                            </Link>} 
                            {(sessionStorage.allowedServices.indexOf("Staffing_Report") != -1) && <Link to="/staffingReport">
                                Staffing Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Duplicate_Staffed_Report") != -1) && <Link to="/duplicateStaffingReport">
                               Duplicate Staffing Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Lead_Wise_Report") != -1) && <Link to="/leadWiseReports">
                                Lead Wise Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Lead_Wise_Report(Jay)") != -1) && <Link to="/jayLeadWiseReport">
                                Lead Wise Report(Jay)
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Customer_Wise_Report") != -1) && <Link to="/customerWiseReports">
                                Customer Wise Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Location_Wise_Report") != -1) && <Link to="/locationWiseReport">
                                Location Wise Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Milestone_Report") != -1) && <Link to="/milestoneReport">
                                Milestone Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Bench_Report") != -1) && <Link to="/benchReport">
                                Bench Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("On_Hold_Report") != -1) && <Link to="/onHoldReport">
                                On Hold Report
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Employee_Reports(Joining, Resigned, exit)") != -1) && <Link to="/employeeReports">
                                Ceriumite Tenure Details
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Unstaffed_Report") != -1) && <Link to="/unStaffedEmployeeReport">
                                Unstaffed Ceriumites
                            </Link>}
                            {(sessionStorage.allowedServices.indexOf("Offboarding_Report") != -1) && <Link to="/offboardingReport">
                                Offboarding Report
                            </Link>}
                        </div>
                    </div>}
                <Link to="/changePassword">
                    Change Password
                </Link>
                <Link to="/">
                    Logout
                </Link>
                <label className ="pull-right" style={{"color":"white", "marginTop":"15px", "marginRight":"2%"}}>
                    Username : {sessionStorage.userName}
                </label>
            </div>      
        </div>
      )
   }
}

export default Links;

Css文件:

.navbar {
    overflow: hidden;
    background-color: #68bcff;
    font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
    float: left;
    font-size: 16px;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

.dropdown {
    float: left;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 16px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn {
    background-color: gray;
}

.dropdown_content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown_content a {
    float: none;
    color: black;
    padding: 6px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown_content a:hover {
    background-color: #ddd;
}

.dropdown:hover .dropdown_content {
    display: block;
}

0 个答案:

没有答案