enter image description here

Mat Pagination(显示0/0)和Mat排序不起作用.有人能帮忙吗?我try 了Stack OverFlow上可用的其他解决方案,但这似乎对我不起作用.

来自API的示例数据:

{
    "body": {
    "httpstatuscode": 200,
    "AppData": [
        {
            "disk": "0",
            "load": "0.25",
            "sno": 1,
            "instances": "Instance1",
            "cpu": "0",
            "ram": "0.25"
        },
        {
            "disk": "0",
            "load": "0",
            "sno": 2,
            "instances": "Instance2",
            "cpu": "0",
            "ram": "0"
        }
    ],
    "opStatusCode": 2000,
    "type": "SUCCESS",
    "message": "DATA RECEIVED SUCCESSFULLY"
}

这是my component.html代码:

<div>  
  <table style="position: sticky; overflow: scroll;"
    mat-table
    matSort
    [dataSource]="dataSource">
    <!--- Note that these columns can be defined in any order.
          The actual rendered columns are set as a property on the row definition" -->

    <ng-container matColumnDef="sno">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>SNO</th>
      <td mat-cell *matCellDef="let element">{{ element.sno }}</td>
    </ng-container>

    <ng-container matColumnDef="instances">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Instances</th>
      <td mat-cell *matCellDef="let element">{{ element.instances }}</td>
    </ng-container>

    <ng-container matColumnDef="cpu">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>CPU %</th>
      <td mat-cell *matCellDef="let element">{{ element.cpu }}</td>
    </ng-container>

    <ng-container matColumnDef="ram">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>RAM %</th>
      <td mat-cell *matCellDef="let element">{{ element.ram }}</td>
    </ng-container>

    <ng-container matColumnDef="load">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Load %</th>
      <td mat-cell *matCellDef="let element">{{ element.load }}</td>
    </ng-container>

    <ng-container matColumnDef="disk">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Disk %</th>
      <td mat-cell *matCellDef="let element">{{ element.disk }}</td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <!-- <tr
      mat-row
      class="remove-background"
      (mouseover)="onMouseOver(e)"
      *matRowDef="let row; let e = index; columns: displayedColumns"
      [ngClass]="{ 'mat-elevation-z8': e == mouseOverIndex, 'background-color': blue }"
    ></tr> -->
    <tr (click)="fetchProxyData(row.instances)" mat-row *matRowDef="let row; let e = index; columns: displayedColumns;"></tr>

    <!-- Row shown when there is no matching data. -->
    <tr class="mat-row" *matNoDataRow>
      <td class="mat-cell" colspan="4">
        <!-- No data matching the filter "{{ input.value }}" -->
        Data not available.
      </td>
    </tr>
  </table>

  <mat-paginator  #paginator
    [pageSizeOptions]="[5, 10, 25, 100, 150, 200]"
    aria-label="Select page of users" class="mat-paginator-sticky"
  ></mat-paginator>
</div>

这是component.ts文件.由于包含一些敏感细节,仅共享其中的部分部分.

export interface tableData {
  sno: number;
  instances: string;
  cpu: number;
  ram: number;
  load: number;
  disk: number;
}

dataForTable: tableData[] = []; 
cpuData=[]; 
ramData=[]; 
loadData=[]; 
diskData=[];

displayedColumns: string[] = ['sno', 'instances', 'cpu', 'ram', 'load', 'disk'];
dataSource = new MatTableDataSource(this.dataForTable);

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;

constructor(private httpclient: HttpClient, private httpService: HttpService) {}

ngAfterViewInit() { 
  this.dataSource.paginator = this.paginator; 
  this.dataSource.sort = this.sort; 
}

ngOnInit(): void { 
  this.fetchData(); 
}

fetchData() {

  this.httpService.getData(request).subscribe((response: any) => {
    const responseString: string = JSON.stringify(response);
    try {
      const jsonObj = JSON.parse(responseString);
      const jsonBody = jsonObj['body'];
      const appData = jsonBody['AppData'];
      this.dataForTable = appData as tableData[];
      this.dataSource=new MatTableDataSource(this.dataForTable);
    } catch (e: any) {
      console.log('Error in parsing json ' + e.message);
    }
    return response;
  });
}

推荐答案

提供数据后,将MatPaginatorMatSort实例分配给MatTableDataSource.

fetchData() {
  this.httpService.getData(request).subscribe((response: any) => {

    try {
      const jsonBody = response['body'];
      const appData = jsonBody['AppData'];
      this.dataForTable = appData as tableData[];
      this.dataSource = new MatTableDataSource(this.dataForTable);

      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
    } catch (e: any) {
      console.log('Error in parsing json ' + e.message);
    }
});

Demo @ StackBlitz

Side note:您可以从response访问body字段.字符串化(SON)对象并将字符串转换回SON是不必要的.

const jsonBody = response['body'];

Typescript相关问答推荐

如何传递基于TypScript中可变类型的类型?

是否可以根据嵌套属性来更改接口中属性的类型?

如何根据另一个属性的布尔值来缩小函数属性的返回类型?

具有泛型类方法的类方法修饰符

如何在TypeScrip中正确键入元素和事件目标?

如何消除在Next.js中使用Reaction上下文的延迟?

如何在Type脚本中动态扩展函数的参数类型?

为什么不能使用$EVENT接收字符串数组?

在列表的指令中使用intersectionObservable隐藏按钮

从对象类型描述生成类型

通过泛型函数本身推断受约束的泛型参数的类型脚本问题

正交摄影机正在将渲染的场景切成两半

类型';字符串|数字';不可分配给类型';未定义';.类型';字符串';不可分配给类型';未定义';

缩小对象具有某一类型的任意字段的范围

TS2739将接口类型变量赋值给具体变量

如何在具有嵌套数组的接口中允许新属性

Select 类型的子项

是否可以将类型参数约束为不具有属性?

为什么类方法参数可以比接口参数窄

为什么过滤器不改变可能的类型?