Wednesday, 28 February 2018

MVC - jQuery - Simple tabs example





Watch this example on YouTube


 

1. Install jQuery UI(Combined Library) through NuGet
2. Layout.chtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/Scripts/jquery-1.12.4.min.js")
    @Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

3.  Index.chtml

<div class="container">
    <ul class="tabs">
        <li class="tab-link current" data-tab="tab-1">Tab One</li>
        <li class="tab-link" data-tab="tab-2">Tab Two</li>
        <li class="tab-link" data-tab="tab-3">Tab Three</li>
        <li class="tab-link" data-tab="tab-4">Tab Four</li>
    </ul>

    <div id="tab-1" class="tab-content current">Some Text 1<br /> </div>
    <div id="tab-2" class="tab-content">Some Text 2<br />very important</div>
    <div id="tab-3" class="tab-content">Some Text 3<br />garbage</div>
    <div id="tab-4" class="tab-content">Some Text 4<br />something in the way...</div>
<script>
    $(document).ready(function () {
        $('ul.tabs li').click(function () {
            var tab_id = $(this).attr('data-tab');
            $('ul.tabs li').removeClass('current');
            $('.tab-content').removeClass('current');

            $(this).addClass('current');
            $("#" + tab_id).addClass('current');
        });
        $('ul.tabs li').mouseover(function () {
            var tab_id = $(this).attr('data-tab');
            $('ul.tabs li').removeClass('current');
            $('.tab-content').removeClass('current');

            $(this).addClass('current');
            $("#" + tab_id).addClass('current');
        });
    });
</script>

4. Add to Site.css


ul.tabs{
    margin:0px;
    padding:opx;
    list-style: none;
    background-color:brown;
}
ul.tabs li{
    background: none;
    color:red;
    display:inline-block;
    padding: 10px 15px;
    background-color:green;
    border-top-left-radius:3px;
    border-top-right-radius:3px;
}
ul.tabs li.current{
    background: white;
    color: blue;
    background-color: yellow;
}
.tab-content{
    display: none;
    background: white;
    padding: 15px;
}
.tab-content.current{
    display: inherit;
    border:1px;
    border-color:green;
}


2 comments:

  1. With exact samething but jquery version, which was 3.3.1 it didn't work for me... Would be the version mismatch?
    Twitter: @difiores1

    ReplyDelete