简体   繁体   English

Angular 材料数据表过滤和分页不适用于来自 rest 的数据 Api

[英]Angular material dataTable Filtering and pagination not working with data from rest Api

Here's angular material table with dataSource property:这是具有 dataSource 属性的 angular 材料表:

 <!-- 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>
    

and this is the correspondant ts file that captures data from rest api call:这是对应的 ts 文件,它从 rest api 调用中捕获数据:

 

     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);
    }

I tried all the solution from the previous post but still doesn't work, I'm not using pagination or filtering in the server side i want simply pagination and filtering in angular mat tabe我尝试了上一篇文章中的所有解决方案,但仍然无效,我没有在服务器端使用分页或过滤我只想在 angular mat tabe 中使用分页和过滤

the solution for this is to assign dataSource.data to the dataFlow from the api call instead of dataSource entire object:解决方案是将 dataSource.data 分配给来自 api 调用的 dataFlow,而不是 dataSource 整个 object:

this.dataSource.data = dataFlow

and everything worked correctly !一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自rest api的角度材料数据表 - Angular material data table from rest api 从 api 获取的数据中的角度材料分页 - Pagination in angular material from data fetched from an api 如何从 rest api 显示 angular 材料表分页上的数据总数,当时正在获取 10 条记录 - How to display total count of data on angular material table pagination from rest api which is fetching at the time 10 records 棱角分明的数据表,带有排序,分页和过滤页面问题 - angular material Data table with sorting, pagination, and filtering page issue 角度材料数据表,使用Firestore进行排序,分页和过滤 - Angular Material Data Table with sorting, pagination, and filtering using Firestore Angular5材质数据表过滤不起作用 - Angular5 Material Data table filtering not working 来自其余 API 的数据未显示在 Angular 材料表中 - Data from rest API not showing up in Angular Material Table Angular Material - mat-table 不渲染来自 rest api 的数据 - Angular Material - mat-table not rendering data from rest api Angular 材料 - Mat-table 不显示来自 REST 的数据 API - Angular Material - Mat-table not displaying data from REST API 角度材料数据表 - 将过滤应用于自定义数据源 - Angular Material Datatable - apply filtering to custom data source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM