Dynamic Angular TreeView showcasing nested children branches

I am in need of creating a treeView that can handle dynamic data. Currently, I am utilizing the syncfusion component which can be found at this link.

The challenge I am facing is that the data object I receive is incomplete, with the "children" being generated as I access them using the id. As a result, I am able to display the initial categories but struggle to link them with their children.

Below is the code snippet from app.component.html:

   <div class="control-section" style="overflow:hidden;">
    <div class="col-lg-12">
        <div class='col-lg-6 nested-data'>
            <div class='content'>
                <h4>Categories</h4>
                <ejs-treeview id="tree" #tree [checkedNodes]='checkedNodes' [fields]='fields'>
                </ejs-treeview>
            </div>
        </div>
    </div>
</div>

And here is the corresponding app.component.ts:

treeData!: object;
    public fields = {
      dataSource: this.treeData,
      id: 'SID',
      text: 'Name',
      child: 'Children'
    };
    public showCheckBox = true;
    public checkedNodes: string[] = ['70', '1', '88'];


 
    
    ngOnInit(): void {

      if(this.request){
        const res = this.service.getCategory(this.request);
        if(res){
          this.Cats= res;

          res.subscribe(tree =>{
            this.treeData =  tree.Category
            this.fields= {
              dataSource: this.treeData,
              id: tree.SID,
              text: 'Name',
              child: 'Children'
            };
          })
  
         }
      }
  

Currently, the issue lies in the fact that only the initial categories are displayed, without their children. Clicking on a category to retrieve its id and fetch the daughter categories is a workaround, but I am unable to do so due to the information being accessible only within a subscribe function and not globally stored. Any suggestions or solutions would be greatly appreciated.

Could someone offer assistance?

Answer №1

When working with a TreeView Hierarchical data source, you have the ability to link child nodes to parent nodes using the child property in the fields section. It is important to note that the data source mapped for the child property should have the same set of properties.

To learn more about the TreeView component, you can check out the documentation provided here.

By default, the TreeView requires a JSON data structure for rendering. Any type of data can be displayed in the TreeView as long as it follows the JSON structure. It is necessary to convert the data obtained from a service into JSON format. For guidance on how to do this, you can refer to this blog.

If you are still experiencing issues, please provide a sample demonstrating the problem for further validation.

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 process of inserting a sparkline chart into a Kendo Angular grid?

I am attempting to display a bullet chart in the first column of my grid. <kendo-grid-column> <ng-template kendoChartSeriesTooltipTemplate let-value="value"> <div> <kendo-sparkline [data]="bulletData" type="bullet" [ ...

Using a SharedModule in Angular2: A Guide

I have a single Angular2 component that I need to utilize across multiple modules. To achieve this, I created a SharedModule as shown below: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-bro ...

Improving my solution with PrimeNG in Angular2 - fixing the undefined tag error

After working with Angular for just three days, I successfully set up a login page dashboard using a web API solution. Everything was working great until I encountered an issue when trying to load the PrimeNG DataTableModule into my components. After searc ...

Utilizing const as the iteration variable in a for loop

I've grasped the concept of using var and let in a for loop in typescript/javascript, but can someone shed light on how and why a const variable as a loop variable behaves? for (const i = 0; i < 5; i++) { setTimeout(function() { console.log( ...

Unusual behavior when importing in Angular 2 using TypeScript

While working on a demo for another question on Stack Overflow, I initially used angular-cli and then switched to Plunker. I noticed a peculiar difference in behavior with the import statement between the two setups. The issue arises with the second impo ...

How come the inference mechanism selects the type from the last function in the intersection of functions?

type T = (() => 1) & (() => 2) extends () => infer R ? R : unknown What is the reason that T is not never (1 & 2)? Does the type always come from the last function, or can it come from one of them? ...

Why does Typescript not enforce a specific return type for my function?

In my custom Factory function, I need to return a specific type: type Factory<T> = () => T; interface Widget { creationTime: number; } const createWidget: Factory<Widget> = () => { return { creationTime: Date.now(), foo: &a ...

Tips for changing input field type from "password" to "text" in Angular?

Is there a way to dynamically convert an input field with type="password" to type="text" in Angular? In my demo, I have two input fields labeled Mobile no and Re-enter mobile number. I want these fields to change to type="text" if the user inputs the same ...

How to store an imported JSON file in a variable using TypeScript

I am facing a challenge with a JSON file that stores crucial data in the following format { "login": { "email": "Email", "firstName": "First name", "lastName": "Last name", ...

How to create an array of objects in Angular 2

Can someone please help me understand how to set up an array of objects so that I can add items to it later on? Here's what I currently have: tab: [{tel:number, name:String}]; ...

Effective strategies for extracting value from asynchronous Angular events that return Promises

When it comes to subscription blocks, extracting the value from my API is possible. var data = null; this._timinServiceProxy.getDateFromNTP().subscribe(result => { data = result; console.log(data); // The expected result }); console.log(data); ...

Exploring the World of Popper.js Modifiers

Within our React and Typescript application, we integrate the react-datepicker library, which utilizes popper.js. Attempting to configure PopperModifiers according to the example provided here: . Despite replicating the exact setup from the example, a typ ...

(Enhancing Angular) Capture HttpResponse errors and seamlessly proceed with the Observable

There's a dropdown text box with type-ahead search functionality. Valid item names prompt the expected list of items in the drop-down menu, while invalid entries trigger a 400 error response from the API. This error is caught by the HttpErrorIntercept ...

Tips for accessing the following element within an array using a for loop with the syntax for (let obj of objects)

Is there a way to access the next element in an array while iterating through it? for (let item of list) { // accessing the item at index + 1 } Although I am aware that I could use a traditional for loop, I would rather stick with this syntax. for (i ...

Symfony seems to be dropping my session unexpectedly during certain requests

Currently dealing with angular 2, I am encountering issues with requesting symfony where certain requests cause the sessions to be lost. Strangely enough, some requests work perfectly fine while others do not. If anyone has any insight or advice on what co ...

Step-by-step guide on setting up multiple tabs in Ionic to work with Firebase

I've been working on developing an offline-accessible application, but I'm facing some challenges. When I test the app without an internet connection, the cached data doesn't display properly. However, if I first open the app online and then ...

Issue with sx prop type error found in the TabPanel component of MUI v5

My first time using MUI with TypeScript has hit a roadblock with the new sx prop. Check out the error displayed by TypeScript in the screenshot linked below: https://i.sstatic.net/JYDTX.png Interestingly, the error only pops up on the TabPanel Component, ...

The Gatsby + Typescript project is reporting that the module with the name "*.module.scss" does not have any exported members

I've recently gone through Gatsby's demo project in their documentation (which is long overdue for an update). I've carefully followed the instructions provided here: I've included an index.d.ts file in the /src directory of my project ...

What is the mechanism behind the integration of Cross Origin functionality between Spring Boot and Angular CLI?

I came across this helpful Spring/Angular tutorial that I've been following. However, when I try to run my application, I encounter the following error: The browser is blocking access to XMLHttpRequest at 'http://localhost:8080/api/employees&ap ...

Retrieve files by utilizing the find() function in couchdb-nano

As CouchDB doesn't have collections, I decided to add a custom type property to my entities. Now, I want to filter all the entities based on that property, for example, retrieve all users with {type:'user'}. In the CouchDB documentation, I c ...