0
votes

I want to Display single object data using angular material table in a single column.

For example I have an object like

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
];


displayedColumns = ['name','position','weight','symbol'];

A single element in the object

I don't want the column names neither I want the data column wise

All I want is I want to render the data row wise one below the other.

so like if I have object of the zeroth Index of an array I want to display all its properties row wise in angular material data table how is it possible?

OR one can suggest any other component for angular material to display the object properties in a same fashion..!!

Here is my Html code for table

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!--- Note that these columns can be defined in any order.
        The actual rendered columns are set as a property on the row definition" -->

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

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

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
   
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
   
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

Appearance of Data must be like

  • first row must display position: 1
  • second row must display name: Hydrogen
  • third row must display weight:1.0079
  • fourth row must display symbol: 'H'

just mentioned here in bullets but I want like this all data in a single column

here is the link I performed the above code

https://stackblitz.com/edit/angular-vbpdqk?embed=1&file=src/app/table-basic-example.html

3
Where is your html code ? - Siju Samson
you do not want the column names only? Because normally data from array is displayed row wise like 0th, 1st, 2nd and so on. Or have I not clearly understood what you meant? - Wahab Shah
Idea is to only just to display the data one below the other using table not header just the data. - kushal Baldev
I edited the question hope its now clear..!! - kushal Baldev

3 Answers

1
votes

You can use ng-repeat-start and ng-repeat-end which looks like :

<table>
  <tr ng-repeat-start="element in items">
    <td>Position</td>
    <td>{{element.postition}}</td>
  </tr>
  <tr>
    <td>Name</td>
    <td>{{element.name}}</td>
  </tr>
  <tr>
    <td>Weight</td>
    <td>{{element.weight}}</td>
  </tr>
  <tr ng-repeat-end>
    <td>Symbol</td>
    <td>{{element.symbol}}</td>
  </tr>
</table>

which your result will be like below:

  • position: 1
  • name: 'Hydrogen'
  • weight:1.0079
  • symbol: 'H'
  • position: 2
  • name: 'Oxygen'
  • weight:15.999
  • symbol: 'O'

Hope this can help you.

1
votes

I was just able to sort out this problem by following just need to make tr instead of td in defination

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Position Column -->
  <ng-container matColumnDef="position">
   <tr mat-cell *matCellDef="let element"> Position : {{element.position}} </tr>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <tr mat-cell *matCellDef="let element"> Name:{{element.name}} </tr>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
   <tr mat-cell *matCellDef="let element">Symbol: {{element.symbol}} </tr>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <tr mat-cell *matCellDef="let element">Weight : {{element.weight}} </tr>
  </ng-container>

  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

for Additional we can add the border for each cell border bottom and even for odd an even row shades as we want..!!

0
votes

In your ts file your will need to define

displayedColumns = ['name','position','weight','symbol'];

Suggestion - dont use table, instead use something like below to print the values

<div *ngFor='let row of ELEMENT_DATA'>
  <ul>
    <li *ngFor='let obj of row | keyvalue'>{{obj.key}}:{{key.value}} </li>
   </ul>
</div>