Currently, I am working with two arrays that share a mutual ID, allowing me to connect them. However, I am facing a challenge with filtering.
In the first array, there is a list of items with checkboxes next to each item. When I select a checkbox, I want to filter and display corresponding items from the second array in a table by matching the mutual ID.
The issue arises when I perform multiple selections. I expect to see multiple results, but due to using the .filter method, I only get one result (the last selected checkbox).
If I select checkboxes for Item 1 and Item 2, I anticipate seeing 3 results (ids 10, 20(x2)) from the 'apps' array in the lower table. And if I deselect a checkbox, the corresponding results should be removed from the table.
let items = [
{id:1, name:'Item 1', appId:10},
{id:2, name:'Item 2', appId:20},
{id:3, name:'Item 3', appId:20},
{id:4, name:'Item 4', appId:30}
]
let apps = [
{id:10, address:'Some street 1', city:'City 1'},
{id:20, address:'Some street 2', city:'City 2'},
{id:20, address:'Some street 2', city:'City 2'},
{id:30, address:'Some street 3', city:'City 3'}
]
this.dataSource = this.items.filter(x => x.appId == apps.id)
https://i.sstatic.net/4OpRR.png
Thank you.
UPDATE
With Rohit's solution, I have successfully achieved my initial goal. However, a new challenge has emerged - selecting a checkbox in the first array clears all previously selected checkboxes in the second array. Any suggestions on how to address this issue?
I have included before and after images. After selecting the checkbox for Item 1, I expect the checkboxes for Some street 9 and Some street 10 to remain selected.
Here is my HTML code:
<table mat-table [dataSource]="dataSourceObjects" class="mat-elevation-z8">
<ng-container matColumnDef="objectChBox">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element">
<mat-checkbox
class=""
[checked]="element.chbxActive"
(change)="checkSingleObject($event, element)"
>
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="address">
<th mat-header-cell *matHeaderCellDef>Address</th>
<td mat-cell *matCellDef="let element">{{element.address}}</td>
</ng-container>
<ng-container matColumnDef="city">
<th mat-header-cell *matHeaderCellDef>City</th>
<td mat-cell *matCellDef="let element">{{element.city}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumnsObjects"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumnsObjects;"></tr>
</table>
https://i.sstatic.net/2SOeX.png https://i.sstatic.net/gKLXW.png