Unable to render information within the PrimeNG tree component

I've set a goal for myself to create a treeview using the PrimeNG Tree Component.

Currently, I have a small service with the following method:

TypeScript:

 getMenuDetails(parentID: number) {
   let url = this.serverURL + 'api/Nodes/' + parentID;
   console.log(url);
   return this.http.get(url)
    .map(res =>  {
      return <TreeNode[]>res.json()
    })
 }

In my component, I call this service like this:

TypeScript:

export class TreeViewComponent implements OnInit {
  mainTree: number = 1;
  rootDetails: TreeNode[];

  constructor(private projectService: ProjectRoleService) { }

  ngOnInit() {
    this.projectService.getMenuDetails(this.mainTree)
      .subscribe(res => this.rootDetails = res)
        console.log(this.rootDetails);    
      };

The issue I'm facing is that I can't seem to display the data in

this.rootDetails

HTML:

<p-tree [value]="rootDetails"></p-tree>

Error:

Cannot find a differ supporting object '[object Object]' of type 'Wurzel'. NgFor only supports binding to Iterables such as Arrays.

Any suggestions on how to resolve this?

EDIT 1:

This is what the data returned from the service looks like:

{
    descr: "WurzelNode"
    id: 1
    lvl: 1
    name: "Wurzel"
    parentid: 0
    position: 1
    rootid:1
}

Answer №1

With PrimeNg, you have the ability to create templates that allow you to utilize your own custom JSON data.

<p-tree [value]="files">
    <ng-template let-node  pTemplate="default">
        {{node.title}}
    </ng-template>
</p-tree>

In the example above, title represents the JSON key that I want to use for displaying content in my tree structure.

Answer №2

When you initially defined the result as an array in this particular scenario,

    rootDetails: TreeNode[];

it seems that what you provided for [value] does not match your later explanations, indicating that it is not actually an array. Try switching to

    this.projectService.getMenuDetails(this.mainTree)
        .subscribe(res => this.rootDetails = [res]

and see if that resolves the issue.

Answer №3

fetchData(){
    this.dataService.retrieveLazyFiles(
    this.URL,
    (response:any) => {
      this.structure.nodes = response.map((element) => <TreeNode>{

        label : `${element.title}_MyValue`,
        data: element.id,
        "expandedIcon": "fa fa-folder-open",
        "collapsedIcon": "fa fa-folder",
        leaf:false,
        type:"1",

      });

    }, (error) => {
      console.log(error);
    });
 }

In addition to the template mentioned, another method suggested by @Antikhippe is to transform the JSON data response and generate a new object with specific keys.

PS: Providing the code for anyone in need of a reference.

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

Developing an Angular 8 web application with seamless integration of ADFS 2.0 for

Looking for guidance on implementing authentication using ADFS 2.0 in my Angular 8 application. Any tips or sample applications would be greatly appreciated! ...

When using the Google API load callback, the Angular(4) router does not actually replace routes; instead, it stacks them up each time

I've been attempting to implement Google Authentication in my Angular 4 application. I have successfully loaded the Google platform.js and api.js in my index.html file. When I click on the login button, this is what I have coded: gapi.load('auth ...

Typescript's Integrated Compatibility of Types

One important concept I need to convey is that if one of these fields exists, then the other must also exist. When these two fields are peers within the same scope, it can be challenging to clearly communicate this connection. Consider the example of defi ...

What is the best way to incorporate code on a Grails server with an Angular client?

After following the steps outlined in this tutorial and making modifications on the angular side, I find myself facing a challenge. How can I incorporate a function into the post method? And where exactly on the server side can I implement an automated ema ...

React did not allow the duplicate image to be uploaded again

I've implemented a piece of code allowing users to upload images to the react-easy-crop package. There's also an "x" button that enables them to remove the image and upload another one. However, I'm currently facing an issue where users are ...

Encountering the error message "Undefined. Please implement using the following snippet" while running a protractor cucumber typescript feature file

Currently, I am utilizing Protractor with TypeScript and Cucumber for automation purposes. After going through some informative articles, I have successfully incorporated feature files and step definitions into my end-to-end project. Below is the structur ...

Troubleshooting navigation bar problems: dealing with overlapping and padding challenges in

I'm exploring how to create a hover effect with a bottom border that overlaps with the parent div (navbar) bottom border. I've applied margin:0px and padding:0px to the navbar container. I'm unsure how to make the hover bottom border effec ...

Using ngFor in Angular 6 to create a table with rowspan functionality

Check out this functional code snippet Desire for the table layout: <table class="table table-bordered "> <thead> <tr id="first"> <th id="table-header"> <font color="white">No.</font> </th> <th id="table-hea ...

Using ngb-carousel to display embedded YouTube iframe videos

Can anyone help me figure out how to embed an Iframe into a ngb-carousel? The carousel is displaying correctly, but the iframe tab is not showing up as expected. <ngb-carousel *ngIf="project.images"> <ng-template class="item" n ...

Tips for resolving the <!--bindings={ "ng-reflect-ng-for-of": "" }--> bug

Just starting out with Angular and I am having some issues with event binding and property binding in my game-control component. I have a button that, when clicked, adds numbers to an array using setInterval method. I am also passing data between compone ...

Only subscribe to Angular 2+ observable when there is a change

Let's look at a situation I'm facing. I have a user service that contains a BehaviorSubject and a method that returns an observable of this BehaviorSubject. Another file is the header component, which subscribes to this observable. The dilemma is ...

React TypeScript with ForwardRef feature is causing an error: Property 'ref' is not found in type 'IntrinsicAttributes'

After spending a considerable amount of time grappling with typings and forwardRefs in React and TypeScript, I am really hoping someone can help clarify things for me. I am currently working on a DataList component that consists of three main parts: A Co ...

Guide to testing template-driven forms in Angular 6

Currently, I am working on a template-driven form which looks like this: <form #form="ngForm" (ngSubmit)="onSubmit()"> <input class="form-control input-lg" id="venue_name" name="venue_name" type="text" #venue_name="ngModel" [(n ...

"An identifier was expected causing a parsing error" triggered by v-on:change

Encountered an issue where importing a class to be used solely as a typescript type annotation resulted in a no-unused vars error. Following advice from this discussion thread, I added "plugin:@typescript-eslint/recommended" to the eslint config, ...

Angular project is showing a unique error that says '$localize' is undefined according to Eslint

I successfully implemented localization in my Angular 15 project. However, I encountered an issue with linting for .ts files: error '$localize' is not defined no-undef Here's the configuration in my .eslintrc.json file: { "root": true, ...

Creating the upcoming application without @react-google-maps/api is simply not possible

After incorporating a map from the documentation into my component, everything seemed to be functioning correctly in the development version. However, when attempting to build the project, an error arose: Type error: 'GoogleMap' cannot be used as ...

The mysterious button that reveals its text only when graced by the presence of a

The text on the button (Remove in this case) will only show up when the mouse is hovered over it. This issue is occurring in an angular project and despite trying to apply CSS for .btn, it does not get overridden. background-color: blue; Button's ...

Integrating a fresh element into the carousel structure will automatically generate a new row within Angular

I'm currently working on an Angular4 application that features a carousel displaying products, their names, and prices. At the moment, there are 6 products organized into two rows of 3 each. The carousel includes buttons to navigate left or right to d ...

Angular Build Showing Error with Visual Studio Code 'experimentalDecorators' Configuration

Currently experiencing an issue in VSC where all my typescript classes are triggering intellisense and showing a warning that says: "[ts] Experimental support for is a feature that is subject to change in a future build. Set the 'experimentalDeco ...

Error: Cannot find definition of Chart in conjunction with primeNg and angular 4

An error occurs when I remove the following line from my index.html file <script src="node_modules/chart.js/dist/Chart.js"></script> Is it possible to eliminate this line from index.html and instead include the equivalent line in my systemjs. ...