Utilizing AngularJS with an extensive collection of JSON data

Currently, I am working on developing an application that utilizes a Parent-Child hierarchy by incorporating AngularJs to connect the information retrieved from the RESTFUL web service with the user interface.

To demonstrate, here is an example of the JSON structure that the web service might provide:

{"id":0,"name":"Parent One","listOfChildren":[{"id":0,"name":"Child 1","childType":"CHILD1"},{"id":1,"name":"Child 2","childType":"CHILD1"}]}

While I understand how to store this data in an array and present it in a table through Angular's ng-repeat feature, I am faced with a challenge. With potentially hundreds or even thousands of children for each parent, and countless parents overall, managing such a massive array becomes daunting.

Additionally, updating a parent with a new child requires searching the entire array to locate the specific parent before inserting the new child, resulting in significant inefficiency.

Therefore, my inquiry revolves around finding a more streamlined approach to handle this scenario without relying on a single extensive array.

I appreciate any guidance or assistance provided. Thank you.

Answer №1

Given the vast amount of data at hand, finding a flawless solution may prove to be challenging. Do you really need to display all parents and children simultaneously? Perhaps implementing pagination to show only 100 nodes at a time could be beneficial. It seems in your case that both parent and children share the same id property value of 0. Is this accurate or do parents and children possess distinct ids? Are you able to modify the format of the json provided by the backend web service?

Bearing some assumptions in mind based on the aforementioned questions, here is my suggested proposal:

Transform your JSON into a structured format similar to the following:

var bigobject = {
    100: {
        "id":100,
        "name":"Parent One",
        "listOfChildren":{
            101: {
                "id":101,
                "name":"Child 1",
                "childType":"CHILD1"
            },
            102: {
                "id":102,
                "name":"Child 2",
                "childType":"CHILD1"
            }
            ... // further children listed here
        }
    },
    103: {
        "id":103,
        "name":"Parent Two",
        "listOfChildren":{
            104: {
                "id":104,
                "name":"Child 3",
                "childType":"CHILD1"
            },
            105: {
                "id":105,
                "name":"Child 4",
                "childType":"CHILD1"
            }
            ... // more children listed here
        }
    },
    ...  // additional parents included
}

The concept involves organizing a single comprehensive json object where each node’s key corresponds to its id. Moreover, transforming a parent’s listOfChildren from an array to an object bearing keys matching the ids of its children.

Incorporating a new child to a parent becomes relatively straightforward. For instance:

var parentId = 103;
var newChild = {"id":110,"name":"Child 2","childType":"CHILD1"};
bigobject[parentId].listOfChildren[newChild.id] = newChild;

To facilitate paginating results, one could utilize the provided limitTo filter along with a customized startFrom filter:

.filter('startFrom', function() {
    return function(input, start) {
        if(!input) return input;
        start = +start;
        return input.slice(start);
    };
});

This would necessitate adjusting your ng-repeat as follows:

<div ng-repeat="(parentId, parent) in bigobject | startFrom:start | limitTo:pageSize">
    <div ng-repeat="(childId, child) in bigobject[parentId].listOfChildren">
        {{parent.name}} - {{child.name}}
    </div>
</div>

I trust this guidance proves helpful in steering you towards a viable direction.

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

ReactJS is throwing an error stating that the component is undefined

Experimenting with ReactJS, I've set up a hierarchy of three nested components: UserProfile.jsx import React from 'react'; const UserProfile = React.createClass({ getInitialState: function() { return { username: "zuck" }; ...

Learn the technique of looping through multiple HTML elements and wrapping them in Vue.js easily!

i need to wrap 2 HTML elements together Here is my code snippet using Vue.js <tr> <th v-for="(item9,index) in product_all" :key="item9.id"><center>Qty</center></th> <th v-for="(item99,index) in product_all" :key=" ...

Android's clean white backdrop as the keyboard disappears

Can someone help me with an issue I'm having regarding the background image on my app? Click here to see the image The background image is set up like this : .pane { background-image: url("../img/inner-banner-bg.jpg"); background-repeat: rep ...

Is there a way to stop mUI from displaying the dropdown menu once Chrome automatically fills in a user's address details?

I am currently developing a form for users to enter their address in order to collect a billing address for payment information. Our services are only available to customers within the United States, so we have implemented an autocomplete UI component with ...

Ways to interpret/decode dynamic JSON structures

Scenario: Here is an example of JSON data with values only, no hard-coded keys for 'City' or 'Temperature' mentioned: { "Bangalore_City": "35_Temperature", "NewYork_City": "31_Temperature", "Copenhagen_City": "29_Temperature ...

Did an ASP.NET Core WebAPI HttpPost contain a JSON property?

Within my ASP.NET Core WebAPI Controller, I have a HttpPost method that receives the request body using the [FromBody] attribute. In this scenario, let's consider that I anticipate receiving the following JSON input, where null is an acceptable value ...

Using Jquery to dynamically add or remove a class based on scrolling behavior

Can someone help me with identifying the position of an element so that when the user scrolls to it and it appears on the screen, an icon can be highlighted? Currently, the script I am using adds the class too early, closer to the bottom of the block. I w ...

Having difficulty getting the sign operator to show up in a text field

Whenever the ADD div is clicked, "+" should be displayed on the textbox. The same goes for SUBTRACT, MULTIPLY, and DIVIDE. However, I am struggling to make the operators show on the textbox. Here is what I have managed to come up with so far. <!D ...

Content must be concealed following the third paragraph

Dealing with an API that generates content in p tags, which can become excessively long. Considered hiding the content after 400 characters, but it poses a risk of cutting through HTML tags. Instead, looking to hide the excess content after 3 paragraphs a ...

When implementing Passport-jwt for fetching user data, req.user may sometimes be undefined

No matter how many answers I search for on Stackoverflow or read through the documentation, I still can't solve my problem. Signing in and signing up works perfectly fine - I have my token. But when I try to fetch my current_user using get('/isAu ...

Maximizing JSON performance: Enhancing Drupal backend and optimizing IONIC Frontend

Currently, I am in the process of developing a hybrid app that combines IONIC and AngularJS, with Drupal serving as the Backend. The data is being fetched using Drupal services and returned in JSON format. My main concern lies in optimizing the speed an ...

Reactivating a function with jQuery once the page has finished loading

When using the Turn4JS function to generate a book interface on my website, I encountered an issue with setting the book size in absolute pixels rather than as a percentage or in another format. For example: $( document ).ready(function() { $("#guest ...

Using Flask to send JSON data to the client and then redirect with make_request

I have created a callback URL with a redirection feature: @spotify_auth_bp.route("/callback", methods=['GET', 'POST']) def spotify_callback(): SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token" CLIENT_ID = os.enviro ...

Using a delimiter to assign the values of one array as keys to another array requires intricate logic and deep thought

Good day to you :-) I currently have two arrays structured as follows: Array 1 Array ( [0] => DATA 1\n [1] => DATA 2\n [2] => DATA 3\n [3] => =\n [4] => DATA 4\n [5] => DATA 5\n ...

What is the behavior of a variable when it is assigned an object?

When using the post method, the application retrieves an object from the HTML form as shown below: code : app.post("/foo", (req, res)=> { const data = req.body; const itemId = req.body.id; console.log(data); console.log(itemId); }) ...

Internet Explorer 11 Ajax problem

Once again, Internet Explorer is causing some issues. I have a file called validation.php where the values from a text box are sent for validation. The text box value is read and then a result indicates whether it is valid or not. This functionality work ...

Enable or disable dropdown based on selected radio button status

I need the dropdown to be disabled unless the radio button labeled prov is selected. It should only be enabled when prov is chosen. I attempted to create a demo, but it's not functioning correctly and I can't figure out why. HTML: <td colsp ...

Getting event properties in a React component using the rest operator: A comprehensive guide

Can someone please assist me? I am new to TypeScript and struggling with how to use event props in my component. I have defined two props and need all my events as rest props. I encountered an error when trying to use my component with onClick event. The ...

When using GSON to convert a JSON string to an object, some unexpected nulls and zeros are being returned

Having a bit of trouble with deserializing a JSON string using Gson. This is the structure of my Notebook class: public class Notebook { public static String DESCRIPTION = "description"; @SerializedName("id") private long id; @Ser ...

Template for standardized data for testing across multiple languages

Currently, I am developing a font library in both C++ and Java. We are looking to automatically generate test data from multiple fonts using Google Test and JUnit for testing purposes. To achieve this, I plan to create a script that will generate data in ...