Toggle between tabs by dynamically selecting radio buttons

On my webpage, I have numerous tabs following the same structure as seen on the angular-ui page. Each section contains tabs for both Markup and Javascript.

My goal is to implement two radio buttons at the top of the page that can switch all tabs to either Markup or Javascript based on the selected button.

I am currently utilizing ui.bootrap.tabs but the tabs are static in nature:

<tabset>
    <tab heading="Markup">content for 1st markup tab</tab>
    <tab heading="Javascript">content for 1st javascript tab</tab>
</tabset>
.
.
.
<tabset>
    <tab heading="Markup">content for 2nd markup tab</tab>
    <tab heading="Javascript">content for 2nd javascript tab</tab>
</tabset>

The radio buttons are sourced from the same location:

<div class="btn-group">
    <button type="button" class="btn btn-primary" ng-model="radioModel" btn-radio="'markup'">markup</button>
    <button type="button" class="btn btn-primary" ng-model="radioModel" btn-radio="'javascript'">javascript</button>
</div>

I have been unsuccessful in my attempts so far. Any assistance would be greatly appreciated.

Answer №1

Regrettably, placing an expression in the active attribute of a tab is currently not feasible, preventing something like this:

<tab heading="Markup" active="radioModel === 'markup'">content for 1st markup tab</tab>

For more information, visit https://github.com/angular-ui/bootstrap/issues/611.

However, there is still a way to achieve the desired functionality in a more complex and dynamic manner. My suggestion is to create a data model in your scope to store the tabs configuration and monitor the radioModel value to toggle the active state on each tab. Here's an example:

angular.module('app', ['ui.bootstrap']);
var TabsDemoCtrl = function ($scope) {

    $scope.radioModel = 'markup';
    $scope.tabs = [
        // First tab set
        [{
            title: "Markup",
            content: "content for 1st markup tab",
            type: 'markup'
        }, {
            title: "Javascript",
            content: "content for 1st javascript tab",
            type: 'javascript'
        }],
        //Second tab set
        [{
            title: "Markup ",
            content: "content for 2nd markup tab ",
            type: 'markup'
        }, {
            title: "Javascript ",
            content: "content for 2nd javascript tab",
            type: 'javascript'
        }]
    ];


    $scope.$watch('radioModel', function (newValue) {
        //Iterate over all the tabset configuration
        angular.forEach($scope.tabs, function (value, key) {
            //iterate over each tab in a tabset
            angular.forEach(value, function (tab, key) {
                //Set the active attribute when needed 
                if (tab.type === newValue) {
                    tab.active = true;
                } else {
                    tab.active = false;
                }
            });
        });
    })
};

And here is the corresponding template:

<tabset>
    <tab ng-repeat="tab in tabs[0]" heading="{{tab.title}}" active="tab.active">{{tab.content}}</tab>
</tabset>
<br/>
<br/>
<tabset>
    <tab ng-repeat="tab in tabs[1]" heading="{{tab.title}}" active="tab.active">{{tab.content}}</tab>
</tabset>

A functional fiddle can be found here :)

Answer №2

Utilize a functional expression in the "active" attribute as shown below.

<tabset>
    <tab heading="HTML" active="htmlSelected">content for the HTML tab</tab>
    <tab heading="CSS" active="cssSelected">content for the CSS tab</tab>

Next, define the function in the scope like this:

angular.module('example', ['ui.bootstrap'])
  .controller('myController', function($scope) {
    $scope.tabModel = 'html';
    $scope.$watch('tabModel', function(selectedTab) {
      $scope.htmlSelected = (selectedTab === 'html');
      $scope.cssSelected = (selectedTab === 'css');
    });
  });

Check out the live example on Plunker: Updated: http://plnkr.co/edit/hJIG5a?p=preview

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 is the procedure for closing a snackbar when the close button is clicked?

I am having trouble closing a snackbar when the close button is clicked. The snackbar should initially pop up on page load and only close when manually triggered. I have set the timeout to zero, but the snackbar does not close when the close button is clic ...

Encountering an Angular compile template error in a local project and jsFiddle, yet everything is running smoothly in Plunker

Lately, I've been noticing that my Angular controller is getting larger and I've been trying to simplify it by utilizing directives and services. However, there's a particular scenario where I'm facing an issue. I have created a direct ...

What is the best way to show the previous month along with the year?

I need help with manipulating a date in my code. I have stored the date Nov. 1, 2020 in the variable fiscalYearStart and want to output Oct. 2020. However, when I wrote a function to achieve this, I encountered an error message: ERROR TypeError: fiscalYear ...

Center a span vertically inside a div as the text within the span grows to occupy the entire div

I am facing an issue with a table that has 25 td's. Each td contains specific elements as shown below: <td> <div> <span> text </span> </div> </td> Additionally, there is a script in place that adj ...

Utilizing the power of Angular's $resource within a factory to showcase information stored in a deeply nested

Just starting to explore the world of $resource concepts and facing a challenge with a piece of code that involves fetching data from a nested JSON. I've created a factory method that successfully retrieves the data and I can see it in the response he ...

Removing HTML DOM elements from a string using JavaScript: A step-by-step guide

Hey there, I'm currently working on my angular application. When it comes to inserting the first 100 characters of content into the "description" meta tag for Facebook sharing, I've run into an issue. The description ends up including the HTML el ...

A unit testing tool in Javascript that does not function properly when in strict mode

Is it possible to perform unit testing on code that is not in strict mode? Many popular testing frameworks, such as mocha and jest, require strict mode. However, I need to use eval in my code which necessitates a non-strict mode scope that cannot be easi ...

Error: The function $.ajax(...).done(...).fail(...).complete does not exist

Out of nowhere, I started encountering the error message below: TypeError: $.ajax(...).done(...).fail(...).complete is not a function Below is my code snippet: this.sendRequest = function (type, extension, data, successCallback, successMsg, failMsg, ...

Oops! A problem occurred during JSON parsing: an unexpected non-whitespace character was found after the JSON

I'm struggling with Json parsing. I've looked at various similar issues reported by users, but I can't pinpoint the error in my code. I apologize if this has already been asked! Here is the content of the first file, index.html: Head Secti ...

How to ensure the priority of props.className in CSS-in-JS implementations

It is important for our components to prioritize the class passed as props over the default class. When passing classes as a prop, it is crucial for the component to give precedence to the class defined in its own file. Text.jsx // The following compo ...

Firefox triggers drag events while passing over a div scrollbar

I am looking to create a file drag and drop feature using a styled div: When dragging files over the div, I want the border color to change When dragging files out of the div, I want the border color to revert back to the original In Firefox 35 (Ubuntu) ...

Switch up the box-shadow color with ColorThief!

Is there a way to adjust this script to change the box-shadow color of #player1? <script type="text/javascript> $(window).ready(function(){ var sourceImage = document.getElementById("art"); var colorThief = new ColorThief(); var color = ...

Retrieve information from an SQL database using user input in a Classic ASP form without the need to refresh the page

Currently, I am in the process of incorporating a new function into an existing Classic ASP system. This feature involves allowing users to scan a barcode that will automatically populate a text field within a form inside a bootstrap modal. The scanner has ...

Adding null at the end of a JSON encoded PHP array

During the debugging process, this is the data being sent to my javascript: header("Content-Type: application/json"); //echo json_encode($full_product_list); echo json_encode(array()); Below is my ajax call: jQuery.get( "non public api sorry ") ...

The compiler option 'esnext.array' does not provide support for utilizing the Array.prototype.flat() method

I'm facing an issue with getting my Angular 2 app to compile while using experimental JavaScript array features like the flat() method. To enable these features, I added the esnext.array option in the tsconfig.json file, so the lib section now includ ...

What are some effective troubleshooting methods for resolving issues with chrome.storage.sync?

My Chrome extension is a simple tool that utilizes the chrome.storage API to keep track of tasks in a organized list. Whenever there is an update to the task list, the array is saved to chrome.storage.sync. I have both of my laptops configured with the Ch ...

Automated browsing: identifying the difference between AJAX and iframes

During my automated requests (scheduled at specific times, without user involvement), I have noticed that xmlHttpRequest includes extra http headers. In order for the server not to be able to distinguish these requests as automated (they should appear exa ...

When deploying on Vercel, template literals cannot be used inside the src attribute of an image in NextJS

My issue involves displaying the client's picture after authentication using a userdropdown menu. The process of showing the user's image and name works smoothly with nextAuth when utilizing useSession(). https://i.sstatic.net/kBBa9.png Display ...

Encountering issues with connecting to the MongoDB server through Node.js

When working with MongoDB in Python, everything runs smoothly without any errors. However, when using Node.js, an error keeps popping up. Can someone please guide me on how to resolve this issue? jdcaovuwqxoqppwwqmjcawpwuaciwowjqwqhpaiwdoqi Below is the ...

tsconfig.json: No input files were detected in the configuration file

I am encountering an issue with my tsconfig.ts file where it says "No inputs were found in config file 'd:/self-study/Web/Learning/Express/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude&a ...