Organize a list in AngularJS by breaking it down based on its headers and displaying them in

As someone who is new to angularJs, I am looking to convert an app to angularJs but I have encountered a roadblock:

The custom accordion markup I am working with is as follows:

<div class="accord_main_wrap" ng-controller="catController">
    <div class="accord_item_wrap" ng-repeat="head in heads">
        <p class="accord_head" data-notvisible="true">{{head.text}}</p>
        <div class="accord_content_wrap">
            <ul>
                <li ng-repeat="sub in subs">{{sub.text}}</li> 
            </ul>
        </div>
    </div>
</div>

In terms of the controller :

function catController($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $scope.heads = [];
    $scope.subs = [];
    $http.post(apiURL,$.param({maincat:"true"}))
    .success(function(mainCatData,status,headers,config){
        for(var d=0;d<mainCatData.cat.length;d++){
            $scope.heads.push(mainCatData.cat[d]);
        }
        for(var h=0;h<$scope.heads.length;h++){
            $http.post(apiURL,$.param({subcat:$scope.heads[h].id}))
                .success(function(subCatdata,stat,hea,conf){
                        for(var s=0;s<subCatdata.cat.length;s++){
                            $scope.subs.push(subCatdata.cat[s]);
                        }
                });
        }
    });

The Issue at Hand :

It's clear that each accordion header should display its own set of subheadings. However, in the current code, $scope.subs mixes and assigns all the subheadings to every accordion.

How can I ensure that the correct subheadings are assigned to their respective main headings?

Answer №1

Every entry within the $scope.heads array should include a nested collection called subs...

function categoryController($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $scope.heads = [];
    $http.post(apiURL,$.param({maincat:"true"}))
    .success(function (mainCatData) {
        for (var d = 0; d < mainCatData.cat.length; d++) {
            var head = mainCatData.cat[d];
            $scope.heads.push(head);
            retrieveSubCategories(head);
        }
    });

    function retrieveSubCategories(head) {
        head.subs = [];
        $http.post(apiURL, $.param({subcat: head.id}))
            .success(function (subCatdata) {
                    for (var s = 0; s < subCatdata.cat.length; s++) {
                        head.subs.push(subCatdata.cat[s]);
                    }
            });
    }
}

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

Using JS/AJAX to update a section of a webpage when a button is clicked

Currently, I am working on developing a generator that will display a random line from a .txt file. Everything is going smoothly so far, but I have encountered an issue - I need a specific part of the page to refresh and display a new random line when a ...

What is the best way to ensure that the Next.js app listener is attached before the 'load' event of the window is triggered?

Recently, I started working with next.js, incorporating TypeScript in version 13.5.5. One crucial requirement for the application is that it must be placed within an iframe and communicate with the parent/host through the window.postMessage event. To achie ...

jQuery problem causes page to scroll when button is clicked

Is there a way to smoothly scroll to the second section of the page when a button is clicked using jQuery? $('.home_scroll_btn a').on('click', function(){ var scrollPosition = $('#intro-home').offset().top; $( ...

Having trouble injecting services into Angular unit tests, resulting in test failures

This is my custom karma configuration file: `use strict`; var path = require('path'); var conf = require('./gulp/conf'); var _ = require('lodash'); var wiredep = require('wiredep'); var pathSrcHtml = [ path.joi ...

attempting to link to an external style sheet hosted on Amazon S3

I am working on creating a widget or snippet of a website that can easily be added to another webpage by including a script tag for my JavaScript file hosted on Amazon S3 and a div element where content will be inserted. Even though I have uploaded the C ...

Is it possible to change the style of an element when I hover over one of its children?

Encountered an issue while working with HTML and CSS: //HTML <div> <div class="sibling-hover">hover over me</div> </div> <div class="parent"> <div>should disappear</div> </div> ...

Achieve text length that does not exceed the specified limit dynamically

Is it possible to determine the length of the visible portion of text that overflows (or calculate the size of the overflow for further processing) using CSS or JavaScript? If so, can this calculation be done dynamically (such as on window resize)? The g ...

Delving into XFN data parsing using Jquery, part two

I need some assistance with correctly parsing the REL attribute on A tags. I have already figured out how to match most XFN values, but there are two that I'm struggling with: "co-worker" and "co-resident". The hyphen seems to be causing an issue with ...

Replicating a row in a table without disrupting a button within the row

As a novice in the world of coding, I am currently embarking on a project to revamp an existing website. The task at hand involves creating a table with a built-in save/edit feature, which I have successfully implemented. However, I am facing a roadblock w ...

Is it necessary to run npm install when a package does not have any dependencies?

If I have an npm package that contains all its dependencies bundled into one file using webpack, and I unpack it into the directory ./my-awesome-package/, should I still run npm install ./my-awesome-package/? I am aware of being able to specify preinstall ...

What is the best way to change the date format from "yyyy-MM-dd" in a JavaScript file to "dd/MM/yyyy" and "MM/dd/yyyy" formats in an HTML file?

Is there a way to transform the date string "2020-08-02" from a JavaScript file into the formats "08/02/2020" and "02/08/2020" in an html file, without relying on the new Date() function in JavaScript? I would greatly appreciate any assistance with this t ...

AngularJS - Issue with retrieving the most recent entry during $routeChangeStart event

I am utilizing the $routeChangeStart function to redirect authorized users to specific URLs and prevent unauthorized access to special pages. In addition, I have dynamically generated pages that can be accessed via their unique page slugs. To achieve this ...

Leveraging JSON Data for Avatars in React.js

Struggling to arrange 10 Avatars side by side for showcasing the user list. However, encountering an issue where the Avatars are being duplicated and showing incorrect initials when passing JSON data. Experimented with this in Code Sandbox linked below. h ...

What is the best way to utilize AJAX for uploading .mp4, .mp3, and .doc files together with additional FormData?

Hello everyone, I have been successfully uploading files (documents and images) using AJAX. However, I am facing some challenges when it comes to uploading videos through AJAX. I have searched extensively on this site for solutions but have not found exact ...

Exploring the capabilities of Vue JS to dynamically update data and model bindings within form

I need help creating a form that allows users to edit their comments. The challenge is to display the current value of the comment using dynamic data from v-for, while also binding the value to a model for sending it with a patch request. Here is an examp ...

Although AJAX $.post functions properly in the View, it seems to encounter issues when relocated to a separate .js file. Interestingly, all other JQuery functions work

I have recently delved into MVC, JQuery, and AJAX, and encountered a perplexing issue. After completing the initial development of a practice website, I dedicated time to enhance the interactivity using JQuery. Everything was functioning smoothly until I ...

Tips for showing a single table with buttons on display

I am using R markdown and have included multiple tables with buttons, but when I open the file, all tables are displayed at once. How can I ensure only one table is shown upon opening the file? https://i.sstatic.net/NFidf.png <script type="text/ja ...

Sending JavaScript/jQuery variables to a different PHP page and navigating to it

For a while now, I've been working on: Executing a .js file on the client side (successful). Passing the returned object (the variables returned) as an array to another php page for further processing (successful but with issues). The first point, ...

application not being served by express file

Here is how my express file is set up: var express = require('express'); var http = require('http'); var conf = require('./conf'); var app = express(); /*********** start server ************/ http.createServer(app).listen( ...

Populate modal form with database information without using Bootstrap

As I work on populating a form with data from an sqlite database, I've come across a stumbling block. My code is available for reference on JSFiddle: https://jsfiddle.net/daikini/e71mvg7n/ One important note: Bootstrap isn't being utilized in t ...