Exploring the power of AngularJS by nesting elements within multidimensional arrays and revealing their content through a simple

Check out this fiddle for accurate information, though it may not be visually positioned correctly:

http://jsfiddle.net/LhRfq/

I am working with a three-dimensional JSON object.

The first level of array items should be displayed in a single line, which is currently working fine...

Similarly, the second layer of selected sub-array data is also displayed in a line when the top array item is checked.

I have been successful in displaying the third layer as well, but I need the sub-items to be directly below their parent array item...

Although my current code is close to what I need, I am struggling to isolate the specific sub-array items for the selected element. As of now, the selected sub-array is displaying multiple times instead of just once...

This is my existing code snippet:

<div ng-repeat="publication in publicationData">
    <ul>
        <li>
            <input type="checkbox" name="pubCat" ng-model="publication.selected">
            {{publication.pubName}}
        </li>
    </ul>
</div>

<br/><br/>

<div ng-repeat="publication in publicationData | filter:{selected:true}" >
    <div>
        <ul id="publists">
            <li ng-repeat="category in publication.pubCat">
                <input type="checkbox" name="pubCat" ng-model="category.selected">{{category.pubCatName}}

                <!-- The following code displays the same array multiple times -->

                <div ng-repeat="publication in publicationData">
                    <div ng-repeat="category in publication.pubCat | filter:{selected:true}">

                        <div>
                            <ul>
                                <li ng-repeat="subcategory in category.pubSubCat">
                                    <input type="checkbox" name="pubSubCat">{{subcategory.pubSubCatName}}
                                </li>
                            </ul>
                        </div>

                    </div>  
                </div;

                <!-- Above code displays the same array multiple times -->

            </li>
        </ul>   
    </div>
</div>

I understand why this issue is happening, but I'm unsure how to correct it to achieve the desired result.

Can you suggest an approach to create a layout like the following example: if "Option 1" is checked in the first array and "SubOpt 1 B" is checked in the second?

|_| Option 1          |_| Option 2          |_| Option 3

|_| SubOpt 1 A        |_| SubOpt 1 B        |_| SubOpt 1 C

                    |_| SubSubOptB 1    
                    |_| SubSubOptB 2     
                    |_| SubSubOptB 3    
                    |_| SubSubOptB 4    

Answer №1

It seems like you are attempting to generate a dynamic gap with margin-left.

One way to do this is by utilizing the ng-style feature which allows for dynamically updating styles:

<div ng-repeat="publication in publicationData" ng-style="myStyle($index)">

In your controller:

 $scope.myStyle = function(index) {
        return {
            "margin-left": (93 * index) + "px" // just an example
        }
    };

This should point you in the right direction,

Check out the demo at Fiddle

I hope this aids you in your objective

Answer №2

My solution involved nesting the ng-repeat of the second dimension of a JSON array inside the ng-repeat of the first dimension, and then nesting the third dimension into the second. The key was to declare the ng-repeat in separate elements for each level of nesting.

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

Am I implementing the Vue.JS onChange function correctly?

After selecting an option from mod_accs_Role_ID, how can I display the corresponding data stored in the database based on that selection? For example, if I select 1 in mod_accs_Role_ID, then the role_Name 'Hello' should be displayed. <div clas ...

Issue with Trix text editor not triggering the change event

Lately, I've been facing some difficulties in getting the tirx-change event to trigger when there are changes in the content of a trix-editor. Despite using React JS for the view, I haven't been able to identify the problem. Below is the code sni ...

Combining arrays in JSON with the power of Groovy 2.4

Below is the JSON input I have, where I am attempting to merge scheduleDetails with the same ID: {"orderLines": [{ "ID": "001", "scheduleDetails": [{ "address": { ...

Utilizing the precise number of URLs provided within a Python array

Currently, I am working on setting up a python script that utilizes URLs. While I have a simple script in place that prompts for and saves the URLs to a file using the Pickle library, here is the code for reference. It's worth noting that I received h ...

Using a pointer to pointer within the main function to create an array in another function

I am struggling with the following code: #include <stdio.h> #include <stdlib.h> void mad(int ***, int, in); int main(void) { int **x; int n,m; scanf("%d%d",&n,&m); mad(x,n,m); x[0][0] = 5; printf("%d\n", ...

backbone.js router failing to recognize URL fragments

I have set up a basic router in my code and initialized it. Issue: I encountered an unexpected behavior when visiting the URL http://localhost/backbone1/#photos/5. Despite expecting an output from console.log() in the JavaScript console, nothing appears. ...

How to eliminate spacing between slides in a 3D Carousel Slider using Bootstrap 5

My website utilizes Bootstrap along with a carousel slider, which is functioning properly. However, I am encountering an issue when the slider transitions from right to left. It briefly displays a white space between slides, which I wish to eliminate. I wa ...

Show the subjects' names and their scores once they have been added to a fresh array

Here is my unique code snippet: let fruits: string[] = ['Apple', 'Banana', 'Orange', 'Grapes', 'Mango']; function capitalize(fruit: string) { return fruit.toUpperCase(); } let uppercaseFruits = fruits ...

Preventing Repeated Clicks in AngularJS

Looking for a better approach to handle double clicks in AngularJS other than using ng-disabled. I have a solution to disable double/multi-click functionality on button click and re-enable it after completing an ajax process. Any suggestions? When our cod ...

Tips for attaching an event listener to activate a server-side email feature?

I have a setup with an express application that serves up a static index.html page. Additionally, there is a function in my app.js file that sends an email whenever the server starts running. My goal is to modify this so that the email is only sent when a ...

Exploring the @HostBinding argument in Angular directives

Need help grasping the concept behind the @Hostbinding argument: Snippet of the code: import { Directive, HostBinding } from "@angular/core"; @Directive({ selector: '[appDropdown]' }) export class DropdownDirective { @HostBinding(&apos ...

How can I make <p> elements change color when scrolling?

My Goal https://i.sstatic.net/JbdXR.gif I aim to bring attention to the <p> element as the user scrolls on the page. Initially, the opacity is set to 0.3, but I want it to change to 1 gradually as the user scrolls down. My Attempt window.o ...

What are the steps for generating website endpoints using search query outcomes?

I am currently working on a ReactJS website as a part of my web development bootcamp project. One interesting feature I have incorporated is a search functionality that uses Flask routes to connect ReactJS endpoints (../Language.js) with my Sqlite3 databa ...

Implementing a dialog box pop-up from a separate React file

My journey with React is just beginning, so forgive me if this question seems basic. I have a delete icon in one of my files, and when it's clicked, I want to display a confirmation dialog box. I found an example on the official Material-UI website: h ...

Creating a user-friendly Express / Angular app from scratch: A beginner's guide

Is there a way to efficiently load a user object or any other object on an Express backend during the initial request and then instantly populate it in an Angular application? I have a particular application where I am sending a JWT token. After successf ...

Tips for effectively interpreting a json string

Trying to extract specific fields from a JSON string and display them on an HTML page, but encountering the following error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data This is the JSON being sent: { "Order0& ...

Generating a new array based on the keys found in a collection of objects

My array structure is quite complex with multiple objects containing different properties. let arr = [ { id: 1, name: "tony", hatColor: "blue" }, { id: 2, name: "larry", hatColor: "red" }, { id: 3, name ...

What could be causing the TypeError I encounter when trying to import @wordpress/element?

Encountering a similar issue as discussed in this related question. This time, I've switched to using '@wordpress/element' instead of 'react-dom/client' based on the recommendation that it also leverages React functionalities. Ho ...

Maximizing Efficiency: Sending Multiple Responses during computation with Express.js

Seeking a way to send multiple responses to a client while computing. See the example below: app.get("/test", (req, res) => { console.log('test'); setTimeout(() => { res.write('Yep'); setTime ...

Enhance Data Filtering in Ag-Grid Using Vue.js

Seeking assistance with Ag Grid in Vue js. I have a scenario where I want to disable the checkbox in the filter upon initial load so that the grid does not display records initially. Is this achievable? For example, in the screenshot provided in the link ...