【问题标题】:Sorting Not Working Angular Material Design Table排序不工作的角材料设计表
【发布时间】:2018-04-27 12:41:04
【问题描述】:

使用 Angular Material Design 表,我只是想对数据表中两列的标题进行排序。 (它们是下面代码中的 Employee Name 和 Job Title 列。)两者都没有排序。不知道为什么。这是我的代码,谢谢,CM

员工表.component.html

<div>

  <mat-table [dataSource] = "dataSource" matSort>
    <ng-container matColumnDef="photo">
      <mat-header-cell *matHeaderCellDef>Profile</mat-header-cell>
      <mat-cell *matCellDef="let employee"><img width = "100" height = "100" src = "../assets/images/{{employee.username}}.jpg" >
      </mat-cell>
    </ng-container>

    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef mat-sort-header>Employee Name</mat-header-cell>
      <mat-cell *matCellDef="let employee">{{employee.name}}</mat-cell>
    </ng-container>


    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef mat-sort-header>Job Title</mat-header-cell>
      <mat-cell *matCellDef="let employee">{{employee.position}}</mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns" color="primary"></mat-header-row>
    <mat-row *matRowDef="let row; columns:displayedColumns"></mat-row>

  </mat-table>
</div>

employee-table.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {MatSort, MatSortable, MatTableDataSource} from '@angular/material';
import {EmployeeService} from '../employee.service';

@Component({
  selector: 'app-employee-table',
  templateUrl: './employee-table.component.html',
  styleUrls: ['./employee-table.component.css']
})
export class EmployeeTableComponent implements OnInit {
  @ViewChild(MatSort) sort: MatSort;
  dataSource;
  displayedColumns = ['photo', 'name', 'position'];

  constructor() { }
  ngOnInit() {   
    this.dataSource =[
            {
              "id": 1,
              "name": "Leanne Grahamz",
              "username": "Bret",
              "position": "Software Developer"
            },
            {
              "id": 2,
              "name": "Ervin Howell",
              "username": "Antonette",
              "position": "Graphic Designer"
            },
            {
              "id": 3,
              "name": "Clementine Bauch",
              "username": "Samantha",
              "position": "Front End Developer"
            },
            {
              "id": 4,
              "name": "Patricia Lebsack",
              "username": "Karianne",
              "position": "Full Stack Developer"
            },
            {
              "id": 5,
              "name": "Chelsey Dietrich",
              "username": "Kamren",
              "position": "Database Administrator" 
            }
    ]; //End data object

    this.dataSource.sort = this.sort;

  }//End ng onInit

}//End class EmployeeTableComponent

【问题讨论】:

  • 您还想看什么帕思·戈达尼?我很乐意编辑我的帖子。

标签: angular angular-material-5


【解决方案1】:

您在生命周期中过早地设置了数据源排序 this.dataSource.sort = this.sort;。正如the material.angular.io examples 所建议的,您应该在视图初始化之后设置数据源排序:

复制自material.angular.io example

 /**
   * Set the sort after the view init since this component will
   * be able to query its view for the initialized sort.
   */
  ngAfterViewInit() {
    this.dataSource.sort = this.sort;
  }

您还应该编辑您的数据源初始化:

ngOnInit() {
 this.dataSource = new MatTableDataSource([{
     "id": 1,
     "name": "Leanne Grahamz",
     "username": "Bret",
     "position": "Software Developer"
    }, 
    ...
    {
     "id": 5,
     "name": "Chelsey Dietrich",
     "username": "Kamren",
     "position": "Database Administrator"
    }
   ]);
}

Here is an edit of the initial example with your code.

【讨论】:

  • 感谢 ibenjelloun 的回复。我认为这可能与对数据源进行排序的那条线有关。那么我应该从 ngOnInit 中删除该行并在 ngOnInit 之后将调用 ngAfterView 并在其中包含该行吗?
  • 排序是唯一需要在ngAfterViewInit 中的行,因为您正在调用由模板初始化的this.sort。我之前没有看到它,但您还需要将数据源指定为MatTableDataSource。您在代码中调用的数据源实际上是角度材料示例中的ELEMENT_DATA
  • 如果你有时间可以编辑你的答案,让我看看你在说什么?
  • 非常感谢您花时间向我展示如何执行此操作。我的桌子现在正在排序。我理解你所做的改变。非常感谢这个论坛以及所有花时间分享他们的专业知识的人,以便世界上会有其他专家......
【解决方案2】:

或者,您也可以使用超时进行排序(如果需要,还可以分页),例如:

setTimeout(() => {
  this.tableDataSource.sort = this.sort;
  this.tableDataSource.paginator = this.paginator;
}, 0);

【讨论】:

  • 我不认为设置没有任何时间值的超时将在这里有所帮助。
  • 你说得对,我忘了设置值(现在编辑)。 0 就足够了,因为我们只想将它移到事件队列的末尾。
猜你喜欢
  • 2019-01-28
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2015-12-28
相关资源
最近更新 更多