为什么垫子分类器和分页器不起作用?

时间:2019-01-14 11:35:11

标签: angular typescript angular-material

我有一个mat-table,可以使用Api从表中获取列列表。 数据正确显示,但是我不能在垫子桌上使用垫子分类和分页。 尽管我通过参考material.angular.io网站使用了排序和分页技术。但仍然无法对表格进行排序或分页。

我的HTML代码:

<mat-table #table [dataSource]="dataSource" matSortclass="mat-cell">                       
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef></mat-header-cell>
    <mat-cell *matCellDef="let row"></mat-cell>
  </ng-container>

  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef mat-sort-header>
      Name
    </mat-header-cell>
    <mat-cell *matCellDef="let row" 
              style="cursor:pointer;" 
              (click)="proceed(row)">
      {{row.projectName}}
    </mat-cell>
  </ng-container>

  <ng-container matColumnDef="description">
    <mat-header-cell *matHeaderCellDef mat-sort-header>
      Description
    </mat-header-cell>
    <mat-cell *matCellDef="let row">
      {{row.projectDescription}}
    </mat-cell>
  </ng-container>

  <ng-container matColumnDef="actions">
    <mat-header-cell *matHeaderCellDef></mat-header-cell>
    <mat-cell *matCellDef="let row; let i = index">
      <button mat-icon-button 
              color="accent" 
              matTooltip="Edit"
              (click)="startEdit(row)">
        <mat-icon aria-label="Edit">edit</mat-icon>
      </button>
      <button mat-icon-button
              color="#b71c1c"
              matTooltip="Delete"
              (click)="deleteItem(row)">
        <mat-icon aria-label="Delete">
          delete
        </mat-icon>
      </button>
    </mat-cell>
  </ng-container>

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

<mat-paginator #paginator 
               [length]="dataSource" 
               [pageIndex]="0" 
               [pageSize]="10" 
               [pageSizeOptions]="[5, 10, 25, 100]">
</mat-paginator>

我的TypeScript代码:-

import {
Component,
ElementRef,
OnInit,
ViewChild,
EventEmitter,
Output
} from "@angular/core";
import { Router, ActivatedRoute, ParamMap } from "@angular/router";


import { SelectionModel } from "@angular/cdk/collections";
import {
MatTableDataSource,
MatSort,
MatDialog,
MatPaginator
} from "@angular/material";
import { BehaviorSubject, fromEvent, merge, Observable } from "rxjs";
import { SnakBarComponent } from "../custom-components/snak-bar/snak- 
bar.component";
@Component({
selector: "app-project",
templateUrl: "./project.component.html",
styleUrls: ["./project.component.scss"]
})
export class ProjectComponent implements OnInit {
isLoadingResults = false;
displayedColumns = ["name", "description", "actions"];
dataSource: MatTableDataSource<any>;
columnList: any = [];
index: number;
id: number;
data;
@ViewChild(MatPaginator) paginator: MatPaginator;
@Output() columnListChange = new EventEmitter<any[]>();
@ViewChild(MatSort) sort: MatSort;
@ViewChild("filter") filter: ElementRef;
selection = new SelectionModel<Element>(false, null);

constructor(
    public dialog: MatDialog,
    public recommendationService: RecommendationService,
    private snakbar: SnakBarComponent,
    private router: Router,
    public globalAppSateService: GlobalAppSateService,
    private dataService: DataService
) {}

addColumns(data, index) {
    // const copiedData = data.slice();
    this.columnList.push(this.createColumnList(data, index));

    // this.columnListChange.next(copiedData);
}
// prepare mat table json to display
private createColumnList(data, index) {
    return {
        index: index,
        id: data.id,
        projectName: data.projectName,
        projectDescription: data.projectDescription,
        selected: false
    };
}

ngOnInit() {


    const project = JSON.parse(this.dataService.getObject("project"));
    if (project != null) {
        this.globalAppSateService.onMessage(project);
    }
    this.loadData();

}



public loadData() {
    this.isLoadingResults = true;
    this.recommendationService.getAllProjects().subscribe(
        data => {
            let index = 0;
            const response: any = data;
            response.forEach(el => {
                index++;
                this.addColumns(el, index);
            });
            const selectedProject = JSON.parse(
                this.dataService.getObject('project')
            );
            if (selectedProject !== null) {
                this.columnList.forEach(project => {
                    const temp: any = project;
                    if (temp.projectName === selectedProject.projectName) {
                        temp.selected = true;
                    }
                });
            }

            this.dataSource = new MatTableDataSource<any>(this.columnList);

            this.dataSource
                .connect()
                .subscribe(list => this.columnListChange.emit(list));
            this.dataSource.sort = this.sort;
            fromEvent(this.filter.nativeElement, 'keyup')
                // .debounceTime(150)
                // .distinctUntilChanged()
                .subscribe(() => {
                    if (!this.dataSource) {
                        return;
                    }
                    this.dataSource.filter = this.filter.nativeElement.value;
                });

            this.isLoadingResults = false;
        },
        error => {
            this.isLoadingResults = false;
            this.snakbar.statusBar('Filed to load projects', 'Filure');
        }
    );
}
public proceed(row) {
    const dialogRef = this.dialog.open(ConfirmDialogComponent, {});
        dialogRef.afterClosed().subscribe(result => {
            if (result === true) {
                this.dataService.setObject('project', JSON.stringify(row));
                this.globalAppSateService.onMessage(row);
                this.router.navigate(['/metadata']);
            }
        });
        this.columnList.forEach(column => {
            if (row.projectName === column.projectName) {
                column.selected = true;
            } else {
                column.selected = false;
            }
        });
        this.dataSource = new MatTableDataSource<any>(this.columnList);

        this.dataSource
            .connect()
            .subscribe(list => this.columnListChange.emit(list));
        this.dataSource.sort = this.sort;
}
ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

}

0 个答案:

没有答案