Какую логику я должен использовать для сортировки своих элементов?
Здесь я генерирую все столбцы и строки.
dynamic-table.component.html
<table>
<tr>
<th *ngFor="let col of columns" (click)="onClickSort()">{{col}}</th>
</tr>
<tr *ngFor="let user of users">
<td *ngFor="let col of columns">{{user[col]}}</td>
</tr>
</table>
dynamic-table.component.ts
import {Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-dynamic-table',
templateUrl: './dynamic-table.component.html',
styleUrls: ['./dynamic-table.component.css']
})
export class DynamicTableComponent implements OnInit {
@Input()
users = [];
@Input()
columns: string[];
constructor() {
}
onClickSort() {
//ADD LOGIC SORT
}
ngOnInit() {
}
}
Мои данные находятся в mock.ts
, я могу найти их в моем сервисе.
app.component.ts
import {Component, OnInit } from '@angular/core';
import {TableService} from './table.service';
@Component({
selector: 'app-root',
styleUrls: ['./app.component.css'],
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
users;
columns;
constructor(private atService: TableService) {
}
ngOnInit(): void {
this.columns = this.atService.getColumns();
this.atService.getUsers().subscribe((res) => this.users = res);
}
}