Loop through different JSON objects with unique values using ng-repeat

I am facing an issue with a JSON file that contains three different objects for each area, and I need some help. Here is an example:

{
    "gebieden":"Antwerpen",
    "onderwerpen":"Gemiddeld netto inkomen per belastingsplichtige",
    "data_2005":"15084,8252887",
    ...
},
{
    "gebieden":"Antwerpen",
    "onderwerpen":"Mediaan netto inkomen",
    "data_2005":"11424",
    ...
},
{
    "gebieden":"Antwerpen",
    "onderwerpen":"Aantal belastingsplichtigen",
    "data_2005":"129568",
    ...
}

The challenge is to display all areas in an accordion group where the property "gebieden" has the same value. I have managed to achieve this layout using AngularJS:

<div class="panel-group" id="accordion">
    <div class="panel panel-default" ng-repeat="item in All | unique:'gebieden'">
        <div class="panel-heading text-center" data-toggle="collapse" data-parent="#accordion" id="{{item.gebieden}}" href="#{{$index}}">
            <h4 class="panel-title">
                {{item.gebieden}}
            </h4>
        </div>
        <div id="{{$index}}" class="panel-collapse collapse ">
            <div class="panel-body text-center">
                <ul class="nav nav-tabs">
                    <li ng-repeat="item in All | unique:'onderwerpen'">
                        <a data-toggle="tab" href="#{{item.onderwerpen}}">
                            {{item.onderwerpen}}
                        </a>
                    </li>
                </ul>
                <div class="tab-content" style="padding:2%">
                    <div id="{{item.onderwerpen}}" class="tab-pane fade in active">
                        <table class="table table-bordered text-center">
                            <thead>
                                <tr>
                                    <th class="text-center">Jaar</th>
                                    <th class="text-center">Waarde</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="(key,val) in item | comparator" ng-hide="$index<2" >
                                    <td>{{key}}</td>
                                    <td>{{val}}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I have implemented a tab layout for the panel body, creating tabs based on the values of "onderwerpen" from the JSON. However, I'm struggling to access the data from the object with a specific "onderwerpen" value as I am using the unique filter to avoid duplicate data. How can I make only the data from the selected tab show?

If my question is unclear, please let me know so I can provide more information. Thank you.

Answer №1

To sort out the object based on the tab that was clicked, you can utilize the filter function. I have provided a functional fiddle below which showcases the properties according to the selected tab - http://jsfiddle.net/uwpjk28w/12/

In essence, I included an ng-click function that alters the onderwerpen property:

<a data-toggle="tab" ng-click="setOnderwerpen(item.onderwerpen)">
     {{item.onderwerpen}}
</a>

The function responsible for modifying the onderwerpen is as follows:

 $scope.selectedOnderwerpen = '';
 $scope.setOnderwerpen = function (onderwerpen) {
        $scope.selectedOnderwerpen = onderwerpen;
 }

Additionally, the filter implementation is detailed below:

<tbody ng-repeat="item in All | filter {onderwerpen:selectedOnderwerpen}:true">
      <tr ng-repeat="(key,value) in item" ng-show="$index < 7">
           <td>{{key}}</td>
           <td>{{value}}</td>
      </tr>
</tbody>

Moreover, by using ng-show, the table will only be displayed once a tab has been selected.

ng-show="selectedOnderwerpen!=''" 

I trust this information proves beneficial.

Answer №2

In my opinion, a more effective way to structure the JSON data would be like this:

{
   "areas":"Antwerp",
   "topics":"Average net income per taxpayer",
   "data":{
      "data_2005":"15084,8252887",
      "data_2006":"14935,2782929",
      "data_2007":"15353,0192747",
      "data_2008":"16040,981705",
      "data_2009":"16050,4881554",
      "data_2010":"15777,0232385",
      "data_2011":"16487,8501985"
   }
},
{
   "areas":"Antwerp",
   "topics":"Median net income",
   "data":{
      "data_2005":"11424",
      "data_2006":"11194",
      "data_2007":"11445",
      "data_2008":"12208",
      "data_2009":"12316",
      "data_2010":"12211",
      "data_2011":"12788"
   }
},
{
   "areas":"Antwerp",
   "topics":"Number of taxpayers",
   "data":{
      "data_2005":"129568",
      "data_2006":"137614",
      "data_2007":"141273",
      "data_2008":"142771",
      "data_2009":"146058",
      "data_2010":"151516",
      "data_2011":"151674"
   }
}

To access the data efficiently, you can utilize the following HTML structure:

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body >
    <div class="panel-group" id="accordion" ng-controller="mainCtrl">
                    <div class="panel panel-default" ng-repeat="item in All">
                        <div class="panel-heading text-center" data-toggle="collapse" data-parent="#accordion" id="{{item.areas}}" href="#{{$index}}">
                            <h4 class="panel-title">
                                {{item.areas}}
                            </h4>
                        </div>
                        <div id="{{$index}}" class="panel-collapse collapse ">
                            <div class="panel-body text-center">
                                <ul class="nav nav-tabs">
                                    <li>
                                        <a data-toggle="tab" href="#{{item.topics}}">
                                            {{item.topics}}
                                        </a>
                                    </li>
                                </ul>
                                <div class="tab-content" style="padding:2%">
                                    <div id="{{item.topics}}" class="tab-pane fade in active">
                                        <table class="table table-bordered text-center">
                                            <thead>
                                                <tr>
                                                    <th class="text-center">Year</th>
                                                    <th class="text-center">Value</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr ng-repeat="(key,val) in item.data" ng-hide="$index<2" >
                                                    <td>{{key}}</td>
                                                    <td>{{val}}</td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

  </body>

</html>

For a live demonstration, visit this plunker link.

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

The custom theme background template color in Oh My Posh is not being recognized

After creating a custom theme for my PowerShell prompt using Oh My Posh, I encountered an issue with the background color not changing as expected when certain conditions are met. Despite correctly identifying the condition as true, the color value #ff0000 ...

Angular error: Attempting to reduce an empty array without an initial value

I am encountering an issue with my array being filtered and reduced. getPageComponents(title: string) { this.pageComponents = []; const pageBlock = this.pageComponents.filter((val) => { if (val.page_title === title) { retur ...

What method can I use to adjust the font style when it overlays an image?

Sorry if this is a bit unclear, but I'm trying to figure out how to change only a section of my font when it overlaps with an image while scrolling. If you visit this example website, you'll see what I'm referring to: For a visual represen ...

AngularJS: How Components Communicate by Interpolating Child Content

Here is an example showcasing intercomponent communication I am looking to achieve a slightly different functionality. Let's say I want the pane content to act as a tab name. For instance <my-tabs> <my-pane>Hello</my-pane> < ...

What is the best method in JavaScript to create three different shades of a color?

My sass function generates lighter and darker versions of a base color, here is the code snippet: $colors: ( betpawa-green: #107A3D, lime-green: #8DC63F, yellow: #FBCD00, ); @mixin color-generator { @each $name, $hex in $colors { &-#{$nam ...

Tips on accessing real-time data from a JSON file

I have been working on this project for a while now and wrote some test scripts. I am using setInterval to update the content every 500 seconds, but I am facing an issue with accessing the input fields. For example, if there is a form with input fields, I ...

Removing all items with a specific ID and its related items in Javascript: How to achieve this recursively?

I am currently facing some challenges in figuring out the most effective approach for this scenario. For example, consider the data structure below: const arr = [{ parentId: 1, children: [ { childId: 11 }, { childId: 21 }, { childId: 31 }, ...

Designing a Curved Stepper Component in the Shape of an S

I've been attempting to create a custom stepper component with an S-curve shape using React, but so far I haven't been successful. I even tried utilizing the MUI Stepper component and experimented with pure HTML and CSS, but couldn't achieve ...

Exploring the RecylcerView and Retrofit2 combo for retrieving data efficiently

I am a beginner in using retrofit2 for Android development. I am currently working on an app that displays earthquake information by fetching data from a URL in JSON format using retrofit and RecyclerView. However, I am facing issues with displaying the fe ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Leveraging Angular-translate with state parameters

My current challenge involves using angular-translate for localization. Everything is working smoothly except for translating data within state parameters. As an example, consider a state configuration like this: .state('about', { url: "/ ...

Using NodeJS to fetch external page data and return Javascript variable information

I need to retrieve the entire DOM of a specific page by sending a request, essentially crawling the website. The HTML directly includes a variable with some data, instead of it being in a separate script file. With my NodeJS backend, I am utilizing request ...

Is it possible to refresh a div on one .aspx page using content from another .aspx page?

Just beginning my journey with asp.net and currently tackling a project that involves two .aspx pages. Events.aspx: This page is where the site admin can update upcoming events and webinars using an available panel to input event title, date, information ...

Learn the process of moving the focus to the next input type using AngularJS

There are four input text boxes for entering credit card numbers. I am looking to automate shifting focus to the next input field. I know how to do this using normal jQuery, but I want to achieve it using Angular. Can anyone offer some guidance? Thank you ...

What's the reason Rails is not recognizing relative JavaScript files?

app/assets/javascripts/application.js: //= require jquery //= require jquery_ujs //= require_tree . //= require bootstrap.min app/assets/javascripts/economy.js: $(document).ready(function() { console.log("loaded file"); }); app/views/economy/index.ht ...

Looking for an API to retrieve random quotes and images from a website?

Greetings, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS along with the basics of JS. Level of Expertise: Beginner During my exploration, I stumbled upon this intriguing website - . It stands out a ...

Problem with implementing swipeable tabs using Material-UI in React

I'm experiencing an issue in my application with the react swipeable tabs from material-ui. I followed all the installation steps recommended in the documentation. Currently, I am encountering the error shown in the screenshot below. Could there be a ...

Generating a collection of objects using arrays of deeply nested objects

I am currently working on generating an array of objects from another array of objects that have nested arrays. The goal is to substitute the nested arrays with the key name followed by a dot. For instance: const data = [ id: 5, name: "Something" ...

Tips on programmatically filtering angular lists

Is there a way to programmatically filter an Angular list? I'm currently working on a project where I need to filter subcategories by clicking on categories. For example, when clicking on "Drinks," I want to display items like Coke, Fanta, Pepsi... ...

Creating a Duplicate of the Higher Lower Challenge Game

To enhance my comprehension of React, I am constructing a replica of the website: Instead of utilizing Google searches, I opted for a dataset containing Premier League stadiums and their capacities. Up to this point, I have crafted the subsequent script: ...