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

Tips on how to render a component only after receiving an AJAX response

I'm encountering an issue with one of my React components. It seems like AJAX is not fetching all the content from the external server before React renders the ChildComp component. https://i.stack.imgur.com/o0ZtH.png Above, you can view the tree of ...

Leverage the Google Drive API for the storage of app-specific data

I'm currently developing a JavaScript application that runs on the client side and need to store a JSON object containing configuration details in a Google Drive Appdata file. Through the Google Drive API, I can successfully check for the file within ...

Save information in a session using html and javascript

I'm having trouble accessing a session variable in my javascript code. I attempted to retrieve it directly but ran into issues. As an alternative, I tried storing the value in a hidden HTML input element, but I am unsure of how to properly do that wit ...

By harnessing a JSON response

After sending an ajax request, the server's response is as follows: {"error":false,"success":true} The ajax code used: $.ajax({ url: '/update', type: 'post', data: $(this).serialize(), success: function(response) ...

Restoring styles back to the default CSS settings

I am currently working on creating visualizations using D3, and one challenge that I have encountered is the need to define a lot of styles within my code instead of in my CSS where I would prefer them to be. This necessity arises mainly to support transi ...

Strategies for limiting a table row in JavaScript or jQuery without utilizing the style tag or class attribute for that specific row

I am looking for a way to limit the display of records in a table. Currently, I can restrict the table rows using the style property, but this causes UI issues such as missing mouse-over effects for the entire row. I need to ensure that mouse-over functi ...

Maximum number of days that can be selected with Bootstrap Datepicker

I currently have a datepicker set with the multidate option and I am looking to specify a maximum number of days that users can select, say 5 days. Once a user has selected 5 days, any additional days should become disabled dynamically. How can this be a ...

The jqGrid is not showing the AJAX JSON data as expected

Currently, I am exploring jqgrid and trying to figure out how to display data in another jqgrid when clicking on a specific colmodel. The retrieved data from the server goes through a function and reaches the grid, but no information is displayed without a ...

Locate a JQuery element within another JQuery element

Apologies for my poor grasp of English. I am working with HTML and JavaScript. <noindex> <h1>This is h1 in noindex</h1> <div> <h1>This is h1 in noindex and in div</h1> <div> <h1>This is h1 in noindex a ...

Utilizing getter and setter functions within a setter with a type guard

I need to implement a getter and setter in my class. The setter should accept a querySelector, while the getter is expected to return a new type called pageSections. The challenge I'm facing is that both the getter and setter must have the same argum ...

Is there a way to receive a JSON request in Express delete using Restangular?

Below is the Restangular code that I am using for deleting an object: $scope.delO = (id){ Restangular .one("footer", id) .get() .then((ob) => { ob.remove(); } .catch.... } The deletion request is being successfully sent, co ...

Using Angular's ng-switch directive within a select dropdown option

Can we implement the [Data Presentation Format] to be utilized in the [Dropdown Box]? Specifically, I would like the "parent" items to appear as is within the dropdown, while the "child" items should have a [tab] indentation to denote their relationship wi ...

Checking the status of a toggle button in Protractor: How to determine if it is enabled or disabled

Currently, I am utilizing protractor for the automation of a mobile application. My objective is to confirm whether the toggle button (attached image) is checked, disabled, or enabled. However, each time I attempt to verify this, it seems to indicate that ...

What is stopping me from injecting an Angular controller?

After some consideration, I made the decision to alter the way in which I inject my controller. I successfully utilized the "$inject Annotation" method, following the instructions provided here. NetworkInterfaceDetailedViewController.$inject['$scope& ...

How can we identify if the user is utilizing a text-input control?

Incorporating keyboard shortcuts into my HTML + GWT application is a goal of mine, but I am hesitant about triggering actions when users are typing in a text area or utilizing the keyboard for select menu operations. I am curious if there exists a method ...

Trouble with Mongoose adding items to an array

ReviewSchema with Mongoose: import mongoose from "mongoose"; const ReviewSchema = new mongoose.Schema({ likes: { type: Number, required: true, default: 0, }, reactedBy: [ { required: true, ...

Determining the file size of an HTML and JavaScript webpage using JavaScript

Is there a way to determine the amount of bytes downloaded by the browser on an HTML+JS+CSS page with a set size during page load? I am looking for this information in order to display a meaningful progress bar to the user, where the progress advances bas ...

Is there a way to ensure that clicking a link_to only opens a partial once?

These are the files I am working with: _comment.haml %div.comment{ :id => "comment-#{comment.id}" } %hr - if current_user && current_user.id == comment.user_id || current_user && current_user.id == reel_user = link_to " ...

Mastering the art of storing data in AngularJS services

When working with a table and adding items such as products and prices, I am looking to store this data in a service. Could you provide guidance on how to achieve this task effectively? routerApp.controller('products', function ($scope, myservic ...

Implementing a sleek show/hide transition using slideToggle in jQuery

Trying to implement show/hide content with slideToggle and it's functioning, but the animation effect on the table is not smooth. Attempted two different codes, but none provided the desired animation effect: $('.more').slideToggle(' ...