Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度材料数据表未排序?_Javascript_Angular_Typescript_Angular Material - Fatal编程技术网

Javascript 角度材料数据表未排序?

Javascript 角度材料数据表未排序?,javascript,angular,typescript,angular-material,Javascript,Angular,Typescript,Angular Material,我已经添加了MatSortModule,在类中实现了@ViewChild,添加了指令,设置了dataSource.sort属性等。当我鼠标悬停时,我得到了表上的箭头,但数据没有排序 想法 import { Component, OnInit, ViewChild } from '@angular/core'; import { MatTableDataSource, MatSort } from "@angular/material"; class Todo {

我已经添加了MatSortModule,在类中实现了
@ViewChild
,添加了指令,设置了dataSource.sort属性等。当我鼠标悬停时,我得到了表上的箭头,但数据没有排序

想法

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

    class Todo {
      id: string;
      description: string;
      complete: boolean;
    }
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
      @ViewChild(MatSort, {static: false}) sort: MatSort;

      /**
      * Control column ordering and which columns are displayed.
      */
      displayedColumns:string[] =  ['id'];
      dataSource: MatTableDataSource<Todo>;

      ngOnInit() {
        const todos: Todo[] = [
          { id: '123', description: 'Complete me!', complete: false },
          { id: '321', description: 'You Completed me!', complete: true }];
        this.dataSource = new MatTableDataSource(todos);
        this.dataSource.sort = this.sort;
      }
    }


    <mat-table class="mat-elevation-z8" [dataSource]="dataSource" matSort>
      <ng-container matColumnDef="id">
        <mat-header-cell *matHeaderCellDef mat-sort-header>ID</mat-header-cell>
        <mat-cell *matCellDef="let row;">{{row.id}}</mat-cell>
      </ng-container>
      <mat-header-row *matHeaderRowDef="displayedColumns">
      </mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
    </mat-table>
从'@angular/core'导入{Component,OnInit,ViewChild};
从“@angular/material”导入{MatTableDataSource,MatSort};
待办事项{
id:字符串;
描述:字符串;
完全:布尔;
}
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
@ViewChild(MatSort,{static:false})sort:MatSort;
/**
*控制列顺序以及显示哪些列。
*/
displayedColumns:string[]=['id'];
数据源:MatTableDataSource;
恩戈尼尼特(){
常量待办事项:待办事项[]=[
{id:'123',description:'Complete me!',Complete:false},
{id:'321',description:'youcompleted me!',complete:true}];
this.dataSource=新MatTableDataSource(todos);
this.dataSource.sort=this.sort;
}
}
身份证件
{{row.id}}

由于视图尚未初始化,因此
MatSort
视图子对象在
ngOnInit
处是
未定义的。要解决此问题,请在使用
ngAfterViewInit
生命周期挂钩初始化视图后,在
数据源上设置
MatSort

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

我也遇到了同样的问题,您需要设置MatSort。 我就是这样修好的

@ViewChild(MatSort, {static: false}) set  sortMeeting (ms: MatSort){
this._sort = ms;
this.renewDataSource(); }

ngOnInit(){
this.dataSource = new MatTableDataSource(todos);
this.renewDataSource();
}


renewDataSource() {
this.dataSource.sort = this._sort;
}

我希望这能有所帮助

我们必须等到
AfterViewInit
之后,才能确保
sortDirective
已完全初始化。