0
votes

I would like to use the dashboard template from bootstrap to build a simple page but currently I'm stuck implementing tabs in the navbar with different content panes in the body.

Overall it's working with two panes but when I switch from the first to the second pane and back to the first one again, I see both tab contents displayed. When I switch again it's working correctly (although a list element inside the first tab isn't set to active anymore).

I've created a not properly working jsfiddle, but hunzaboys codepen is working properly and showing my problem.

Where is my error? Why are both tab contents shown when switching back?

EDIT:

Seems like the problem was having another navbar nested inside a tab contend, especially having two "active" classes inside the tab seems to cause the problems (even if the active element isn't a nav item or something else).

I made a new example: https://codepen.io/anon/pen/KXMoRV

If you switch between the top navbar to "Einstellungen" and back to "Überblick" again you will see both content panes active at the same time.

If you reload the page and remove the "active" class from the button in line 45 this doesn't happen.

                    <button class="btn btn-primary active" type="button">Test</button>

Can anyone help me out?

Thanks in advance for any help!

2
If you open the console you have some errors related to bootstrap.js and jquery. You need to add jquery before bootstrap. - Aslam
Aaah I can change the order how the resources, thank you. I updated the fiddle (jsfiddle.net/70gx0nfo/4) but it always says that I need popper.js even if it's already included. - Robert
check my answer - Aslam
Yes in order to use B4, you need to add popper.js. It's a requirement if you want to use the js files. - Aslam
I corrected the order but it didn't load the popper.js correctly. So I know used the created codepen example from hunzaboy. It shows my problem when switching between the tabs. - Robert

2 Answers

1
votes

Solved this by myself because I haven't found a proper answer or solution anywhere.

Instead of using the nav function from bootstrap in the top nav bar and in each of its content panes, I am only using the bootstrap nav now in the content panes and controlling the top nav bar manually now.

I made a codepen which shows the current state and the expected behaviour:

https://codepen.io/anon/pen/KXWvLV

Here is the code to manually control the divs and simulate the nav behaviour:

$("#link-overview").on("click", function () {
    console.log("[INFO] Showing overview");
    $("#pane-overview").show();
    $("#pane-settings").hide();
    }
});

$("#link-settings").on("click", function () {
    console.log("[INFO] Showing settings");
    $("#pane-settings").show();
    $("#pane-overview").hide();
    }
});
0
votes

Your scrips are not included in proper order as per Boostrap Docs. http://getbootstrap.com/docs/4.0/getting-started/download/#bootstrap-cdn

Codepen: https://codepen.io/hunzaboy/pen/zEBPEw

/*
 * Base structure
 */


/* Move down content because we have a fixed navbar that is 3.5rem tall */

body {
  padding-top: 3.5rem;
}


/*
 * Typography
 */

h1 {
  margin-bottom: 20px;
  padding-bottom: 9px;
  border-bottom: 1px solid #eee;
}


/*
 * Sidebar
 */

.sidebar {
  position: fixed;
  top: 51px;
  bottom: 0;
  left: 0;
  z-index: 1000;
  padding: 20px;
  overflow-x: hidden;
  overflow-y: auto;
  /* Scrollable contents if viewport is shorter than content. */
  border-right: 1px solid #eee;
}


/* Sidebar navigation */

.sidebar {
  padding-left: 0;
  padding-right: 0;
}

.sidebar .nav {
  margin-bottom: 20px;
}

.sidebar .nav-item {
  width: 100%;
}

.sidebar .nav-item+.nav-item {
  margin-left: 0;
}

.sidebar .nav-link {
  border-radius: 0;
}


/*
 * Dashboard
 */


/* Placeholders */

.placeholders {
  padding-bottom: 3rem;
}

.placeholder img {
  padding-top: 1.5rem;
  padding-bottom: 1.5rem;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<!-- First jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!--Then Popper.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>

<!-- Then Bootstrap -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>


<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="#">Dashboard</a>
  <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

  <!-- Navbar -->
  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="nav navbar-nav mr-auto">
      <li role="presentation" class="active nav-item">
        <a class="nav-link" data-toggle="tab" aria-controls="tab-overview" href="#tab-overview">Übersicht</a>
      </li>
      <li role="presentation" class="nav-item">
        <a class="nav-link" data-toggle="tab" aria-controls="tab-overview" href="#tab-settings">Einstellungen</a>
      </li>
    </ul>
  </div>
</nav>

<div class="container-fluid">
  <div class="tab-content">
    <div class="tab-pane active" id="tab-overview">
      <div class="row">
        <nav class="col-sm-3 col-md-2 d-none d-sm-block bg-light sidebar">
          <ul class="nav nav-pills flex-column">
            <li class="nav-item">
              <a class="nav-link active" href="#">Data 1</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Data 2</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Data 3</a>
            </li>
          </ul>
        </nav>

        <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
          <h1>Dashboard</h1>

          <section class="row text-center placeholders">
            <div class="col-6 col-sm-3 placeholder">
              <img src="data:image/gif;base64,R0lGODlhAQABAIABAAJ12AAAACwAAAAAAQABAAACAkQBADs=" width="200" height="200" class="img-fluid rounded-circle" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <div class="text-muted">Something else</div>
            </div>
            <div class="col-6 col-sm-3 placeholder">
              <img src="data:image/gif;base64,R0lGODlhAQABAIABAADcgwAAACwAAAAAAQABAAACAkQBADs=" width="200" height="200" class="img-fluid rounded-circle" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
            <div class="col-6 col-sm-3 placeholder">
              <img src="data:image/gif;base64,R0lGODlhAQABAIABAAJ12AAAACwAAAAAAQABAAACAkQBADs=" width="200" height="200" class="img-fluid rounded-circle" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
            <div class="col-6 col-sm-3 placeholder">
              <img src="data:image/gif;base64,R0lGODlhAQABAIABAADcgwAAACwAAAAAAQABAAACAkQBADs=" width="200" height="200" class="img-fluid rounded-circle" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
          </section>
        </main>
      </div>
    </div>

    <div class="tab-pane" id="tab-settings">
      <div class="row">
        <nav class="col-sm-3 col-md-2 d-none d-sm-block bg-light sidebar">
          <ul class="nav nav-pills flex-column">
            <li class="nav-item">
              <a class="nav-link active" href="#">Settings 1</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Settings 2</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Settings 3</a>
            </li>
          </ul>
        </nav>

        <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
          <h1>Einstellungen</h1>

          <section class="row text-center placeholders">
            <div class="col-12 col-sm-12 placeholder">
              <h4>Label</h4>
              <div class="text-muted">Something else</div>
            </div>
          </section>
        </main>
      </div>
    </div>
  </div>
</div>