0

我在我的角度应用程序中有一个可以正确显示和渲染的 mattable,但排序不起作用。当我单击时播放标题的动画,但没有任何反应。我正在使用 augury 进行调试,我可以看到正在设置排序升序和降序属性。有一个名为 _arrowDescending 的属性设置为 asc 或 des 来表示顺序,这是正确的,除了排序能力之外,一切看起来都很好。这是我的文件:

应用模块.ts:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule }   from '@angular/forms';
import {MatButtonModule, MatCheckboxModule, MatTableModule, MatSortModule,} from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';


import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    MatTableModule,
    MatSortModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

import { Component, OnInit, ViewChild } from '@angular/core';
import {MatSort, MatTableDataSource} from '@angular/material';

export interface WorkspaceCase {
  caseNumber: string;
  subject: string;
  typeology: string;
  caseType: string;
  disposition: string;
  daysOpen: number;
  sarDays: number;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  selectedStatus:string;
  displayedColumns: string[] = ['caseNumber', 'subject', 'typeology', 'caseType', `disposition`, `daysOpen`, `sarDays`];
  assignedCases : WorkspaceCase[] = [{caseNumber: "123", subject: "test", typeology: "test", caseType: "test", disposition: "test", daysOpen: 2, sarDays: 4},
  {caseNumber: "12s3", subject: "tesst", typeology: "stest", caseType: "tesst", disposition: "tesst", daysOpen: 22, sarDays: 24}];
  dataSource;
  //filteredCases = [];

  @ViewChild(MatSort) sort: MatSort;
  ngAfterViewInit() {
    this.dataSource.sort = this.sort;
  }
  ngOnInit() {
    this.dataSource = new MatTableDataSource(this.assignedCases);

  }

  onFilterChange(newValue) {
    this.selectedStatus = newValue;
    this.applyFilter(this.selectedStatus);
    alert(this.selectedStatus );
  }

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

应用组件.html

<!-- MY WORKSPACE-->
<div class="row align-items-center ods-margin__top--large">
        <div class="col-md-10">
           <h2 class="ods-text__body--medium "><span class="opticon opticon-home ods-margin__right--tiny"></span>My Workspace</h2>
        </div>
        <div class="col-md-2">
           <div class="ods-form-field select__group ">
              <div class="ods-select__wrapper">
                 <select [ngModel]="selectedStatus" (ngModelChange)="onFilterChange($event)" class="ods-select__input" id="">
                 <option>Filter By Status</option>
                 <option>Status 1</option>
                 <option>Status 2</option>
                 <option>Status 3</option>
                 <option>Status 4</option>
                 </select>
              </div>
           </div>
        </div>
     </div>
     <div class="row">
        <div class="col-md-12">
           <div id="my_workspace" class="table-area">
              <mat-table [dataSource]= "assignedCases" matSort class="mat-elevation-z8">
                 <ng-container matColumnDef="caseNumber">
                    <mat-header-cell *matHeaderCellDef mat-sort-header> Case Number </mat-header-cell>
                    <mat-cell *matCellDef="let case"> {{case.caseNumber}} </mat-cell>
                 </ng-container>
                 <ng-container matColumnDef="subject">
                    <mat-header-cell *matHeaderCellDef mat-sort-header> Subject </mat-header-cell>
                    <mat-cell mat-cell *matCellDef="let case"> {{case.subject}} </mat-cell>
                 </ng-container>
                 <ng-container matColumnDef="typeology">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> Typeology </th>
                    <td mat-cell *matCellDef="let case"> {{case.typeology}} </td>
                 </ng-container>
                 <ng-container matColumnDef="caseType">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> Case Type </th>
                    <td mat-cell *matCellDef="let case"> {{case.caseType}} </td>
                 </ng-container>
                 <ng-container matColumnDef="disposition">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> Disposition </th>
                    <td mat-cell *matCellDef="let case"> {{case.disposition}} </td>
                 </ng-container>
                 <ng-container matColumnDef="daysOpen">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> Days Open </th>
                    <td mat-cell *matCellDef="let case"> {{case.daysOpen}} </td>
                 </ng-container>
                 <ng-container matColumnDef="sarDays">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header> Days To File Sar </th>
                    <td mat-cell *matCellDef="let case"> {{case.sarDays}} </td>
                 </ng-container>
                 <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
                 <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
              </mat-table>
              <div class="ods-margin--tiny">
                 <a id="table_expand"><span class="opticon opticon-arrowdown"></span></a>
              </div>
           </div>
        </div>
     </div>
     <!-- MY WORKSPACE (END)-->

app.component.css

    @import "../../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";

body { 
  font-family: Roboto, Arial, sans-serif;
  margin: 0;
}

.basic-container {
  padding: 30px;
}

.version-info {
  font-size: 8pt;
  float: right;
}
4

2 回答 2

0

代替

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

尝试在构造函数或 ngOnInit 中定义排序。

ngOnInit(){
        this.dataSource.sort = this.sort;
}
于 2018-07-13T13:34:12.170 回答
0

您正在正确初始化数据源,但实际上并未在 HTML 模板中使用它。

  <mat-table [dataSource]= "assignedCases" matSort class="mat-elevation-z8">

应该

  <mat-table [dataSource]= "dataSource" matSort class="mat-elevation-z8">
于 2021-02-02T13:35:10.277 回答