How to keep my selected menu open in my side bar - html

My html look's like this below and I am also using Bootstrap. I would like the menu to stay open on my sidebar, after i have selected it.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="treeview-menu" id="submenu_toggle">
<li>
<a href="tickets.php">
<i class="fa fa-circle-o sub_menu_fa"></i>Inbox
</a>
</li>
<li>
<a href="tickets.php?status=assigned">
<i class="fa fa-circle-o sub_menu_fa"></i>Mine
</a>
</li>
<li>
<a href="tickets.php?status=overdue">
<i class="fa fa-circle-o sub_menu_fa"></i>Overdue
</a>
</li>
<li>
<a href="tickets.php?status=closed">
<i class="fa fa-circle-o sub_menu_fa"></i>Closed
</a>
</li>
</ul>

Since it's not a one page application, consider using this. You are required to load this js & css on every page in menu item.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="treeview-menu" id="submenu_toggle">
<li>
<a href="tickets.php">
<i class="fa fa-circle-o sub_menu_fa"></i>Inbox
</a>
</li>
<li>
<a href="tickets.php?status=assigned">
<i class="fa fa-circle-o sub_menu_fa"></i>Mine
</a>
</li>
<li>
<a href="tickets.php?status=overdue">
<i class="fa fa-circle-o sub_menu_fa"></i>Overdue
</a>
</li>
<li>
<a href="tickets.php?status=closed">
<i class="fa fa-circle-o sub_menu_fa"></i>Closed
</a>
</li>
</ul>
<script>
$( document ).ready(function() {
var url = window.location.href; //get current page url
$(".treeview-menu i").each(function() {
if (url == (this.href)) {
$(this).addClass("active"); //add active class to matched LIst item
}
});
});
</script>
<style>
.active{
border-bottom:1px solid red;
}

<script>
$(function() {
// for bootstrap 3 use 'shown.bs.tab', for bootstrap 2 use 'shown' in the next line
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// save the latest tab; use cookies if you like 'em better:
localStorage.setItem('lastTab', $(this).attr('href'));
});
// go to the latest tab, if it exists:
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('[href="' + lastTab + '"]').tab('show');
}
});
</script>

Related

Redirect to 'href ' and change active class of menu

I'm trying to change the active class of menu when I click on 'a' tag using jquery. I've searched a lot but none of them worked for me. when I click on a tag it redirects to 'href' attribute of the a tag and doesn't change the active menu.when I use 'e.preventDefult' it doesn't go to the 'href' but the active menu changes as I want.
What should I do?
This is the jquery I used:
<script>
$(document).ready(function(e) {
$('.navigation-menu-body ul li a').click(function (e) {
$('.navigation-menu-body ul li a.active').removeClass('active');
$(this).addClass('active');
$('.navigation-menu-body ul.navigation-active').removeClass('navigation-active');
$(this).parent().parent().addClass('navigation-active');
$('.navigation-icon-menu ul li.navigation-active').removeClass('active');
$('.navigation-icon-menu ul li').addClass('active');
window.location.href = $(this).attr('href');
console.log("href:", $(this).attr('href'));
e.preventDefault();
});
})
This is the html:
<div class="navigation">
<div class="navigation-icon-menu">
<ul>
<li data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li data-toggle="tooltip" title="سوابق خرید">
<a href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
</div>
<div class="navigation-menu-body">
<ul id="navigationDashboards" class="navigation-active">
<li class="navigation-divider">داشبورد</li>
<li>
<a class="active" href="/dashboard/index">فروش و مدیریت مشتری</a>
</li>
</ul>
<ul id="festivals">
<li class="navigation-divider">جشنواره ها</li>
<li>
جشنواره های من
</li>
</ul>
<ul id="serialNumbers">
<li class="navigation-divider">کدهای جشنواره</li>
<li>ثبت کد جدید </li>
<li>امتیازات من در هر اپلیکیشن </li>
</ul>
<ul id="invoices">
<li class="navigation-divider">سوابق خرید</li>
<li>خریدهای من </li>
</ul>
</div>
</div>
When clicking on an anchor tag, you goes to a different page, so in order for you to add an active class check the URL against the hrefs on document.ready and then add the class if the argument matches.
$document.ready(function() {
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,''));
$('.navigation-menu-body ul li a').each(function(){
if(urlRegExp.test(this.href)){
$(this).addClass('active');
}
});
});
I don't know much about jQuery but you can do it pretty easy in vanilla js.
First you specify an id for each li you got. then you need to check url so you can add active class for li tag of url you're currently in.for example:
<ul onload="checkUrl()">
<li id="tab1" data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li id="tab2" data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li id="tab3" data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li id="tab4" data-toggle="tooltip" title="سوابق خرید">
<a id="tab4" href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
then the js:
function checkUrl() {
if (document.location.href === "https://example.com/url-of-first-tab") {
document.getElementById("tab1").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-second-tab") {
document.getElementById("tab2").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-third-tab") {
document.getElementById("tab3").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-forth-tab") {
document.getElementById("tab4").classList.add("active");
}
}
Also keep it in mind that none of your a tags shoud have active class in html!

Set div by default active

I have 3 div's and 3 buttons each button on click reveals one div but what im trying to achieve is to set div-nr1 to be by default displayed
this is the angular code that im using...
var app = angular.module('app',[]);
app.controller('Test',function($scope){
$scope.show = 1;
});
this are buttons
<ul>
<li class="has-subnav" ng-click="show = 1">
<a routerLink="/dashboard">
<i class="fa fa-home fa-2x"></i>
</a>
</li>
<li class="has-subnav" ng-click="show = 2">
<a routerLink="/projects">
<i class="fa fa-laptop fa-2x"></i>
</a>
</li>
<li class="has-subnav"ng-click="show = 3" >
<a routerLink="/teams" >
<i class="fa fa-users" aria-hidden="true"></i>
</a>
</li>
</ul>
and this is one of the div's
<div class="information-boxes" ng-show="show==1">
<ul class="box-docs" >
<li>
<div class="docImages">
<img src="../assets/images/lp.png" alt="">
</div>
Couldn't you just init the show variable in the markup like below?
<div ng-controller="MyController" ng-init="show=1">

Apply CSS class to HTML Action Link in Razor

I have the following HTML code:
<li>
<i class="fa fa-dashboard fa-fw"></i> Dashboard
</li>
The Code displays the menu item as the following image:
I want to replace the link with #Html.ActionLink in Razor code, I try this:
<li>
<i class="fa fa-dashboard fa-fw">#Html.ActionLink("Dashboard", "Index", "Home")</i>
</li>
But unfortunately it displays the menu item as:
I also tried:
<li>
#Html.ActionLink("Dashboard", "Index", "Home", new { #class = "fa fa-dashboard fa-fw" })
</li>
The style overrides the text font and displays the result as:
How can I do this correctly?
Try this:
<li>
<a href="#Url.Action("Dashboard", "Home")">
Dashboard <i class="fa fa-dashboard fa-fw"></i>
</a>
</li>

Side bar with Way points

Hey my name is Kasper!
I'm making a side bar where on my portfolio website, I just learned about #Way points some day's ago.
And now I want to add that to my menu bar, But I can't seem to get it right, this is what my code looks like:
<!-- /#sidebar-wrapper ----------------------------------------------------------------------->
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<div class="menu-links scroll-me">
<li class="sidebar-brand">
<div href="#">
<a href="#header">
<img class="ImageFixsTyle" src="assets/img/logsmall.png" alt="SbnLogo" /></a>
</div>
</li>
<hr />
<li><i class="fa fa-archive fa-2x"></i></li>
<hr />
<li><i class="fa fa-globe fa-spin fa-2x"></i></li>
<hr />
<li><i class="fa fa-user-plus fa-2x"></i></li>
<hr />
<li><i class="fa fa-mars-stroke fa-2x"></i></li>
<hr />
<li><i class="fa fa-envelope fa-2x"></i></li>
<hr />
</div>
</ul>
</div>
<!-- /#sidebar-wrapper ----------------------------------------------------------------------->
The Code can be seen in action on : http://kaslabdesign.dk/
now What I want to happen is each time I come to one of my Menu points The menu point we are at needs to be high Lighted.
I have looked at some guides but none of them have anything to do with my specific problem.
I hope anyone can help me to solve my problem since i'm soon bald because of this.
have a nice day/evening & thank you for reading this.
best regards Kasper.
p.s I hope someone can help me ;)! and I hope i explain good enought..
Try using http://imakewebthings.com/waypoints/guides/jquery-zepto/
Handle each block as waypoint and when it is hit, highlight the associated link in the menu. The association is made by the href attribute.
$(function() {
$('p').waypoint({
handler: function(direction) {
$("a").css("color", "");
$("a[href='#" + this.element.id + "']").css("color", "red");
}
})
});
ul {
position:fixed;
top:0;
left:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.0/jquery.waypoints.js"></script>
<ul>
<li>
item1
</li>
<li>
item2
</li>
<li>
item3
</li>
</ul>
<div>
<p id="item1">a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br></p>
<p id="item2">a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br></p>
<p id="item3">a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br></p>
</div>

Bootstrap UI Collapse directive behaving weird

I'm a bit stumped with a problem with the bootstrap.ui module for AngularJS.
This is my HTML:
<nav id="sidebar">
<ul id="main-nav" class="open-active">
<li ng-controller="CollapseCtrl" class="dropdown">
<a ng-click="isCollapsed = !isCollapsed" href="javascript:;">
<i class="fa fa-wrench"></i>
Verktøy
<span class="caret"></span>
</a>
<ul ng-show="isCollapsed" class="sub-nav">
<li>
<a href="#/vernerunde">
<i class="fa fa-th-list"></i>
Ny Vernerunde
</a>
</li>
<li>
<a href="#/interaksjon">
<i class="fa fa-gears"></i>
Ny Interaksjon
</a>
</li>
<li>
<a href="#/brukerstyring">
<i class="fa fa-user"></i>
Brukerstyring
</a>
</li>
</ul>
</li>
<li ng-controller="CollapseCtrl" class="dropdown">
<a ng-click="isCollapsed = !isCollapsed" href="javascript:;">
<i class="fa fa-search"></i>
Lister
<span class="caret"></span>
</a>
<ul ng-show="isCollapsed" class="sub-nav">
<li>
<a href="#/list">
<i class="fa fa-desktop"></i>
Alle
</a>
</li>
</ul>
</li>
</ul>
</nav>
And this is the controller responsible for the isCollapsed model:
controllers.controller('CollapseCtrl', function ($scope) {
$scope.isCollapsed = false;
});
I've also made a small gif which I'm going to throw in that shows my actual problem. Now, I'm not too sure whether or not it's present in the gif, but it seems if I click the first element in the list, it works. Then when that first element has been toggled, and I click the element underneath, it totally breaks apart.
EDIT1:
Okay, I changed the directive from ng-show="" to collapse="", still using "isCollapsed". Now it's working, but the height of the box that's collapsed is too small.
Though I don't have much insight into the problem I strongly suspect that the issue is in here:
<a ng-click="isCollapsed = !isCollapsed" href="javascript:;">
<i class="fa fa-wrench"></i>
Verktøy
<span class="caret"></span>
</a>
Mind that the ng-click is executed once , but once a variable changes, the scope lifecycle will recalculate the scope several times. And once a variable which depends on isCollapsed will change, the scope is re-calculated again, and so forth. This might explain the unusual openings and closings of the title panes. Although I don't see exactly why isCollapse is recalculated.
Solution: Move it as a method inside the controller.