Angular 无法使用角度材质对表格进行排序2

Angular 无法使用角度材质对表格进行排序2,angular,angular-material2,lifecycle,angular-cdk,Angular,Angular Material2,Lifecycle,Angular Cdk,我有一张有棱角的材料桌子。HTML格式如下: 委员会甄选 提交的节目 凸轮检查名称 {{element.name} 创建日期 {{element.cdate |日期:'mediumDate'} 创建人 {{element.createdby} 凸轮ID {{element.camsid} 地位 {{element.sqldbinfo.status} 我正在创建一个表并希望对其进行排序,但不幸的是,数据正在呈现,但排序没有进行 从'@angular/core'导入{Component,View

我有一张有棱角的材料桌子。HTML格式如下:


委员会甄选
提交的节目
凸轮检查名称
{{element.name}
创建日期
{{element.cdate |日期:'mediumDate'}
创建人
{{element.createdby}
凸轮ID
{{element.camsid}
地位
{{element.sqldbinfo.status}
我正在创建一个表并希望对其进行排序,但不幸的是,数据正在呈现,但排序没有进行

从'@angular/core'导入{Component,ViewChild,OnInit};
从“@angular/material”导入{MatTableDataSource,MatSort};
从'@angular/cdk/collections'导入{DataSource};
从“../services/cams.service”导入{CamsService};
从“../actauth.service”导入{ActauthService};
从“../services/local data.service”导入{LocalDataService};
从“../services/act.service”导入{ActService};
从“../act modal/act modal service.service”导入{ActModalService};
@组成部分({
选择器:“应用程序camslist”,
templateUrl:'./camslist.component.html',
样式URL:['./camslist.component.less']
})
导出类CamslistComponent实现OnInit{
displayedColumns=['name','cdate','createdby','camsid','status'];
数据源;
StatusText:数组;
SubmittedButtonShowHide:布尔值;
建造师(
私人摄像机服务:摄像机服务,
私有身份验证:ActauthService,
专用localDataService:localDataService,
私人actService:actService,
专用modalService:ActModalService){
}
@ViewChild(MatSort)排序:MatSort;
showSubmitted:boolean=false;
恩戈尼尼特(){
this.auth.getMyLogon().subscribe(
res=>{
res.EmployeeId=29007154;
此.camsService.all(res).订阅(
凸轮=>{
如果(凸轮长度==0){
this.modalService.openNoCamsIdMsgModal();
}否则{
this.dataSource=新MatTableDataSource(cams);
this.dataSource.sort=this.sort;
};
})
})
}
//ngAfterViewInit(){
// }
applyFilter(filterValue:string){
this.dataSource.filter=filterValue;
}
toggleSubmitted(){
this.showSubmitted=!this.showSubmitted;
}
检查状态(){
常量数组=this.dataSource.data;
//tslint:禁用下一行:forin
for(数组中的常量st){
this.StatusText=array[st].sqldbinfo.status;
如果(!(此.StatusText.includes('Submitted',0))){
返回this.SubmittedButtonShowHide=true;
}否则{
返回this.SubmittedButtonShowHide=false;
}
}
}
设置当前凸轮(选择凸轮){
//测试数据
selcams.camsid=66502;
this.actService.tabs.cams=selcams;
this.localDataService.setLocalStorage(“selcams”,selcams);
}
}
导出接口元素{
名称:字符串;
cdate:字符串;
createdby:string;
camsid:编号;
状态:字符串;
}

这是一个稳定的版本,其中,数据被渲染,但排序没有发生。每当我在顶级级别添加带有OnInitAfterViewInit时,它总是给我一个错误。请帮帮我。

你的代码应该是这样的

ngAfterViewInit() { this.dataSource.sort = this.sort; }
您的数据源声明如下:

 dataSource;
您可以使用HTTP调用来填充它:

this.auth.getMyLogon().subscribe(
  res => {
    res.EmployeeId = 29007154;
    this.camsService.all(res).subscribe(
      cams => {
        if (cams.length == 0) {
          this.modalService.openNoCamsIdMsgModal();
        } else {
          this.dataSource = new MatTableDataSource(cams);
          this.dataSource.sort = this.sort;
        };
      })
  })
错误

无法在callProviderLifecycles(core.js:12706)的camslist component.ngAfterViewInit(camslist.component.ts:48)中设置未定义的属性“sort”


源于在HTTP调用结束之前运行
ngAfterViewInit
。因此,您的数据源没有实例化,因此出现了错误

“它总是给我一个错误”…这是??你的文章必须全部引用。你的分类在哪里?您的意思是不能通过单击列标题进行排序,还是在创建数据源之前不能对数据进行排序?(顺便说一句,您的
ngAfterViewInit
错误源于您没有实现AfterViewInit接口。您的类应该从以下内容开始:
导出类CamslistComponent实现OnInit,AfterViewInit
(别忘了导入它))这是在类级别导入AfterViewInit并将其添加到Oninit之后出现的错误:*无法在callProviderLifecycles(core.js:12706)的CamslistComponent.ngAfterViewInit(camslist.component.ts:48)中设置未定义的属性“sort”**@trichetriche我完全像你一样导入。你完全正确。你能帮我一个解决方案吗。删除你的
ngAfterViewInit
?我仍在等待关于我对你问题的第一次评论的答案,如果我不知道你的问题,我无法给你一个问题的答案:
你的排序在哪里?你的意思是你可以吗“不能通过单击列标题进行排序,或者在创建数据源之前不能对数据进行排序?
即使是在ngOnInit中编写的排序代码也不能正常工作
this.datasource=new MatTableDataSource(cams);this.dataSource.sort=this.sort;
round
this.dataSource.sort=this.sort;
带超时:
setTimeout(()=>this.dataSource.sort=this.sort)
。在表的每个标题旁边都有箭头符号,单击它应该排序