0

In an input form, I am showing a list of textual values in drop down and storing an index value of the user selected item in the back end table.

For example, I have an array like this. In the input form, I am showing the name attribute in the dropdown, but storing value attribute in the table.

NEIGHBOURHOOD_TYPES = [
    {value: 1, name: 'School'},
    {value: 2, name: 'College'},
    {value: 3, name: 'Hospital'}
];

In a display screen of that same data, I am fetching the records from table and i am getting value 1,2,3, etc. I want to show its corresponding name attribute in a simple <td> element.

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

    <!-- Type Column -->
    <ng-container matColumnDef="type">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Type </th>
      <td mat-cell *matCellDef="let row"> {{row.neighbourhoodType}} </td>
    </ng-container>

showing 1,2,etc. But I want it to show its corresponding name attribute.

what is the simplest and best option to do it?

Refer the screen shot of the usage screen

(sorry for this naive question, but I am new to angular.).

2 Answers 2

0

Use *ngFor attribute.

<table>
    <tr *ngFor="let item of NEIGHBOURHOOD_TYPES">
        <td>{{item.value}} +' : '+{{item.name}}</td>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

I do not want to show all values of that array, rather want to show only the item that user has saved in the backed table. (please refer my updated edit and screenshot and code snippet)
0

Got an answer from these two threads.

https://stackoverflow.com/questions/42580100/typescript-take-object-out-of-array-based-on-attribute-value

Call TypeScript function in HTML without a click event

    <ng-container matColumnDef="type">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Type </th>
      <td mat-cell *matCellDef="let row"> {{getNeighbourhoodTypeName(row.neighbourhoodType)}} </td>
    </ng-container>

Had a write a tiny function in the ts file

  getNeighbourhoodTypeName(val):string {
    return this.NEIGHBOURHOOD_TYPES.find(nType => nType.value === val).name;
  }

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.