Using AngularJS to create multiple sets of parent-child tabs and dynamically setting a specific tab as

Working with angularjs in this scenario:

I have 2 primary tabs and each of these tabs can contain one or multiple nested tabs.

The code snippet is as follows:

<tabset class="tabbable-line">
    <tab ng-repeat="(key,value) in tabsData" heading="{{key}}">
      <tabset class="tabbable-line">
        <tab class="tabbable-line" ng-if="key=='master1'" ng-repeat="(k2,v2) in value" heading="{{k2}}">
          <!--some data-->
        </tab>
        <tab class="tabbable-line" ng-if="key=='master2'" ng-repeat="(k2,v2) in value" heading="{{k2}}">
          <!--some data-->
        </tab>
      </tabset>
    </tab>
  </tabset>

These tabs are dynamically generated from a JSON object. A sample JSON structure is provided below:

var tabsData = { 
    "master1": {
      "master1-child1": ["some-data1"], 
      "master1-child2": ["some-data2"]
    }, 
    "master2": {
         "master2-child1": ["some-data3"], 
         "master2-child2": ["some-data4"]
    }
 };

Upon clicking a button, I aim to highlight a specific parent tab along with its child tab, but facing difficulties achieving this functionality.

Hence, the parent tabs will always remain as 'master1' and 'master2', while the child tabs within them may vary and their names are not fixed.

I attempted setting the active property to true for the parent tab as shown below, yet it did not yield the desired outcome:

Similarly, I tried implementing this logic in my controller using the following code:

 $scope.tabsData.master2.active = true;

However, I am uncertain about the correct approach to achieve this. Moreover, I am unsure about how to handle making the child tabs active as well.

If anyone has any insights or suggestions, they would be greatly appreciated.

For demonstration purposes, here is the jsfiddle I created:

  https://jsfiddle.net/hubod4a2/2/

Any help is welcome!

Answer №1

I made adjustments to the JSON structure for easier manipulation

$scope.tabsData = [{
    name: "master1",
    tabs: [{
        name: "master1-child1",
        content: "master1-child1-content"
      },
      {
        name: "master1-child2",
        content: "master1-child2-content"
      }
    ]
  }, {
    name: "master2",
    tabs: [{
        name: "master2-child1",
        content: "master2-child1-content"
      },
      {
        name: "master2-child2",
        content: "master2-child2-content"
      }
    ]
  }]

I also updated the tabset in a way that demonstrates how a well-structured JSON object can be easily utilized with ng-repeat.

<div ng-app="tabs" ng-controller="tabController">

  <tabset class="tabbable-line">
    <tab ng-repeat="tab in tabsData" heading="{{tab.name}}">
      <tabset class="tabbable-line">
        <tab class="tabbable-line" ng-repeat="ctab in tab.tabs" heading="{{ctab.name}}">
          {{ctab.content}}
        </tab>
      </tabset>
    </tab>
  </tabset>
  <button type="submit" class="btn btn-primary" ng-click="setData()">Submit</button>
</div>

I trust this explanation makes sense. Cheers!

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

Exploring the varying behaviors of Javascript window.scrollTo across different browsers

I encountered a problem with the scrollTo function when the body has a dir=rtl attribute. Here's a jsfiddle showcasing my issue. HTML: window.scrollTo(-200, 0); <html> <head> </head> <body dir="rtl"> <div width="10 ...

Router-view failing to display component

I am currently working on setting up basic routing in Vue. I have identified three file listings that seem to be causing issues. When I include <router-view> in app.vue, the foo component appears in the browser. However, when I follow the instruction ...

What does a transformed SVG element's getBoundingClientRect() method return?

Today I experimented with the effects of using getBoundingClientRect() on an SVG element that has undergone rotation. Evaluation: The outcome revealed: Chrome, Safari, Opera, and IE seem to calculate the local (untouched) bounding box of the element, ...

What causes jQuery results to resemble a function?

When I create a jQuery wrapped set and insert it into console.log, the output appears as follows: I am aware that we can manipulate the console to display objects or arrays. For example, when we have: var obj = { 0: 'some', 1: 'dat ...

Leveraging ES6 Generators for Efficient XMLHttpRequests

My goal is to simplify AJAX calls using ES6 generators, but I've encountered some issues: let xhr = new XMLHttpRequest() function *statechange() { yield xhr.readyState; } let gen = statechange(); xhr.open("GET", myUrl, true); xhr.onreadystatec ...

remove all clicks from jQuery queue

I am facing an issue with a click event that triggers other click events, resulting in the addition of elements to the DOM. EDIT: However, upon clicking it multiple times, additional elements are added each time. Checking the jQuery queue confirms that an ...

How to style the first dropdown value in AngularJS to appear bold?

Is there a way to style only the first value in a dropdown list as bold without using jQuery? Here is the code for the dropdown: <div class="col-xs-3"> <select-box id="ad-version-select" options="curItem.stats.version" model="state.version" i ...

"Customizing the Material-ui TextField DOM Element: A Step-by-Step

After trying the code below, I was expecting to see a yellowish "Hi," but instead received [object Object]. Is there a way to correct this issue? Possibly utilizing InputProps would help, but I haven't been able to locate a comprehensive tutorial on ...

"Exploring the world of AngularJS through Onsen's API

Having trouble transitioning between pages in your mobile app using Onsen UI? Check out the code snippet below from my app.js file, which aims to share data between pages during the transition. // Wordpress Posts Controller var app = angular.module(' ...

Once the image has been observed, what steps can be taken to close it?

I wish to implement a functionality where clicking on the view button will enlarge the image, and another click on the page will return it to its original size. import * as React from 'react'; import Box from '@mui/material/Box'; imp ...

Creating a sleek horizontal drop-down navigation menu using Bootstrap 5 and CSS

Attempting to design a horizontal dropdown menu for small screens has been a challenge. However, all the resources I've come across seem to be outdated and ineffective. ...

The drop-down button unexpectedly disappears when using Bootstrap in combination with jQuery autocomplete

I have some javascript code that is structured like: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Autocompl ...

AngularUI Select2 ajax: ng-model not being updated with selection changes

After upgrading a solution from Angular 1.2.0rc1 to Angular 1.2.5, there seems to be an issue with the Select2 ajax functionality for a dropdown that we are using in conjunction with AngularUI. Prior to the update, everything was functioning correctly - S ...

Discovering the pixel value of scrolled distance (scroll delta) using jQuery scroll event

Here's the issue I'm facing: $(window).scroll(function(e){ console.log(e); }) I'm trying to figure out how much I've scrolled in pixels. It seems like the scroll value may be influenced by the window size and screen resolution ...

Retrieve the source of the image within the button that you've just clicked

I'm currently working on retrieving the SRC attribute of an image contained within a button that triggers the opening of an accordion built with Bootstrap. However, I am struggling to find a suitable example of what I specifically need. So far, most e ...

Set a point at specific coordinates on a 3D object model using Three.js

Currently working on a Three.js App using the React Template. The main idea is to have a 3D model that represents Planet Earth within the app, along with a space station model. I want to be able to rotate the station around the world by inputting specifi ...

Tips for utilizing jQuery to check for a value within an input field?

Hey there! I'm currently teaching myself jQuery in order to achieve a specific effect. I'll do my best to explain my issue, but please bear with me as English is not my first language. My current goal is to create an input field that will disapp ...

Click handler fails to trigger on dynamically rendered elements through Ajax Callback, limited to Safari browser only

I'm facing an issue with getting a feature to work on Safari and iOS devices, even though it works perfectly on Firefox, Chrome, Edge, and IE. The problem arises with an input field on the page that sends user input to the server and updates the DOM ...

The Meteor update is unsuccessful on the Mongo Sub Collection and will not continue

I am currently facing an issue with updating a specific object within an array in my object based on a value. Whenever I try to add the update code, the function gets stuck at that point without proceeding further. None of the console.log calls after the u ...

What is the best way to reset a looping variable in a Node.js application?

I have encountered an issue with an API method in my nodejs app. Within one of my controllers, I am attempting to generate pages upon clicking. However, I have found that when calling the method, my for loop fails to reset the values to their default state ...