我已经使用Angular和Material设置了一个组件。我有想要使用BitBucket状态API正确公开的数据: https://bitbucket.status.atlassian.com/api#status
我只是停留在如何对列启用排序的功能上,我想使用默认设置对所有3种进行排序。任何方向将不胜感激。
这是一个堆叠闪电战:https://stackblitz.com/edit/angular-gmdy9j
HTML:
<div class="example-table-container">
<table mat-table [dataSource]="data"
matSort matSortActive="name" matSortDirection="desc">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
<td mat-cell *matCellDef="let row">{{row.status}}</td>
</ng-container>
<ng-container matColumnDef="created_at">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
Created
</th>
<td mat-cell *matCellDef="let row">{{row.created_at | date}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
TS:
import {HttpClient} from '@angular/common/http';
import {Component, ViewChild, AfterViewInit} from '@angular/core';
import {MatSort} from '@angular/material';
import {merge, Observable, of as observableOf} from 'rxjs';
import {catchError, map, startWith, switchMap} from 'rxjs/operators';
@Component({
selector: 'app-headlines',
templateUrl: './headlines.component.html',
styleUrls: ['./headlines.component.scss']
})
export class HeadlinesComponent implements AfterViewInit {
displayedColumns: string[] = ['name', 'status', 'created_at'];
tableDatabase: TableHttpDatabase | null;
data: BitBucketIssue[] = [];
resultsLength = 0;
isLoadingResults = true;
@ViewChild(MatSort) sort: MatSort;
constructor(private http: HttpClient) {}
ngAfterViewInit() {
this.tableDatabase = new TableHttpDatabase(this.http);
merge(this.sort.sortChange)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.tableDatabase!.getRepoIssues(
this.sort.active, this.sort.direction);
}),
map(data => {
// Flip flag to show that loading has finished.
this.isLoadingResults = false;
this.resultsLength = data.incidents.length;
console.log(data.incidents.length)
console.log(data.incidents)
return data.incidents;
}),
catchError(() => {
this.isLoadingResults = false;
return observableOf([]);
})
).subscribe(data => this.data = data);
}
}
export interface BitBucketApi {
incidents: BitBucketIssue[];
}
export interface BitBucketIssue {
name: string;
status: string;
created_at: number;
}
/** An example database that the data source uses to retrieve data for the table. */
export class TableHttpDatabase {
constructor(private http: HttpClient) {}
getRepoIssues(sort: string, order: string): Observable<BitBucketApi> {
const href = 'https://bqlf8qjztdtr.statuspage.io/api/v2/incidents.json';
const requestUrl =
`${href}?q=&sort=${sort}&order=${order}`;
return this.http.get<BitBucketApi>(requestUrl);
}
}
答案 0 :(得分:1)
似乎您没有将所需的 MatTableDataSource 链接到检索到的数据。 您需要将 MatSort 元素分配给 MatTableDataSource 排序属性,并使用 MatTableDataSource 显示数据。一切都在官方文档https://material.angular.io/components/sort/overview
中有详细记录语言代码
import {HttpClient} from '@angular/common/http';
import {Component, ViewChild, AfterViewInit} from '@angular/core';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import {merge, Observable, of as observableOf} from 'rxjs';
import {catchError, map, startWith, switchMap} from 'rxjs/operators';
/**
* @title Table retrieving data through HTTP
*/
@Component({
selector: 'table-http-example',
styleUrls: ['table-http-example.css'],
templateUrl: 'table-http-example.html',
})
export class TableHttpExample implements AfterViewInit {
displayedColumns: string[] = ['name', 'status', 'created_at'];
tableDatabase: TableHttpDatabase | null;
data: BitBucketIssue[] = [];
dataSource: MatTableDataSource<BitBucketIssue>;
resultsLength = 0;
isLoadingResults = true;
@ViewChild(MatSort) sort: MatSort;
constructor(private http: HttpClient) {}
ngAfterViewInit() {
this.tableDatabase = new TableHttpDatabase(this.http);
merge(this.sort.sortChange)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.tableDatabase!.getRepoIssues(
this.sort.active, this.sort.direction);
}),
map(data => {
// Flip flag to show that loading has finished.
this.isLoadingResults = false;
this.resultsLength = data.incidents.length;
console.log(data.incidents.length)
console.log(data.incidents)
return data.incidents;
}),
catchError(() => {
this.isLoadingResults = false;
return observableOf([]);
})
).subscribe(data => {
this.data = data; this.dataSource = new MatTableDataSource(data);
this.dataSource.sort = this.sort;
});
}
}
export interface BitBucketApi {
incidents: BitBucketIssue[];
}
export interface BitBucketIssue {
name: string;
status: string;
created_at: number;
}
/** An example database that the data source uses to retrieve data for the table. */
export class TableHttpDatabase {
constructor(private http: HttpClient) {}
getRepoIssues(sort: string, order: string): Observable<BitBucketApi> {
const href = 'https://bqlf8qjztdtr.statuspage.io/api/v2/incidents.json';
const requestUrl =
`${href}?q=&sort=${sort}&order=${order}`;
return this.http.get<BitBucketApi>(requestUrl);
}
}
HTML
<div class="example-table-container">
<table mat-table [dataSource]="dataSource"
matSort matSortActive="name" matSortDirection="desc">
<!-- Title Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>
<!-- State Column -->
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
<td mat-cell *matCellDef="let row">{{row.status}}</td>
</ng-container>
<!-- Created Column -->
<ng-container matColumnDef="created_at">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
Created
</th>
<td mat-cell *matCellDef="let row">{{row.created_at | date}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>