Guide to creating nested collapsing rows in AngularJS

My attempt to implement expand and collapse functionality in AngularJS for a section is not yielding the desired result.

To demonstrate, I created a simple demo of collapsible/expandable sections in AngularJS which works fine. You can view it here. The example showcases the functionality with a collapsible/expandable first row.

I then applied the same concept to another example where I have a table with different sections such as GENERAL_INFORMATION and USER_INFORMATION that I want to collapse/expand. My code for this can be found here.

<tr ng-repeat="(key, value) in name">
    <uib-accordion>
      <div uib-accordion-group class="panel-default" is-open="status.open">
          <uib-accordion-heading>

            <td ng-if="value.type == 'LABEL'" colspan="2" style="background: #777; color: white;">
                {{key}}
            </td>
          </uib-accordion-heading>
      </div>
   </uib-accordion>

    <td ng-if="value.type == 'FIELD'">{{key}}
        <span ng-if="value.required">
            <sup class="required">*</sup>
        </span>
    </td>
    <td ng-if="value.type == 'FIELD'"
    >
        {{value.value}}
    </td>      
</tr>

Any suggestions on how to improve this implementation?

Answer №1

By utilizing foundational AngularJS techniques, you can achieve the desired effect by combining ng-if with a toggleable show variable.

If you have multiple heading and content pairings, an array of show can be used to accomplish this.

This code snippet showcases the basic approach. However, for a cleaner implementation, consider encapsulating the functionality in a directive similar to the uib-accordion.

angular.module('myApp', ['ngAnimate'])
  .controller('myController', ['$scope', function($scope) {
    $scope.show = [];
    $scope.show[0] = false;
    $scope.show[1] = false;

    $scope.toggle = function(index) {
      $scope.show[index] = !$scope.show[index];
    };
  }]);
table {
  width: 100%;
}

.heading {
  height: 48px;
  background-color: #555;
  color: #fff;
  cursor: pointer;
}

.content {
  background-color: #eee;
  height: 150px;
  transition: all 3s ease-out;
}


/* The initial CSS styles for the enter animation */

.fade.ng-enter {
  transition: 0.5s linear all;
  opacity: 0;
}


/* The final CSS styles for the enter animation */

.fade.ng-enter.ng-enter-active {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular-animate.js"></script>

<div ng-app="myApp">
  <table ng-controller="myController">
    <tr class="heading">
      <td ng-click="toggle(0)">
        Heading
      </td>
    </tr>
    <tr ng-if="show[0]" class="fade">
      <td class="content">
        Content
      </td>
    </tr>
    <tr class="heading">
      <td ng-click="toggle(1)">
        Heading
      </td>
    </tr>
    <tr ng-if="show[1]" class="fade">
      <td class="content">
        Content
      </td>
    </tr>
  </table>
</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

Should $http be called by the Service or the Controller - whose responsibility is it?

Being a backend developer, I find myself struggling a bit with the concept of Angular UI MVC. I am trying to draw connections to my backend MVC so that I can have a better understanding of Angular's mindset. In my backend setup, I have services comm ...

After an AJAX request is completed, the event.keyCode is not returning the key codes for the up and

I have a function that uses AJAX to autocomplete a text field. The results are added to a specific div element. I am trying to implement the functionality where users can navigate through the results using the up and down arrow keys. However, I am encoun ...

Guide to directing a user through an Ajax call to a particular Controller and action

Here is a custom JavaScript function that updates data when the Update button is clicked. function UpdateData() { var obj = { "testData": $("#hdn_EditdsVal").val(), "feature": $("#hdn_EditdsVal").val() }; $.ajax({ url: ...

retrieve essential data from Firebase using JavaScript

Currently, I am utilizing the get() method to retrieve data in my React Native application. Here is the code snippet that I am using: firebase .firestore() .collection("users") .doc("test") .get() .then(response => { console.log(respo ...

Ways to eliminate all jQuery events (or avoid reinitializing multiple times)

As I work on building a web page using AJAX, I have encountered an issue with setting up click events. One particular button on the page allows users to exit and return to their previous location. However, each time the user rebuilds the same div and sets ...

"Responding to an Ajax request with a .NET Core server by sending an xlsx

My web application exclusively supports .xlsx files. I have implemented a function in my controller that converts .xls files to .xlsx format successfully. When trying to open a .xls file, I send it via an Ajax request. However, the converted .xlsx file do ...

In Angular, when a promise returns an "undefined" value, how does this interact with .NET

When I execute this code snippet, I am encountering an issue where the response returned is "undefined" instead of the expected value. Here is the code in question: $scope.SameNameFunction = function() { var payload = { itemname: $scope.EventD ...

Utilizing useEffect Hooks to Filter Local JSON Data in React Applications

For my engineering page, I have been developing a user display that allows data to be filtered by field and expertise. Fields can be filtered through a dropdown menu selection, while expertise can be filtered using an input field. My initial plan was to ...

React Native Header Icon Not Showing on the Left Side

I'm brand new to react and currently working on a project where navigation is done through a hamburger menu. I haven't encountered any errors in my code, but for some reason, the hamburger menu icon isn't displaying as expected. Interestingl ...

Is it possible to create a return using messageEmbed in Discord?

I have been working on creating a Discord bot that responds to the ! status command with the server status. I took inspiration from this existing bot. The only change I made was to ensure that the bot replies immediately to the ! status command without re ...

Is there a way to duplicate items similar to MS Word by using a combination of ctrl + mouse click +

In my fabricjs application, I currently clone an object by clicking ctrl + left mouse click on it, which works fine. However, I would like to be able to clone the object in a similar way to MS WORD, by using ctrl + click + drag. Has anyone achieved this f ...

Ways to display notifications when the user is not actively browsing the website?

How can websites display notifications even when the user is not actively on the site? Take Facebook messenger, for instance. Even with the page closed, notifications still pop up. The same goes for Twitter, which also sends push notifications. I ...

Is there a way to update and save both dependencies and devDependencies with a single command in Npm?

Is there a way to update multiple npm dependencies and save them to their respective locations in the package.json file? This is what my package.json looks like: { "dependencies": { "gulp": "^3.0.0" }, "devDependencies": { "gulp-eslint" ...

Steps for relocating Express server (using MERN stack) to a subdirectory and functioning on Heroku

In my MERN stack project, I recently had to reorganize the server-related files into their own subdirectory due to issues with ESLINT, VSCODE, and package.json configurations that were causing errors. However, after making this change, Heroku started thro ...

Is it possible to use just one <map> tag for multiple images in jQuery or JavaScript?

I have multiple images on my website <img usemap="#slideMap_branches" src="branches.png" width="890" height="270" alt="Slide 4"> <img usemap="#slideMap_order" src="order.png" width="890" height="270" alt="Slide 2"> <img usemap="#slideMap_c ...

``There Seems to be an Issue with Loading Content in Tabs

Hey! I'm encountering an issue with my jquery tabs. The first tab content loads fine, but when I select another tab, the content doesn't display. Then, when I try to go back to the first tab, it doesn't load either. Here's the HTML cod ...

What is the best way to incorporate bullet points into a textarea?

How can I make a bullet point appear when pressing the 'enter' button inside a textarea? I tried using this code, but it doesn't seem to work. Any suggestions? <script> $(".todolist").focus(function() { if(document.getElementById( ...

Modifying website elements with JavaScript

Can someone assist me with getting a script to work on my website that will allow me to switch between four different sets of content using buttons labeled 1, 2, 3, and 4? I have tried using addClass and removeClass but cannot seem to get it right. Here i ...

Click on the ng-click attribute to access the HTML page

I need a button that can perform the following tasks: Execute multiple functions from the controller using ng-click(they must be called in HTML) Direct to another html page located in "static/page.html" onclick, when used for navigation (location.hr ...

Is there a way to automatically remove files from the "dist" directory when deleting the prototype from the "src" folder?

I am new to build systems and seeking assistance with the following question. Here is the structure of my boilerplate: /src (working folder) ___/templates(jade files) ___/scss ___/scripts /dist (compiled files) ___/css ___/js ___.html files In the te ...