【问题标题】:Mat-table, mat-sort is not working properly垫表,垫排序无法正常工作
【发布时间】:2018-06-04 12:41:26
【问题描述】:

我尝试在我的项目中在 mat-table 中实现 mat-sort。开始我的项目后,我看到“排序”图标,可以点击它,但我的结果中没有任何排序,总是相同的视图。

组件文件:

@Component({
  selector: 'app-test-component',
  templateUrl: './testComponent.component.html',
  styleUrls: ['./testComponent.component.css'],
  providers: [TestService],
})

export class TestComponent implements OnInit, OnDestroy, AfterViewInit {

  displayedColumns = ['Column1'];

  dataSource = new MatTableDataSource<UserModel>();
  private exDisplayNames: Subscription;


  @ViewChild(MatSort) sort: MatSort;

  constructor(private testService: TestService) {   }

  ngOnInit() {
    this.exDisplayNames = this.testService.userNamesChanged.subscribe((userNameData: UserModel[]) => {
      this.dataSource.data = userNameData;
    });
    this.testService.fetchUserName();
  }

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

  ngOnDestroy() {

    if (this.exDisplayNames) {
        this.exDisplayNames.unsubscribe();
    }
  }

 }

服务文件:

@Injectable()
export class TestService {


    userNamesChanged = new Subject<UserModel[]>();
    private availableUserName: UserModel[] = [];


    private fbSubs: Subscription[] = [];

    constructor(private db: AngularFirestore) { }


    fetchUserName() {
        this.fbSubs.push(this.db.collection('names/')
            .snapshotChanges()
            .map(docArray => {
                return docArray.map(doc => {
                    return {
                        displayUserName: doc.payload.doc.data().nick,
                    };
                });
            })
            .subscribe((userData: UserModel[]) => {
                this.availableUserName = userData;
                this.userNamesChanged.next([...this.availableUserName]);
            }, error => {
                console.log(error);
            }));
    }

}

html 文件:

<section fxLayoutAlign="center">
  <mat-card>
    <mat-card-content>
      <mat-table [dataSource]="dataSource" matSort>

        <ng-container matColumnDef="Column1">
          <mat-header-cell *matHeaderCellDef mat-sort-header>Nick</mat-header-cell>
          <mat-cell fxLayout="row" fxLayoutAlign="center center" *matCellDef="let element">{{ element.displayUserName}}</mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
      </mat-table>
    </mat-card-content>
  </mat-card>
</section>

还导入了所有必要的材料模块:MatSortModule, MatTabsModule

我的步骤是: 1. 组件新增:@ViewChild(MatSort) sort: MatSort。 2.实现AfterViewInit。 3. 补充:

ngAfterViewInit() {
   this.dataSource.sort = this.sort;
 }
  1. 在html文件中添加:&lt;mat-table&gt;中的matSort。
  2. 将 mat-sort-header 添加到 &lt;mat-cell-header&gt;

就像我写的:我在列上看到排序图标,但点击后 - 没有任何变化,没有任何排序。

【问题讨论】:

  • 你能为此创建一个 plunkr/jsfiddle 吗?

标签: html angular typescript material-design


【解决方案1】:

我不确定您的 UserModel 的外观如何,但我认为它不包含属性 Column1

matColumnDef="Column1"

将该部分更改为 matColumnDef="displayUserName" 或您在模型中拥有的任何属性。

还要改displayedColumns = ['displayUserName'];

来自docs

默认情况下,MatTableDataSource 排序时假定已排序列的名称与列显示的数据属性名称匹配。例如,以下列定义命名为 position,与行单元格中显示的属性名称匹配。

【讨论】:

  • matColumnDef 与显示的列名称相连。
  • 谢谢!!这与 datasource.sortingDataAccessor 一起处理我的数据源(.orderline.lineNumber)中的嵌套对象终于解决了我的问题。
【解决方案2】:
ngOnInit() {
  this.exDisplayNames = this.testService.userNamesChanged.subscribe((userNameData: UserModel[]) => {
    this.dataSource = new MatTableDataSource(userNameData);
    setTimeout(() => this.dataSource.sort = this.sort);
  });
  this.testService.fetchUserName();
}
  1. 您的排序器在视图初始化时绑定到您的数据源,但它不会等待您的请求。
  2. 您应该创建一个数据源的新实例并停止在属性中摆弄。 Material 提供了高层次的抽象,利用它。

注意:在我的解决方案中,您可以看到超时,它用于更改检测。你可能不需要它,我只是把它放在那里只是为了确定。

【讨论】:

  • 这个解决方案中的超时完全解决了我的问题。谢谢!
  • ngAfterContentChecked() { this.dataSource.sort = this.sort } 在这种情况下这也是一个修复。
猜你喜欢
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多