Exploring the interaction between Bootstrap and AngularJS in creating unique menu functionality

UPDATE: added JSFiddle link

I am currently working on creating a dynamic menu or set of options that will be populated based on server requests. The data structure I am dealing with is as follows (some unnecessary data has been omitted):


{
    "name" : "root",
    "subOptions" : [{
            "name" : "A",
            "subOptions" : [{
                    "name" : "A1",
                }, {
                    "name" : "B1",
                }, {
                    "name" : "B3",
                }, {
                    "name" : "B4",
                }, {
                    "name" : "B5",
                }
            ]
        }, {
            "name" : "B",
            "subOptions" : []
        }, {
            "name" : "C",
            "subOptions" : [{
                    "name" : "C1",
                }, {
                    "name" : "C2",
                }, {
                    "name" : "C3",
                }
            ]
        }
    ]
}

The challenge I am facing lies in the presentation of suboptions, which should appear below their respective parent element. I am using Bootstrap 3 grid system to define the layout of these elements dynamically based on screen size.

When an element like 'C' is clicked, its suboptions should be displayed directly below it. This behavior should be consistent for all parent elements and their respective suboptions, even when the grid layout changes responsively.

In my current implementation, I am utilizing Angular's ng-repeat directive to iterate over the options:

<div>
    <div class="row">
        <div ng-repeat="option in object.options">
            <div class="container col-lg-4 col-md-4 col-sm-6 col-ms-8 col-xs-12">
                <div ng-click="selectOption(option)">
                    <div class="name"><span>{{option.name}}</span></div>
                </div>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-4 col-md-4 col-sm-4 col-ms-4 col-xs-4" ng-repeat="option in activeOption.suboptions">
                <button class="btn btn-lg" ng-click="selectSuboption(option)">{{ ::option.name }}</button>
            </div>
        </div>
    </div>
</div>

Currently, the suboptions are always displayed at the bottom of the page, irrespective of their parent element. To address this issue, I have two main questions:

  • Is there a way to achieve this dynamic display effect seamlessly?
  • How can I manipulate the container to insert suboptions below their respective parent elements?

Apologies for the lengthy post, but I wanted to provide a comprehensive overview of my problem in hopes of finding a solution. For reference, here is a JSFiddle showcasing my current implementation: https://jsfiddle.net/w5xrjhmd/6/

Answer №1

After quickly working on a solution for your issue without spending much time on styling, here's what I came up with. I hope this helps.

You can view it on CodePen here

<div ng-app="myAppModule" ng-controller="MyController">
  <div class="panel-body">
    <div class="list-group" ng-repeat="s in data">
      <a href="#" class="list-group-item" ng-class="{active: isActive(s)}" ng-click="select(s); showMe = !showMe">{{s.code}}</a>
      <a href="#" ng-show="showMe" ng-repeat="o in s.subOptions" class="btn btn-sm">{{o.code}}</a>
    </div>
  </div>
</div>

This code snippet is specific to AngularJS and does not include your data set or controller structure. I haven't made any changes to those parts.

  $scope.select = function(i) {
    $scope.selected = i;
  };
  $scope.isActive = function(item) {
    return $scope.selected === item;
  };

Here is the HTML with some basic styling implemented. Hopefully, it's accurate.

You can also view it on CodePen here

<div ng-app="myAppModule" ng-controller="MyController">
  <div ng-repeat="s in data" class="container col-lg-4 col-md-4 col-sm-6 col-ms-8 col-xs-12">
    <button class="btn btn-sm" ng-class="{'btn-info': isActive(s)}" ng-click="select(s); showMe = !showMe">{{s.code}}</button>
    <button href="#" ng-show="showMe" ng-repeat="o in s.subOptions" class="btn btn-sm">{{o.code}}</button>
  </div>
</div>

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

Is there a way to lock the eye icon in place within the password field using Vue.js?

I added an eye icon to the right side of my password input fields. However, occasionally error messages may pop up on the page to signify issues with the input fields. When these error messages appear below the relevant input field, the position of the eye ...

Loop through JSON array within an angular controller

I am currently attempting to iterate through a JSON array and display the values on the frontend of my application. I have provided my code, but I'm having trouble retrieving specific values (startDate, endDate) from within the array and displaying th ...

Issue with displaying content in AngularJS view set by a service

I'm currently facing a challenge in accessing the view with an expression that doesn't seem to be working correctly. Here is my Service: angular.module('myApp', []).service('myService', function($timeout){ this.sayHello = ...

Attempting to duplicate Codepen's code onto my local machine

Trying to figure out how to make this work locally after finding it on codepen https://codepen.io/oxla/pen/awmMYY Seems like everything works except for the functionality part. I've included the JS File and the latest Jquery in my code. <head&g ...

What is the functionality of {all: initial} and how does it address issues with default values?

Why do paragraphs inherit properties like line-height and text-align from the div element? If values for these properties are: normal -> for line-height [i.e. font-size = 16px (initial / default value) * normal (value is usually around 1.2 -> 19.2 ...

Is it possible to deactivate an anchor tag based on the result of a conditional statement that returns a string?

I've created an anchor tag (not a button) with text that redirects me to another page <StyledTableCell align="center"> <Link href={`/races/results/${race.id}`}>{race.race_name}</Link> </StyledTableCell> There is a ...

The function given to requestAnimationFrame is not being triggered as expected

I have successfully implemented an infinite loop animation using setInterval. However, I want to enhance the performance by switching to requestAnimationFrame(). Unfortunately, the function supplied to requestAnimationFrame() is not being called for some r ...

What is the best method for fetching the values of a select element in React.js?

I'm struggling to retrieve the value of a selected element in a dropdown list. I've tried debugging it, but haven't been able to get the value. I attempted to console log e.target.value, but unfortunately, it didn't work. Any thoughts o ...

Prevent redirection on buttons within ionic lists

THE ISSUE: I am facing an issue in my Ionic app where I am using the ion-list component. Each item in the list can be swiped to reveal a button (add/remove to favorites). Additionally, each item serves as a link to another page. The problem arises when ...

Linking HTML files across different directories

Here is the current structure of my folders: de/ index.html en/ index.html hu/ index.html img/ image.png style/ stylesheet.css I am wondering how I can link to the css file in the style folder and the image in the img folder directly ...

A web page with a full-width header, content constrained to a set width, logo elements displayed with flexbox, and a footer that

I've been facing challenges in making this website responsive on both desktop and mobile devices. Initially, the header background image wasn't displaying as intended, followed by issues with the flexbox logo elements in the header and the sticky ...

Joomla website experiencing issues with Bootstrap popover functionality

Just wanted to share that I am currently working on this page: I found an example that inspired me from here: I have a feeling that Joomla might be including a resource that is causing conflicts with the popover not showing. This is the code snippet I h ...

Angular: Enhance communication with controllers and directives

Currently, I am in the process of learning Angular. One interesting feature I have been working on is a directive that displays a select element. Whenever there is a change in this select element due to the ng-change event, a specific method defined in the ...

Method in AngularJS Controller Instance

I have a unique angular js controller where I am structuring it for inheritance by other controllers. Rather than defining the function as a single one within the angular's controller function, I am taking a different approach: function SomeCtrl($ ...

Remove the triangle shape from the div and reveal the background

Is it possible to use CSS tricks to cut a triangle out of a div and show the background behind it? I didn't think this was achievable, but I know you pros can surprise me. Here's what I'm aiming for: Please don't suggest a 3-column sol ...

Tips for transforming a string into an object using AngularJS

Here is a string I'm working with: $scope.text = '"{\"firstName\":\"John\",\"age\":454 }"'; I am trying to convert it into a JavaScript object: $scope.tmp = {"firstName":"John","age":454 }; Please note: J ...

Transforming a datetime-local Element into a Date Object

I am currently utilizing the HTML5 element datetime-local with the requirement of having two different formats for the date. One format will be stored as a date object in the database, while the other format will be used as a string to set the datetime-loc ...

Send an array of data using AngularJS multipart form

I am struggling with an issue where I have an array of colors and sizes, along with other data such as product name and images. Despite being able to send image files and product names without any problems, attaching the color and size array leads to a 500 ...

VueJS Error: Unable to access the 'className' property of an undefined variable

I'm currently working on a menu design project where I need to highlight the active tab/page that the user is on by adding a color class and utilizing JavaScript to update the active link. Here's a snippet of my template: <div class="menu ...

Obtaining the referring URL after being redirected from one webpage to another

I have multiple pages redirecting to dev.php using a PHP header. I am curious about the source of the redirection. <?php header(Location: dev.php); ?> I attempted to use <?php print "You entered using a link on ".$_SERVER["HTTP_REFERER"]; ?> ...