Troubleshooting data binding problems when using an Array of Objects in MatTableDataSource within Angular

I am encountering an issue when trying to bind an array of objects data to a MatTableDataSource; the table displays empty results. I suspect there is a minor problem with data binding in my code snippet below.

endPointsDataSource;
  endPointsLength;
  endPointsPage = 0;
  endPointsPageSize = 50;
  allProjectsEndpointsList = [];

  populateCoverage(objList) {
    let tObj = { resourceName: '', endpoints: [] }
    let cont = 1;
    let lastName = ''
    try {
      lastName = objList[0][4];
    } catch (e) { }
    tObj.resourceName = lastName;
    let resourceDefinitionList = [];
    objList.forEach(function (item) {
      let obj = { "projectName": item[6], "method": item[1], "endpoint": item[0], "description": item[3], "summary": item[4], "isManual": item[5] };
      if (lastName == item[4]) tObj.endpoints.push(obj);
      else if (lastName != item[4]) {
        resourceDefinitionList.push(tObj);
        tObj = { resourceName: item[4], endpoints: [obj] }; lastName = item[4]
      }
    });
    var arr: any = []
    arr = tObj;
    resourceDefinitionList.push(arr)
    return resourceDefinitionList;
  }

  showAllProjectsEndpoints() {
    this.resource.getAllProjectsEndpoints().subscribe((results) => {
      this.handler.hideLoader();
      if (this.handler.handle(results)) {
        return;
      }

      this.allProjectsEndpointsList = this.populateCoverage(results['data']);
      this.endPointsDataSource = new MatTableDataSource(this.allProjectsEndpointsList);
    }, (error) => {
      this.handler.hideLoader();
      this.handler.error(error);
    });
  }

Here is the corresponding html template code:

<div class="mat-elevation-z8 ">

                    <mat-table [dataSource]="endPointsDataSource">

                        <!-- projectName Column -->
                        <ng-container matColumnDef="projectName">
                            <mat-header-cell *matHeaderCellDef>Name
                                <mat-cell *matCellDef="let element">
                                    {{element.projectName}}</mat-cell>

                            </mat-header-cell>
                        </ng-container>

                        <!-- method Endpoint Column -->
                        <ng-container matColumnDef="method">
                            <mat-header-cell *matHeaderCellDef>Endpoint
                                <mat-cell *matCellDef="let element">{{element.method}}</mat-cell>
                            </mat-header-cell>
                        </ng-container>

                        <!-- endpoint Column -->
                        <ng-container matColumnDef="endpoint">
                            <mat-header-cell *matHeaderCellDef>Endpoint
                                <mat-cell *matCellDef="let element">{{element.endpoint}}</mat-cell>
                            </mat-header-cell>
                        </ng-container>

                        <!-- description Column -->
                        <ng-container matColumnDef="description">
                            <mat-header-cell *matHeaderCellDef>Description
                                <mat-cell *matCellDef="let element">{{element.description}}</mat-cell>
                            </mat-header-cell>
                        </ng-container>
                        
                        <mat-header-row *matHeaderRowDef="endPointsColumns"></mat-header-row>
                        <mat-row *matRowDef="let row; columns: endPointsColumns;"></mat-row>

                    </mat-table>

                    <mat-paginator [hidden]="vulnerabilityLength == 0" matColumnDef="position"
                        [pageSizeOptions]="pageSizeOptions" [pageSize]="vulnerabilityPageSize"
                        [pageIndex]="vulnerabilityPage" (page)="changeVul($event)" [length]="vulnerabilityLength">
                    </mat-paginator>
                    
                </div>

If someone could review and correct any mistakes I may have made?

The resulting empty table can be seen here.

The retrieved data appears as shown here.

Answer №1

Make sure to review your function populateCoverage. Ensure that each item in your allProjectsEndpointsList array is formatted correctly.

{resourceName:...,endpoints:[...]}

Therefore, you must add some code like this:

{{elements.endpoints[0].description}}

HINT: It can be helpful to write in a td sometimes.

{{element|json}}

In order to visualize the formatting of the element -even if the table breaks-

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

Adding Tooltips to Your Bootstrap 5 Floating Labels Form: A Step-by-Step Guide

Can you include tooltips or popovers in Bootstrap 5 floating labels text? I've set up a form with floating form fields and I'd like to provide instructions for certain fields using tooltips or popovers. I can make it work with the standard form ...

Place the Drawer element at the bottom of the menu

Is there a way to position the menu point at the bottom of the screen? It would be great if we could easily customize the style of the navigation options for each element. Check out my App Navigator below: const AppNavigator = createDrawerNavigator( ...

Angular button not functioning properly with disabled attribute

I have been experimenting with both [disabled] or disabled but it doesn't seem to be working according to my TypeScript conditions. There are 4 buttons that I need to disable based on various TypeScript conditions. I have a total of 7 conditions that ...

Adjust the size of both the right and left price scales on TradingView Lightweight Charts

Is it possible to set a fixed width for both the right and left price scales on a chart? Let's say, 50 pixels for the right price scale and 70 pixels for the left price scale? For a working example, please visit https://jsfiddle.net/TradingView/cnbam ...

Differences in how line breaks are handled in script output have been observed when comparing Atom and Notepad

Currently, I am utilizing a small script that generates a .txt file and inputs some data into it. Strangely, when I open the .txt file in Atom, the content appears on separate lines as I intended. However, when I access the same file in notepad, all the co ...

storing information between elements

Imagine I have a requirement to include a data provider element for my users, like this: <user-data-provider user-data="{{data}}"></user-data-provider> This element would send an ajax request to retrieve the logged in user's information. ...

Tips for implementing lazy loading with an owl carousel featuring background images

Is there a way to add lazy loading to a semi custom owl carousel that uses background images instead of regular img tags? I've tried using Owl carousel's function for lazy loading but it doesn't seem to work. How can I achieve this? This is ...

Retrieving properties from video element following webpage loading

I am trying to access the 'currentSrc' value from a video object in my code. Here is what I have: mounted: function () { this.$nextTick(function () { console.log(document.getElementById('video').currentSrc) }); }, Despi ...

Scripts for Azure mobile services

As a newcomer to server scripts, I am facing an issue that I believe has a simple solution, although I have been unable to find the answer so far. My current setup involves using azure mobile services for retrieving and inputting user information, with a f ...

Deliver output data from Spring to Angular

I am working on a microservice in Spring that needs to return a value distinguishing between users and admins to Angular. So far, I have managed to return a boolean value, but I am struggling to change it to work with strings or any other data type. I trie ...

Avoiding server requests in Firefox by refraining from using ajax

I've implemented the following jquery code: $("#tasksViewType").selectBox().change( function (){ var userId = $('#hiddenUserId').val(); var viewTypeId = $("#tasksViewType").val(); $.post('updateViewType& ...

Tips for integrating a back button functionality with AJAX requests

My single page web application allows users to enter genre, date range, and other inputs before making an ajax post request to a Java Spring MVC server. Despite everything working well, I now want to implement a back functionality. If a user wants to go b ...

When utilizing a [ngSwitch] with the toolbar (md-toolbar) in Angular2, the buttons fail to load correctly

I am currently designing a toolbar that includes a menu button using the following code: <md-toolbar class="header"> <div class="header-wrapper m-x-30 clearfix"> <div class="logo-container"> <div class="logo-image align-middle" ...

Every time I push my code to Heroku, the deployment runs smoothly. However, I encounter a frustrating 404 error when trying to access

When deploying my app, I encounter an issue where the .glb file in my three.js project receives a 404 resource not found error. Despite trying to adjust the file path without success, the deployment of the entire project is flawless. For local running, I a ...

Working with arrow functions in TypeScript syntax

I came across the following code snippet in TypeScript: (() => { const abc = 'blabla'; ... })(); Can someone explain what exactly this syntax means? I understand arrow functions in JS, so I get this: () => { const abc = &apos ...

Typescript does not directly manipulate values. For instance, using a statement like `if(1==2)` is prohibited

I am currently developing an Angular application with a code coverage report feature. There is a method where I need to skip certain lines based on a false condition. So, I attempted to create a function in my component like this: sum(num1:number,num2:nu ...

The websocket connection established with apollo-server is somehow producing nonsensical output for the connection params

onConnect should receive the connectionParams supplied by the client and then validate that the token has not expired by checking the token property on the connectionParams object. On the client-side, I send these parameters in the following manner: const ...

Troubleshooting CORS problem: Angular 2 and AspNetCore WebApi encounter error with preflight response having invalid HTTP status code 401

Hello there! I am currently working on implementing a straightforward token based authentication method in an Angular 2 RC6 application against an AspNetCore WebApi project that I developed using Visual Studio 2015. If you're interested, I have uploa ...

I'm having trouble retrieving data from the server using the AngularJS $http.get() function. What am I doing wrong

Ensure that your question is clear and that your code properly showcases the issue at hand. Feel free to leave any questions or comments below for clarification. app.js var express = require('express'); var app = express(); app.use(express.sta ...

Top 5 Benefits of Utilizing Props over Directly Accessing Parent Data in Vue

When working with VueJS, I've noticed different approaches to accessing parent properties from a component. For example, let's say I need to utilize the parent property items in my component. Option One In this method, the component has a props ...