Show rows in the table based on the child nodes retrieved from the backend database

I am facing a minor issue with a table

Below is the table

    
    <table>
      <thead>
        <th>Name</th>
        <th ng-repeat="value in drilldownReport.columns">
         {{ drilldownReport.columnNames[value] }}
        </th>
      </thead>
      <tbody>
        <tr ng-repeat="offer in drilldownReport.data">
          <td>{{ offer.criteria.id }}</td>
          <td ng-repeat="value in drilldownReport.columns">
            {{ drilldownReport.fixDisplay(offer.overall[value]) }}
          </td>
        </tr>
        <tr ng-repeat="offer in drilldownReport.childsNodes">
          <td>{{ offer.criteria.id }}</td>
          <td ng-repeat="value in drilldownReport.columns">
            {{ offer.overall[value] }}
          </td>
        </tr>
      </tbody>
    </table>

The data is fetched from the controller as shown below

     
    .then(function (data) {
      vm.data = data;
      vm.data.map(function(items) {
        console.log(items.childs);
        vm.childsNodes = items.childs;
      });
    }

where vm.data returns an array of objects like this

[  
   {  
      "type":"offer",
      "criteria":{  
         "type":"offer_id",
         "id":"55e8f8d43744b0cd38bfb6bd"
      },
      "overall":{  
         "cost_per_click":0,
         "offer_price":0
      },
      "childs":[  
         {  
            "type":"offer",
            "criteria":{  
               "type":"browser",
               "id":"Firefox"
            },
            "overall":{  
               "cost_per_click":0,
               "offer_price":0
            },
            "childs":[  
              ...
            ]
         },
         {  
            "type":"offer",
            "criteria":{  
               "type":"browser",
               "id":"Chrome"
            },
            "overall":{  
               "cost_per_click":0,
               "offer_price":0
            },
            "childs":[  
              ...
            ]
         }
      ]
   }
]

The structure includes nested childs which replicate the parent node properties endlessly. Each object within childs can also have its own set of nested childs. The number of nested levels can vary, ranging from 1 to multiple.

Any suggestions on how to render this in the table effectively?

EDIT

In case you need information about the columns and columnNames

vm.columns = [
  "offer_price",
  "cost_per_click"
]

vm.columnNames = {
  "offer_price": "Offer Price",
  "cost_per_click": "CPC"
};

I came across this article which seems similar to my requirement, but I am unsure how to integrate it into my existing code.

Answer №1

If you want to implement a recursive directive, you can achieve it by following these steps:

Element.html

<tr>
    <td>{{item.id}}</td>
    <td>{{item.name}}</td>
</tr>
<element ng-repeat="subItem in item.children" item="subItem"></element>

elementDirective.js

.directive('element', function() {
    restrict: 'E',
    templateUrl: 'path/Element.html',
    scope: {
        item: '='
    }
});

In your main HTML file:

<table>
    <tr><th> ... </th></tr>
    <element item="nestedData">
</table>

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

What is the best way to perform a redirect in Node.js and Express.js following a user's successful login

As I work on developing an online community application with nodejs/expressjs, one persistent issue is arising when it comes to redirecting users to the correct page after they have successfully signed in. Despite reading several related articles and attem ...

What is the best method for encrypting a URL that contains AngularJS data?

Here is the URL that needs to be encrypted: <a class="btn btn-success btn-sm btn-block" href="@Url.Action("myAction", "myController")?Id={{repeat.Id}}&HistoryId={{repeat.HistoryId}}" ng-cloak>View History</a> I am seeking guidance on enc ...

Using the PATCH method in Rails instead of POST

i have some code to display here <%= form_for @campaign, url: brands_campaign_path(@campaign), method: :patch, :html => {:multipart => true, :class => 'form-horizontal'} do |f| %> and it generates the following <form class ...

Utilizing JSON to display multiple markers on a Google Map

How can I display multiple markers on a Google Map using JSON data? I have successfully added a single marker, but I'm facing issues with adding multiple markers. Below is the code for a single marker (which is working): var lat=position.coords.lati ...

Use JavaScript to submit a form in Django containing the <a></a> tag

I am having an issue with my form submission using JavaScript. When I try to submit the form, it is sending a GET request to the backend, but I am expecting it to send a POST request instead. Here is the HTML file code snippet: <form id="my_form&qu ...

Transfer password securely through an ajax call

Is it secure to send a password through an Ajax request? I have a login box that makes an Ajax request to check the login credentials and receive a JSON Object with any errors. Would it be better to use form redirection instead? [EDIT] Storing the encry ...

Loading the Facebook JavaScript SDK asynchronously using React and Redux

I have a process that involves loading a JavaScript SDK, which looks like this: class Facebook{ constructor(){ window.fbAsyncInit = () => { FB.init({ appId : 'myappID', ...

AngularJS - updating my UI after making changes to the data

Hello, I am looking to update an image on my user interface. I have different data points with a star icon. When I click on the star, it toggles a value in my object between true and false. This functionality is working well, but I am unsure how to dynamic ...

Nested data selection in React

I have been experimenting with creating a nested select optgroup and attempted the following: const data = [ { sectorId: 5, sectorName: "Sector One", departments: [ { deptName: "Production", jobtitles: [ { ...

Tips for granting permissions to a single user profile to delete another user profile in jhipster

Is there a way to expand the administration drop down menu in jhipster to include other authorities? I believe I need to modify the navbar.html file and the specific line below: <li ng-class="{active: `vm.$state.includes('admin')`}" ng-switc ...

Updating the AngularJS promise does not reflect changes in the scope set to a single item within an array

I am working with two controllers: function AppCtrl($scope, $dataService) { $scope.projects = $dataService.data.projects; } function ProjectCtrl($scope, $route, $q) { //Accessing the current project from the projects array $scope.project = $s ...

Can express.js extract the prefix from a router?

I'm curious if there's a way to extract the prefix from the Router in express.js. Below is my index.js file: const express = require("express"); const router = express.Router(); const admin = require("./admin"); const home = require("./home"); ...

Animations in Phaser do not function properly on touch screen devices, although they work seamlessly on desktop computers

In my phaser.js game, I have a variety of animations set up: this.anims.create({ key: 'catchingUp', frames: this.anims.generateFrameNumbers('android', { start: 0, end: 4}), frameRate: this.frameSpeed * 6, ...

The jQuery toggle functionality seems to be malfunctioning

I have created a form that should toggle (hide and show) with the click of a button, but for some reason it's not working. Can someone please take a look at my code below and let me know what I'm doing wrong? $(document).ready(function () { ...

How to extract user data from a JWT token using Node.js?

In my MEAN stack authentication application, I am utilizing Node and Angular. After a successful login, I set a JWT token and store it in a session within the controller by assigning the token to config.headers through a service interceptor: var token = j ...

Adjusting the aspect ratio of an ortographic camera when resizing

Is there a method to configure an orthographic camera in THREE.JS in a way that: 1)It retains a consistent aspect ratio regardless of window resizing. This aspect ratio should remain constant and not be affected by the window's dimensions or aspect r ...

javascript ajax is not executing correctly

With the help of Ajax, I have developed a console that allows me to dynamically execute PHP functions. This is how it appears https://i.sstatic.net/Wj2Ww.png The issue arises when the console gets filled with numerous commands, making it difficult to rea ...

What is the most effective way to dynamically incorporate external input into a SlimerJS script?

Could use some assistance with SlimerJS. My current program requires intermittent input from stdin to proceed with the next task. The code below functions effectively when using PhantomJS+CasperJS for reading external input, but encounters difficulties wi ...

Creating a dynamic slider using jQuery

Here is the code snippet I have been working on: var current_slide = 1, animation_time = 1000, pause = 3000, slide_container = $('.slide_container'), interval, height = slide_container.height(), slides ...

What prevents me from utilizing interpolation within a directive?

I have come across a couple of articles discussing the topic of "Assigning a scoped variable to the value of an HTML attribute tag". Here are the references: https://docs.angularjs.org/guide/interpolation How to assign angularjs variable value to html el ...