Unable to display information on mat table without receiving an error message

Hello everyone, I am a novice in Angular and I seem to be encountering an issue with displaying data on my mat table. Surprisingly, no errors are being shown. While I can see the column names, the actual data is not visible. Can someone help me figure out what mistake I might have made?

api.service.ts

    export class ApiService {
     dataRow:any;
    }
constructor(private httpClient : HttpClient) { }

service.component.html

<tr mat-header-row *matHeaderRowDef="['sid','vid','serviceName','createdOn']"></tr>
        <tr class="rowhover" (click)="displayData(row,row.sid)" mat-row *matRowDef="let row; columns: ['sid','vid','serviceName','createdOn']"></tr>
        </table>

service.component.ts

 displayData(row,sid:any)
  {
   // console.log(row);    
    this.apiService.dataRow=row;
   this.router.navigate(["/vendor-list/vendor/a/services/a/details/",sid]);
  }

service-detail.component.html

 <table mat-table [dataSource]="serviceDetailsDataSource" class="mat-elevation-z1">
            <ng-container matColumnDef="sid">
                <th mat-header-cell *matHeaderCellDef> Service ID </th>
                <td mat-cell *matCellDef="let element"> {{element.sid}} </td>
            </ng-container>

            <ng-container matColumnDef="vid">
                <th mat-header-cell *matHeaderCellDef> Vendor ID </th>
                <td mat-cell *matCellDef="let element"> {{element.vid}} </td>
            </ng-container>

            <ng-container matColumnDef="serviceName">
                <th mat-header-cell *matHeaderCellDef> Service Name </th>
                <td mat-cell *matCellDef="let element"> {{element.serviceName}} </td>
            </ng-container>

            <ng-container matColumnDef="serviceDescription">
                <th mat-header-cell *matHeaderCellDef> Service Description </th>
                <td mat-cell *matCellDef="let element"> {{element.serviceDescription}} </td>
            </ng-container>

            <ng-container matColumnDef="createdOn">
                <th mat-header-cell *matHeaderCellDef> Created On </th>
                <td mat-cell *matCellDef="let element"> {{element.createdOn}} </td>
            </ng-container>

            <tr mat-header-row *matHeaderRowDef="['sid','vid','serviceName','serviceDescription','createdOn']"></tr>
            <tr mat-row *matRowDef="let row; columns: ['sid','vid','serviceName','serviceDescription','createdOn']"></tr>
            </table>

service-detail.component.ts

import { Component, OnInit  } from '@angular/core';
import { ApiService } from 'src/app/api.service';
import { MatTableDataSource } from '@angular/material';

@Component({
  selector: 'app-service-detail',
  templateUrl: './service-detail.component.html',
  styleUrls: ['./service-detail.component.css']
})
export class ServiceDetailComponent implements OnInit {

  serviceDetailsDataSource:any;

  serviceDetailsDisplayedColumns: string[] = ['serviceID', 'vendorID', 'createdOn'];
  /*serviceDetailsDataSource = SERVICE_DETAILS_DATA;*/

  constructor(public apiService:ApiService) { }
  dataRow1:any;
  ngOnInit() {
    this.dataRow1=this.apiService.dataRow;
    //this.serviceDetailsDataSource=this.dataRow1;
    this.serviceDetailsDataSource=new MatTableDataSource(this.dataRow1); 
    console.log(this.serviceDetailsDataSource)
  }

}

Answer №1

Make sure to populate the array with the data retrieved from the service.

Revise

this.serviceDetailsDataSource=new MatTableDataSource(this.dataRow1);

to

this.serviceDetailsDataSource=new MatTableDataSource([this.dataRow1]);

Hopefully this clarifies things for you!

Answer №2

The data retrieved from the API is in JSON format, therefore all you need to do is enclose it in square brackets like so:

MatTableDataSource([this.dataRow1]);

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

The data retrieved from an HTTP GET request is not displaying on the angular 4 line chart

Attempting to generate a line chart using Angular 4 and chart.js with data from a database received through an http get method is presenting some challenges: export class ValvesComponent implements OnInit { valves: Valve[]; ignitions: number[] = [8, 5 ...

A more efficient approach to specifying types for the 'navigation' object in React Native's Stack Navigator

Struggling with modularizing prop types in React Navigation, particularly when using TypeScript. The Typescript section of the React Navigation documentation suggests defining types for each screen props in this manner: //types.ts export type RootStackPara ...

Should we utilize the component @Input as a parameter for the injected service constructor, or should we opt for the ServiceFactory

Within Angular 12 lies a simplified component structured as follows: @Component({ selector: 'app-list', templateUrl: './list.component.html', styleUrls: ['./list.component.less'] }) export class ListComponent implements ...

How can one access a dynamically generated element in Angular without using querySelector?

Currently in the process of developing my custom toastr service, as shown in the GIF below https://i.sstatic.net/Zpbxs.gif My Objective: https://stackblitz.com/edit/angular-ivy-tgm4st?file=src/app/app.component.ts But without using queryselector. It&apos ...

Creating actions safely with redux typesafety

I've made a mistake several times by forgetting to properly extract the connected action creator from props, as shown below: import {actionCreator} from 'my-actions'; interface Props { actionCreator: typeof (actionCreator); } const Foo: ...

Encountering the error "Element implicitly has an 'any' type because expression of type 'string' cannot be used to index type '{}'" can be frustrating when working with React TypeScript

I'm encountering an issue when trying to access an object with an id in the code below. An error message stating 'Element implicitly has an 'any' type because expression of type 'string' can't be used to index type ' ...

Send a function as a property to a child functional component

I am attempting to pass a callback function to a functional component child in order to update the parent. However, I am encountering an error Uncaught TypeError: updateVal is not a function within the child component (I will add a comment to indicate wh ...

Accessing data from the subscribe method

When attempting to use certain values for graphs, I encountered an issue where the value returned becomes undefined once outside of the subscribe function. I have experimented with putting the values in an array of objects but still believe this is the co ...

Failure to trigger the before-prepareJSApp hook in nativescript-dev-webpack

I am currently facing some challenges while trying to bundle my Nativescript app using webpack. The first issue arises when I run the following command: tns build android --bundle After running this command, it seems like webpack is not doing anything. ...

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

Issue encountered during project upload due to deployment error

Make sure to wrap useSearchParams() in a suspense boundary on the "/auth-callback" page. For more information, check out https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout import React, { useEffect } from 'react'; import { useRou ...

Building an advanced numerical input validation system with Angular 7

In AngularJS, the following code is used to create a form with an input field of type "number": <form name="f" novalidate> <input type="number" ng-model="x" name="x"> </form> <div ng-if="f.x.$error.number">NaN</div> Is it p ...

Component inspired by Angular Mat-form-field

Hello fellow Angular developers, I'm currently working on creating reusable and composable components similar to Material's mat-form-field. My goal is to have a main component containing predefined components that can interact internally. Here i ...

Finding a date from a calendar with a readonly property in Playwright

Just starting out with the playwright framework after working with Protractor before. I'm trying to figure out the correct method for selecting a date in Playwright. selector.selectDate(date) //having trouble with this ...

Converting Firebase Querysnapshots into an Array of objects: A step-by-step guide

I am struggling to extract specific teams from a group of teams. It appears that I need to transform the snapshot into the Team object. Is there a simpler way to convert a snapshot into an array? As a beginner in firebase/angular, I may be overlooking some ...

Exploring the World of Angular 2 Modules

I've developed an application with various views, such as a spreadsheet and a two-panel view. Both of these views share common functionalities like navigation, searching, and filters. To streamline the process, I created a common module that is import ...

Exploring the implementation of initiating paypal in NestJs using Jest testing framework

Currently, I am creating a test for a method within NestJs that is responsible for initiating a Paypal Payment intent. When I execute either the yarn test:watch or simply yarn test command, the test described below runs successfully and passes. However, up ...

Unable to transition to a different module by leaping

I recently started using Angular 2 and followed all the instructions, but I'm having trouble loading a component from an external module. Plnkr - Take a look at this code. It's very simple with just 3 links and a few empty components (the app co ...

Ways to receive real-time notifications upon any modifications in my cloud firestore database?

I am currently in the process of developing a chat application using Angular and Firebase Cloud Firestore. My goal is to have a counter on the client side that updates whenever any document in the 'groups' collection is updated. Within my clien ...

The element called 'mat-menu-item' is unrecognized and not known

I am facing an issue with my Angular app, version 'Angular: 15.2.2'. I am trying to incorporate material design, but I am having trouble with the mat-menu-item not being recognized even though the mat-menu is recognized. @angular/[email prot ...