代码之家  ›  专栏  ›  技术社区  ›  anuj kumar

角度Mat分页和Mat排序不起作用

  •  1
  • anuj kumar  · 技术社区  · 2 年前

    enter image description here

    Mat分页(显示0的0)和Mat排序不起作用。有人能帮忙吗?我尝试了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"
    }
    

    这是 我的组件.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>
    

    这是 组件.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;
      });
    }
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   Yong Shun    2 年前

    分配 MatPaginator MatSort 实例到 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

    旁注: 您可以访问 body 的字段 response .对(JSON)对象进行字符串化并将字符串转换回JSON是不必要的。

    const jsonBody = response['body'];