How to create an AngularJS Accordion with dynamic is-open attribute using ng-repeat

Even though I can get it to work without using ng-repeat, the issue arises when I have a list of elements and is-Open doesn't function properly.
1. It should only open one panel at a time (sometimes it opens all)
2. The value of is-Open should be true when a panel is opened
3. If a user clicks on the content of a panel after opening it, I should be able to retrieve the value (similar to is-Open for the panel).

HTML

<body ng-app="myApp">
        <div class="col-sm-6" ng-controller="Controller">
            <div class="column-nav">
                <uib-accordion close-others="oneAtATime">
                    <uib-accordion-group  ng-repeat="group in groups" ng-scroll="group in groups" is-open="$parent.isOpen">
                        <uib-accordion-heading ng-model="checkTitle">
                            {{group.title}}
                        </uib-accordion-heading>
                            <a>{{group.content}}</a>
                    </uib-accordion-group>
                </uib-accordion>
            </div>
            <div>
                Panel Header Open: {{isOpen}} <br>
                Panel oneAtATime: {{oneAtATime}}
            </div>
        </div>  
    </body>

App.js

myApp.controller('Controller', ['$scope', function($scope) {

    $scope.isOpen = false;
    $scope.oneAtATime = true;

    // Data 
     $scope.groups = [
    {
      title: "Dynamic Group Header - 1",
      content: "Content - 1"
    },
    {
      title: "Dynamic Group Header - 2",
      content: "Content - 2"
    }
  ];

  //log
  $scope.$watch('isOpen', function(){
        console.log(" watch isOpen:" +$scope.isOpen);
   }, true);

  }]);

Plunker

Thank you for your assistance and feedback.

Answer №1

This response offers a small tweak in comparison to the other solutions provided. (I still have some uncertainty regarding your third condition, or at least what it entails.)

Here is the adjustment made:

...is-open="$parent.isOpen">

Transformed into this (without altering any data):

...is-open="group.isOpen" ng-click="updateOpenStatus()">

In addition, a new function is included in the controller to establish a "universal" open status. (as per requirement 2)

$scope.updateOpenStatus = function(){
  $scope.isOpen = $scope.groups.some(function(item){
    return item.isOpen;
  });
}

http://plnkr.co/edit/xMgmLiL65BBimUJ7wbVE?p=preview

Answer №2

Assign a unique ng-model to each accordion individually in order for it to function correctly

<uib-accordion-group  ng-repeat="group in groups" ng-scroll="group in groups">
    <uib-accordion-heading ng-model="uniqueTitle$index">
        {{group.title}}
    </uib-accordion-heading>
    <a>{{group.content}}</a>
</uib-accordion-group>

Answer №3

Give this a try:

$scope.groups = [
    {
      title: "Dynamic Group Header - 1",
      content: "Content - 1",
      isOpen: false
    },
    {
      title: "Dynamic Group Header - 2",
      content: "Content - 2"
      isOpen: false
    }

Using:

Panel Header Open: {{group.isOpen}} <br>

I believe each group needs to have its own properties.

UPDATE

I tested it on plunker and it seems to work like this:

 $scope.groups = [
    {
      isOpen: false,
      title: "Dynamic Group Header - 1",
      content: "Content - 1"
    },
    {
      isOpen: false,
      title: "Dynamic Group Header - 2",
      content: "Content - 2"
    }

ALSO

<uib-accordion-group  ng-repeat="group in groups" ng-scroll="group in groups" is-open="group.isOpen">

Don't forget to remove $scope.isOpen from the js file

Answer №4

Although this response may be a bit delayed, I have a simple solution that could benefit future visitors.

Take a look at the code snippet below:

<div ng-if="$parent.isOpen" ng-cloak>

Thank you to everyone who has contributed. If you wish to optimize processing by removing the ng-if once the content is rendered, consider using ::.

<div ng-if="::$parent.isOpen" ng-cloak>

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

Submitting HTML forms in SilverStripe using Ajax

I need to transfer data from a basic HTML form to a controller using Ajax, then handle the data and send a response back. Currently, my setup looks like this: HomePage.ss <form method="POST" class="form-horizontal submit-form" onsubmit="return checkf ...

Tips for transmitting one or multiple data using jquery.ajax

I found this code on stackoverflow and it is working well for uploading multiple files. However, I am facing an issue where I cannot send additional parameters along with the file upload. Despite trying various methods, I have been unsuccessful in sending ...

What are some strategies for efficiently displaying a large amount of data on a screen using Javascript and HTML without sacrificing performance?

Imagine having a hefty 520 page book with over 6,200 lines of text ranging from 5 to 300 characters each. The challenge lies in efficiently displaying this content on the screen for users to read without causing lag or performance issues. Attempting to re ...

Updating all the direct components within the corresponding category with jQuery

Here is the HTML content I am working with: <li class="info"> info<li> <li class="other"> info<li> <li class="other"> info<li> <li class="Error"> error<li> <li class="other"> error<li> < ...

Struggling to sort data within ng-repeat using tabs?

Here is a JSON object I am working with: $scope.projects = [ { "_id": "58ab1cb6edf5430e9014e2bc", "name": "Project 1", "issues": [ { "status": 0, "name": "Hart Cummings" }, { "status": 1, ...

What is the best way to integrate a Laravel partial view with AngularJS within a template URL?

I'm trying to display a Laravel Blade view file within an AngularJS directive, like this: var commentsApp = angular.module('CommentApp',[]); commentsApp.directive('commentForm',function(){ return { restrict: 'EA& ...

Comparison of performance between virtual dom and dirty-checking

As a newcomer to the world of React, I am very curious about the performance differences between React's virtual DOM and Angular's dirty checking method. React utilizes a “diffing” algorithm. a. How does this algorithm work? b. Does it m ...

Endless cycle of React hooks

I am struggling to understand why I keep encountering an infinite loop in my useClick function. I can see that I am updating the state value inside the useEffect using setVal, but according to the second parameter, useEffect should only run on onClick as ...

Using JavaScript, enter the value into the input field and then redirect the

I'm encountering a minor issue with my contact form and jQuery redirection based on input fields. Everything was working smoothly until I attempted to integrate the redirection into the form validation code. Redirection snippet: $("#signup").submit ...

Exciting Update: Next.js V13 revalidate not triggering post router.push

Currently using Next.js version 13 for app routing, I've encountered an issue with the revalidate feature not triggering after a router.push call. Within my project, users have the ability to create blog posts on the /blog/create page. Once a post is ...

Is there a more efficient method to achieve the desired effect without making multiple calls to jQuery ajaxSuccess?

Currently, I am working on creating an effect that involves a quick fade-out followed by a fade-in of the element once the request is successful. Since jQuery processes elements in a routine manner (top to bottom), I have managed to achieve my desired eff ...

Issue with floating date and time not functioning properly in ICS file for Yahoo Calendar

I have set up a calendar event in Google, Apple, and Yahoo calendars for each individual customer. The event is scheduled based on the customer's address at a specific time, so there should be no need for timezone conversion. However, I encountered an ...

Instructions on changing the color and font weight of an asterisk within a Textfield using Material UI

Is there a way to style the asterisk in red and bold within a Textfield using material UI? I have a placeholder text without a label, as my label is null or empty. I would like to make the placeholder star RED. Is this possible? Here is the code snippet: h ...

Can JavaScript be adapted to simulate the features of an object-oriented programming language?

Is there a method in Javascript to imitate an object-oriented language? For example, is it possible to replicate the ability to define custom classes/objects with properties? Given that JSON serves as a means for passing objects in JavaScript, I assume the ...

Why won't my redux application store the results of an asynchronous API request using redux-thunk's caching mechanism?

I am new to using Redux in my project. Currently, I am developing an application that displays a list of soccer leagues in each country. The process involves fetching a list of countries first, then using the country names to fetch the soccer leagues. Not ...

Setting a random number as an id in the constructor in Next JS can be achieved by generating a

What steps can be taken to resolve the error message displayed below? Error: The text content does not match the HTML rendered by the server. For more information, visit: https://nextjs.org/docs/messages/react-hydration-error Provided below is the code i ...

Please explain the concept of the Node.js event loop

I've spent countless hours poring over guides and resources on the event loop, yet I still can't grasp its essence. It's common knowledge that the libuv library is responsible for implementing the event loop, but what exactly is this entity ...

Using Express and Node.js to display a page populated with information

On my webpage, I currently have a list of Teams displayed as clickable links. When a link for a specific Team is clicked, the Key associated with that Team is extracted and sent to the /team/:key route to retrieve the respective data. If the data retrieval ...

Using QuerySelector with Angular Directive will throw an error as it is not a recognized

When creating a directive that integrates a jQuery carousel, I want to hide it at the beginning until it is fully ready. Here's the directive code snippet: return { restrict: 'E', replace: true, scope: true, templateUrl: ...

Using JavaScript/jQuery to Set a Timer for Animation with Google Maps Markers

After clicking a marker on a Google map, I've managed to make it bounce with the following code. However, I'm looking for a way to stop the animation after 2 seconds. Is there some kind of timer function that I can incorporate? Here's the ...