0

good day, i'm trying to add an active style to indicate current page on my web app bt couldn't get a solution, i looked at previous post and answers where i saw this which was said to have worked

<ul class="nav">
 <li><a data-target="#" data-toggle="pill" href="#accounts">Accounts</a></li>
 <li><a data-target="#" data-toggle="pill" href="#users">Users</a></li><br/> 
</ul>

when i used the data-toggle attribute in this code

<ul class="nav nav-tabs">
            <li class="colour">@Html.ActionLink("Staffs", "addStaff", "SchlAdmin")</li>
            <li class="colour" data-toggle="tab">@Html.ActionLink("Students", "addStudent", "SchlAdmin")</li>
            <li class="colour">@Html.ActionLink("Incidents", "staffInc", "SchlAdmin")</li>
            <li class="colour" data-toggle="tab">@Html.ActionLink("Admin", "addasset", "SchlAdmin")</li>
            <li class="colour">@Html.ActionLink("Search", "staffSearch", "SchlAdmin")</li>
            <li class="colour" data-toggle="tab">@Html.ActionLink("Profile", "prof", "SchlAdmin")</li>
        </ul>

i couldn't redirect to the page that has the data-toggle attribute but made that page active.

2 Answers 2

2

I have already made the same issue. Please see my Code and I think it helps you, if you have some question I'm ready to answer you.

View:

            <ul class="nav nav-tabs" id="myTab">
                <li id="activeTab" class="active"><a data-toggle="tab" href="#active">Active</a></li>
                <li id="currentTab" class=""><a data-toggle="tab" href="#current">Current</a></li>
                <li id="completedTab" class=""><a data-toggle="tab" href="#completed">Completed</a></li>
            </ul>
            <div class="tab-content" id="myTabContent">
                <div id="active" class="tab-pane fade active in">
                    @Html.Action("Active", "Home")
                </div>
                <div id="current" class="tab-pane fade">
                    @Html.Action("Current", "Home")
                </div>
                <div id="completed" class="tab-pane fade in">
                    @Html.Action("Completed","Home")
                </div>
            </div>

JavaScript - when need to catch Active tab from URL and make it open

 $(document).ready(function() {
        var tab = @ViewBag.tabId;
        switch (tab) {
        case 1:
            removeAllActiveTabs();
            $('#activeTab').addClass('active');
            $('#active').addClass('active');
            $('#active').addClass('in');
            break;
        case 2:
            removeAllActiveTabs();
            $('#currentTab').addClass('active');
            $('#current').addClass('active');
            $('#current').addClass('in');
            break;
        case 3:
            removeAllActiveTabs();
            $('#completedTab').addClass('active');
            $('#completed').addClass('active');
            $('#completed').addClass('in');
            break;
        default:
            $('#activeTab').addClass('active');
            $('#active').addClass('active');
            $('#active').addClass('in');
            break;
        }
    });

    function removeAllActiveTabs() {
        $('#myTab').each(function() {
            $(this).find('li').removeClass('active');
        });
        $('#myTabContent').each(function() {
            $(this).find('div').removeClass('active');
            $(this).find('div').removeClass('in');
        });
    }

JavaScript - If you want to change URL when tab change

   //When Client click on Tabs show in URL
    $('#activeTab').on('click',function() {
        window.history.pushState('','','/Home/SomeAction?tabId=1');
    });
    $('#currentTab').on('click',function() {
        window.history.pushState('','','/Home/SomeAction?tabId=2');
    });
    $('#completedTab').on('click',function() {
        window.history.pushState('','','/Home/SomeAction?tabId=3');
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add a classname active to your li. Example:

<li class="active"><a data-target="#" data-toggle="pill" href="#users">Users</a></li>

1 Comment

adding a class name active to a particular li element only makes that class active, as i switch tabs, that class still remain active

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.