Why is the content not being displayed even though a nav pill is selected and active?

My goal is to create a single page for multiple user types instead of separate views for ADMIN and USER. I want to display different elements based on the user's privilege level. Here is the code I came up with:

<html lang="en" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<!-- ... -->
<div class="row justify-content-center">
        <div class="col-sm-1 px-2 pt-1">
            <ul class="nav nav-pills flex-column">
                <li class="nav-item" sec:authorize="hasAuthority('ADMIN')">
                    <a th:class="'nav-link' + ${loggedUser.isAdmin() ? ' active' : ''}" data-toggle="pill" href="#admin-tab">Admin</a>
                </li>
                <li class="nav-item">
                    <a th:class="'nav-link' + ${!loggedUser.isAdmin() ? ' active' : ''}" data-toggle="pill" href="#user-tab">User</a>
                </li>
            </ul>
        </div>
        <div class="col-sm-11 p-0 tab-content">
             <div id="admin-tab" class="tab-pane container mw-100 p-0"...>
             <div id="user-tab" class="tab-pane container p-0 mx-auto" th:object="${loggedUser}"...>
        </div>
      </div>

Upon rendering in the browser (while logged in as an ADMIN), the code transforms as follows:

<div class="row justify-content-center">
        <div class="col-sm-1 px-2 pt-1">
            <ul class="nav nav-pills flex-column">
                <li class="nav-item">
                    <a class="nav-link active" data-toggle="tab" href="#admin-tab">Admin</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" data-toggle="tab" href="#user-tab">User</a>
                </li>
            </ul>
        </div>
        <div class="col-sm-11 p-0 tab-content">
            <div id="admin-tab" class="tab-pane container mw-100 p-0">
                <!-- content for the admin tab -->
            </div>
            <div id="user-tab" class="tab-pane container p-0 mx-auto">
                <!-- content for the user tab -->
            </div>

However, there seems to be an issue where the content isn't displayed immediately. Clicking on the User tab before going back to the Admin tab shows the content. This behavior is problematic and needs fixing.

Further investigation shows that even with the Bootstrap 4 tablist role added, the issue persists. Why is the tab content not displaying correctly, and how can this be resolved?

Answer №1

To resolve the issue, simply add the active class to the #active-tab, which will ensure that its content is displayed by default. Problem solved! 😄

<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>Home Page</title>
</head>
<body>
<div class="container mw-100">
<div class="row justify-content-center">
        <div class="col-sm-1 px-2 pt-1">
            <ul class="nav nav-pills flex-column">
                <li class="nav-item">
                    <a class="nav-link active" data-toggle="tab" href="#admin-tab">Admin</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" data-toggle="tab" href="#user-tab">User</a>
                </li>
            </ul>
        </div>
        <div class="col-sm-11 p-0 tab-content">
            <div id="admin-tab" class="tab-pane container mw-100 p-0 active">
                <p class="text-center">Admin tab content</p>
            </div>
            <div id="user-tab" class="tab-pane container p-0 mx-auto">
                <p class="text-center">User tab content</p>
            </div>
         </div>
     </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e140f0b1b0c073e4d5048504d">[email protected]</a>/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3839c83839681dd9980b3c2ddc2c5ddc2">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f7fafae1e6e1e7f4e5d5a1bba3bba7">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps for utilizing AJAX effectively?

I am currently experimenting with AJAX in order to achieve a seamless page update without the need for a reload. My goal is to have the value change dynamically when I switch the select button from 1 to 5, and have the number 5 displayed live. Additionall ...

Tips for limiting a website to load exclusively within an iframe

I am managing two different websites: https://exampleiframe.com (a third-party website), https://example.com (my own website) My goal is to limit the loading of https://example.com so that it only opens within an iframe on https://exampleiframe.com To a ...

Using SCSS based on the browser language in Angular: A Step-by-Step Guide

Is there a way to implement SCSS that is dependent on the user's browser language? When I checked, I found the browser language specified in <html lang = "de"> and in the CSS code as html[Attributes Style] {-webkit-locale: "en&quo ...

Implementing PHP code to prevent the submission of forms with invalid inputs

When working on a form validation process in PHP, I encountered a challenge in preventing the form from being submitted to the database when there are errors in the input fields. While I was able to display error messages for invalid inputs upon submitti ...

How can I make sure addEventListener only responds to numbers and not images?

Currently, I am facing a dilemma with implementing a button that features an image on it and needs to be placed within another div. Despite successfully achieving this, I am struggling to comprehend the JavaScript code outlined in a tutorial I followed. Th ...

The CSS and JS codes are not successfully integrating into the webpage

I am encountering an issue with loading CSS and JS files onto my page. My project involves PHP and Xampp. The file structure is as follows: My Site - CSS - index.css - JS - index.js - Index.php (Apologies for the lack of a folder tre ...

Assess the equation twice using angular measurement

I am attempting to assess an expression that is originating from one component and being passed into another component. Here is the code snippet: Parent.component.ts parentData: any= { name: 'xyz', url: '/test?testid={{element["Te ...

Having trouble with utilizing pako.js in your JavaScript code? The error "Pako is not defined

I'm relatively new to the world of JavaScript. Currently, I'm tackling an algorithm that involves deflating in Java and inflating in JavaScript. While researching solutions, I came across pako.js as a recommended tool for decompression. However, ...

Stop automatic redirection after an AJAX Post request has been successfully completed

I have been trying to figure this out for a while now, but I am stuck and could really use some help. I need to stop my page from redirecting after an AJAX call is successful. Here is the code I have: <!doctype html> <html> <head> < ...

Unable to access Bootstrap dropdown menu after initial ajax form submission

I am encountering an issue with a dropdown menu on my webpage, specifically within the manager.php file. Please excuse any formatting discrepancies as I am using Bootstrap: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna ...

Is offline mode supported in ASP.NET Core MVC for both viewing and submitting data?

We are currently developing a web application that utilizes various technologies including:- ASP.NET Core MVC SQL Server Entity Framework Core Bootstrap IIS Is it possible for our web application to operate in offline mode for specific screens? For insta ...

Revolutionary Approach to Efficiently Handle Multiple rows with Jquery

Greetings to everyone, I am currently in the process of developing an application that retrieves data from a database via AJAX by calling a .php file. Within this app, I have a table with 4 columns. The first two columns consist of dropdown menus, the thi ...

I am currently working on integrating a Netlify form into a contact form using React.js

I attempted various strategies but couldn't achieve any progress. I also consulted this post However, none of the suggestions seemed to work for me. Is there any other way to accomplish this? import React, { Component } from 'react'; impor ...

Python - Using Selenium to update the value in an input field

I've run into an issue while attempting to change the value of an input field in a form using Selenium and Python. Check out the time picker My intention is to use .sendKeys() for each component (Hour, Minutes, Seconds, AM/PM). Here is the code snip ...

AJAX - Retrieving the server response following a page change by the user

My Understanding: I comprehend that when I make an ajax call to my server, a handler is created, the request is sent, my PHP script receives and processes the request, and sends back a response which my JavaScript can parse. Additionally, I am aware that ...

Unleash the power of drag-and-drop functionality with cdkDrop

I am currently tackling a project that requires the implementation of a drop zone functionality where elements can be dragged from a list and dropped in a zone for free movement. I intend to utilize a cdkDropList for the zone due to its comprehensive list ...

The side modal features a full-height design with headers, bodies, and footers that are

I have created a side modal that extends to full height. The content in the modal body is quite lengthy, but I am facing an issue where the header, body, and footer are not equal in size. I am considering using scrollspy on the HTML body tag to potentially ...

how can I apply a class name to an SVG element within styled components

I've been attempting to style an SVG icon component using styled-components, but I'm encountering some issues as the styles I apply to the close icon are not taking effect. import styled from 'styled-components' import Close from ' ...

Adding elements to a JavaScript modal using a loop

When working with JavaScript, I am dynamically adding content to a modal. This involves populating different data into various table rows and cells within the modal. <table class="table table-striped"> <thead> <tr> ...

Encountering issues while integrating jquery library into an asp.net 4.0 application

After creating an application in asp.net4.0, I utilized a master-page within the application. Incorporating a reference to a jQuery JS library file located in the Scripts folder on the master-page presented a challenge when attempting to write jQuery code ...