Angular 角度9-角度材料垫分页器不工作

Angular 角度9-角度材料垫分页器不工作,angular,angular-material,Angular,Angular Material,我正在使用的paginator角材料。但它不起作用。我在谷歌上搜索并尝试了很多东西,但都不起作用。这是我的component.html <kt-portlet> <kt-portlet-header [class]="'card-head-lg'" > <ng-container ktPortletTitle> <span>Subjects list</span>

我正在使用的paginator角材料。但它不起作用。我在谷歌上搜索并尝试了很多东西,但都不起作用。这是我的component.html

<kt-portlet>
    <kt-portlet-header [class]="'card-head-lg'" >
        <ng-container ktPortletTitle>
            <span>Subjects list</span>
        </ng-container>

        <ng-container ktPortletTools>
            <button (click)="addSubject()"  mat-raised-button matTooltip="Create new subject" color="primary"
                type="button">
                <span>New Subject</span>
            </button>
        </ng-container>
    </kt-portlet-header>

    <kt-portlet-body>
        <div class="form mb-3">
            <div>
                <div class="row align-items-center">

                    <div class="col-md-4 kt-margin-bottom-10-mobile">
                        <label>Search:</label>
                        <mat-form-field class="mat-form-field-fluid" appearance="outline">
                            <input matInput  [(ngModel)]="searchKey"  (keyup)="applyFilter()"  />
                            <button mat-button matSuffix mat-icon-button aria-label="Clear" *ngIf="searchKey" (click)="onSearchClear()">
                                <mat-icon>close</mat-icon>
                              </button> 
                        </mat-form-field>
                    </div>

                </div>
            </div>
        
            <div class="row align-items-center collapse form-group-actions kt-margin-top-20 kt-margin-bottom-20"
                [ngClass]="{'show' : selection.selected.length > 0}">
                <div class="col-xl-12">
                    <div class="form-group form-group-inline">
                        <div class="form-label form-label-no-wrap">
                            <label class="font-bold font-danger">
                                <span>Records selected:</span>
                                {{ selection.selected.length }}
                            </label>
                        </div>
                        <div>
                            <ng-container>
                                <button (click)="deleteSubjects()" mat-raised-button color="warn"
                                    matTooltip="Delete selected subjects" class="mat-button-mt-4">
                                    <mat-icon>delete</mat-icon>
                                    Delete All
                                </button>&nbsp;
                            </ng-container>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mat-table-wrapper" [hidden]="loading">
            <mat-table class="lmat-elevation-z8" #table [dataSource]="dataSource" matSort #sort1="matSort"
                matSortActive="id" matSortDirection="asc" matSortDisableClear>
                <ng-container matColumnDef="select">
                    <mat-header-cell *matHeaderCellDef class="mat-column-checkbox">
                        <mat-checkbox (change)="$event ? masterToggle() : null"
                            [checked]="selection.hasValue() && isAllSelected()"
                            [indeterminate]="selection.hasValue() && !isAllSelected()" [color]="'primary'">
                        </mat-checkbox>
                    </mat-header-cell>
                    <mat-cell *matCellDef="let row" class="mat-column-checkbox">
                        <mat-checkbox (click)="$event.stopPropagation()"
                            (change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row)"
                            [color]="'primary'">
                        </mat-checkbox>
                    </mat-cell>
                </ng-container>

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

                  <ng-container matColumnDef="status">
                    <mat-header-cell *matHeaderCellDef mat-sort-header>Status</mat-header-cell>
                    <mat-cell *matCellDef="let subject">
                        <span
                            class="label label-lg label-light-{{ getItemCssClassByStatus(subject.status) }} label-inline">{{  getItemStatusString(subject.status) }}</span>
                    </mat-cell>
                </ng-container>

                <ng-container matColumnDef="actions">
                    <mat-header-cell *matHeaderCellDef>Actions</mat-header-cell>
                    <mat-cell *matCellDef="let element">
                        <ng-container>
                            
                            <button (click)="editSubject(element)" mat-icon-button color="primary" matTooltip="Edit subject"
                                >
                                <mat-icon>create</mat-icon>
                            </button>&nbsp;
                        </ng-container>

                        <ng-container>
                            <button (click)="deleteSubject(element.id)" mat-icon-button color="warn" matTooltip="Delete subject" type="button"
                                >
                                <mat-icon>delete</mat-icon>
                            </button>&nbsp;
                        </ng-container>

                    </mat-cell>
                </ng-container>

                <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>

                <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
            </mat-table>
        </div>
        <div class="mat-table-bottom">
            <mat-spinner [diameter]="20" *ngIf="loading"></mat-spinner>
            <mat-paginator  [pageSize]="10" [pageSizeOptions]="[5, 10, 15 ]"
                [showFirstLastButtons]="true"></mat-paginator>
        </div>
    </kt-portlet-body>

</kt-portlet>

我还重新安装了angular material,并按照人们的建议尝试了许多方法,但仍然不起作用。

我通过更新angular/material解决了这个问题。还有,我变了

从'@angular/material'导入{MatTableDataSource}


它解决了我的问题。我希望它能对其他人有所帮助。

您是否在应用程序中添加了MatPaginator作为一个模块?试试这个-是的。已添加。@Shivam仍不工作您在项目中使用过管道吗?
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { SelectionModel } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { LayoutUtilsService, MessageType } from '../../../../../../core/_base/crud';

// Services and Models
import { SubjectService } from "../../service/subject.service"
import { Subject } from '../../../models/ISubject';
import { EditSubjectComponent } from '../../subject-edit/edit-subject/edit-subject.component';

@Component({
  selector: 'kt-subject-list',
  templateUrl: './subject-list.component.html',

})
export class SubjectListComponent implements OnInit, AfterViewInit {

  dataSource: MatTableDataSource<any>;
  @ViewChild(MatSort, null) sort: MatSort;
  @ViewChild(MatPaginator,  { static: false }) paginator: MatPaginator;

  
  displayedColumns: string[] = ['select', 'name', 'description', 'status', 'actions'];

  searchKey: string;
  selection = new SelectionModel<Subject>(true, []);
  subjectResult: Subject[] = [];
  loading = true;
  constructor(
    public dialog: MatDialog,
    public snackBar: MatSnackBar,
    private layoutUtilsService: LayoutUtilsService,
    private subjectService: SubjectService
  ) {
    
   }


  ngOnInit() {

    this.relodGrid();

  }
  ngAfterViewInit(): void {  
    
  
   
  }


  relodGrid() {
    this.loading = true;
    
    this.subjectService.getAll().subscribe(
      list => {
        let array = list.content.map(item => {

          return {
           id: item.id,
            name: item.name,
            description: item.description,
            status:item.status

          };
        });
        debugger;
        this.loading = false;
        this.subjectResult = list.content;
        this.dataSource = new MatTableDataSource(array);
       
       
        this.dataSource.sort = this.sort;
        setTimeout(() => {
          this.dataSource.paginator = this.paginator;
        });
       
        
        this.dataSource.filterPredicate = (data, filter) => {
          return this.displayedColumns.some(ele => {
            return  ele != 'status' && ele != 'actions' && ele != 'select' && data[ele].toLowerCase().indexOf(filter) != -1;
          });

          
        };
      }
      
      
      );
  }



  deleteSubject(id) {
    const _title: string = "Delete Subject";
    const _description: string = "Are you sure to delete the subject?";
    const _waitDesciption: string = "Deleting Subject";
    const _deleteMessage = "Subject has successfully deleted";

    const dialogRef = this.layoutUtilsService.deleteElement(_title, _description, _waitDesciption);
    dialogRef.afterClosed().subscribe(res => {
      if (!res) {
        return;
      }

      this.subjectService.delete(id).subscribe((result) => {

        this.relodGrid();
        this.layoutUtilsService.showActionNotification(_deleteMessage, MessageType.Delete);
      });

    });
  }


  deleteSubjects() {
    const _title: string = "Delete All Subjects";
    const _description: string = "Are you sure to delete all subjects";
    const _waitDesciption: string = "Deleting";

    const dialogRef = this.layoutUtilsService.deleteElement(_title, _description, _waitDesciption);
    dialogRef.afterClosed().subscribe(res => {
      if (!res) {
        return;
      }

      debugger;
      const idsForDeletion: number[] = [];
      for (let i = 0; i < this.selection.selected.length; i++) {
      idsForDeletion.push(this.selection.selected[i].id);
      }
      this.subjectService.deleteRange(idsForDeletion).subscribe((result) => {

        if(result.operationSuccess){
          this.relodGrid();
          this.layoutUtilsService.showActionNotification("All subjects deleted successfully", MessageType.Delete);
          this.selection.clear();
        }else{
          this.layoutUtilsService.showActionNotification("Some error occurred while deleting records.", MessageType.Delete);
        }
       
      });
     
     
    });
  }


  isAllSelected(): boolean {

    const numSelected = this.selection.selected.length;
    const numRows = this.subjectResult.length;
    return numSelected === numRows;

  }


  masterToggle() {

    if (this.selection.selected.length === this.dataSource.data.length) {
      this.selection.clear();
    } else {
      this.dataSource.data.forEach(row => this.selection.select(row));
    }
  }


  addSubject() {

    const newSubject = new Subject();
    newSubject.id =0;
    newSubject.name='',
    newSubject.description='';
    newSubject.status = 1;
    this.subjectService.initializeFormGroup(); // Set all defaults fields
    this.editSubject(newSubject);
  }

  editSubject(subject: Subject) {

    const dialogConfig = new MatDialogConfig();
    dialogConfig.width = "50%";
    dialogConfig.disableClose = false;
    dialogConfig.data = {subject}
    this.subjectService.populateForm(subject);
    const dialogRef = this.dialog.open(EditSubjectComponent, dialogConfig);

    dialogRef.afterClosed().subscribe(res => {
      this.subjectService.form.reset();
      this.relodGrid();

    });
  }

  onSearchClear() {
    this.searchKey = "";
    this.applyFilter();
  }

  applyFilter() {
    this.dataSource.filter = this.searchKey.trim().toLowerCase();
  }

  getItemCssClassByStatus(status: number = 0): string {
        switch (status) {
            case 0:
                return 'danger';
            case 1:
                return 'success';
            
        }
        return '';
    }

    /**
     * Returns Item Status in string
     * @param status: number
     */
    getItemStatusString(status: number = 0): string {
        switch (status) {
            case 0:
                return 'Disabled';
            case 1:
                return 'Enabled';
        
        }
        return '';
    }

}
index.d.ts.MatPaginator.html:1 ERROR TypeError: Cannot read property 'pipe' of undefined
    at MatSelect.ngAfterContentInit (select.js:613)
    at callProviderLifecycles (core.js:33979)
    at callElementProvidersLifecycles (core.js:33951)
    at callLifecycleHooksChildrenFirst (core.js:33933)
    at checkAndUpdateView (core.js:46590)
    at callViewAction (core.js:46951)
    at execEmbeddedViewsAction (core.js:46908)
    at checkAndUpdateView (core.js:46586)
    at callViewAction (core.js:46951)
    at execEmbeddedViewsAction (core.js:46908)
 import { MatTableDataSource } from '@angular/material/table';