【问题标题】:Angular material dataTable Filtering and pagination not working with data from rest Api角度材料数据表过滤和分页不适用于来自rest Api的数据
【发布时间】:2020-08-06 16:34:37
【问题描述】:

这是带有 dataSource 属性的角材料表:

 <!-- filter -->
    
    <mat-form-field>
        <input matInput (blur)="applyFilter($event.target.value)" placeholder="Filter">
    </mat-form-field>
    
    <!-- data table-->
    <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
    
        <!--- Note that these columns can be defined in any order.
              The actual rendered columns are set as a property on the row definition" -->
    
      <!-- nom Column -->
        <ng-container matColumnDef="cin">
            <th mat-header-cell *matHeaderCellDef> Cin </th>
            <td mat-cell *matCellDef="let chauffeur"> {{chauffeur.cin}} </td>
        </ng-container>
    
        <!-- nom Column -->
        <ng-container matColumnDef="nom">
            <th mat-header-cell *matHeaderCellDef> Nom </th>
            <td mat-cell *matCellDef="let chauffeur"> {{chauffeur.nom}} </td>
        </ng-container>
    
        <!-- prenom Column -->
        <ng-container matColumnDef="prenom">
            <th mat-header-cell *matHeaderCellDef> prenom </th>
            <td mat-cell *matCellDef="let chauffeur"> {{chauffeur.prenom}} </td>
        </ng-container>
    
        <!-- prenom Column -->
        <ng-container matColumnDef="dateExpr">
            <th mat-header-cell *matHeaderCellDef> Date Expiration permis </th>
            <td mat-cell *matCellDef="let chauffeur"> {{chauffeur.dateValiditePermis}} </td>
        </ng-container>
     <!-- Actions Column -->
        <ng-container matColumnDef="actions">
            <th mat-header-cell *matHeaderCellDef> Actions </th>
            <td mat-cell *matCellDef="let chauffeur">
                <button mat-button (click)="editChauffeur(chauffeur)">Détails</button>
                <button mat-button (click)="openDialog(chauffeur.idChauffeur)">Supprimer</button>
            </td>
        </ng-container>
    
    
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    
    


    <mat-paginator #paginator [pageSizeOptions]="[2,5, 10]" showFirstLastButtons></mat-paginator>
        <mat-divider></mat-divider>
    

这是从rest api调用中捕获数据的对应ts文件:

 

     export class ChauffeurComponent implements OnInit {
             
               //---****----
               displayedColumns: string[] = ['cin','nom','prenom','dateExpr','actions'];
              
               //******bound to table data */
               dataSource=new  MatTableDataSource();
             
              //---paginator def
               @ViewChild(MatPaginator, {static:false}) paginator: MatPaginator;
            
            
              constructor(public dialog: MatDialog, private router:Router, private chauffeurService:ChauffeurService) {
             
                 //since ngOnint method is executed only once we have to
                //subscribe to routing to load data correctly whenver a route is activated to our component
                router.events.subscribe(event => {
                  if (event instanceof NavigationEnd) {
                    this.reloadData();
                  }
                });
            
               }
            
              ngOnInit() {
                
              }
            
            
              reloadData(){ 
            
               
                //get remoted data and populate dataSource object 
                this.chauffeurService.getChauffeurs().subscribe(dataFlow => {this.dataSource=dataFlow ;
                  this.dataSource.paginator = this.paginator;
                 
                }, 
                  error => {
                    
                  });
            }//----filter method
     applyFilter(filterValue: string) {
         this.dataSource.filter = filterValue.trim().toLowerCase();
         console.log("filtring : "+this.dataSource);
    }

我尝试了上一篇文章中的所有解决方案,但仍然不起作用,我没有使用分页 或在服务器端过滤我只想在角垫表中进行分页和过滤

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    解决方案是通过 api 调用将 dataSource.data 分配给 dataFlow,而不是 dataSource 整个对象:

    this.dataSource.data = dataFlow

    一切正常!

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      相关资源
      最近更新 更多