angular - 如何在 isAllselected 函数的数据源中仅获取过滤后的数据,即。 Angular 7 中的复选框列表函数 "isAllSelected"

标签 angular typescript angular-material angular7

我有一个带有所有/行选择复选框和过滤功能的垫表,我正在数据源上应用过滤器,并且我想从此数据源检索所有过滤后的数据。

过滤器已正确应用,在我的垫子表中,过滤后的数据仅显示过滤后的项目,当时,我正在选择有效的行项,但当我想在垫子标题中“全选”复选框时,过滤的项目行在前端被选择,但在我通过调试器检查时,在选定的行数组中,所有行都被选择,但所需的过滤数据行不在数据源中。如何解决这个问题?如果有人现在将示例 URL 放在评论中。

这是一些代码:

<mat-form-field appearance="outline">
    <mat-label>Search</mat-label>
    <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search">
    <mat-icon matSuffix style="font-size: 16px;">search</mat-icon>
</mat-form-field>

<table mat-table #table [dataSource]="dataSource">
    <ng-container matColumnDef="select">
        <th mat-header-cell *matHeaderCellDef>
            <mat-checkbox (change)="$event ? masterToggle() : null"   [checked]="selection.hasValue() && isAllSelected()"
                 [indeterminate]="selection.hasValue() && !isAllSelected()">
            </mat-checkbox>
        </th>
        <td mat-cell *matCellDef="let row">
            <mat-checkbox (click)="$event.stopPropagation()"
                     (change)="$event ? selection.toggle(row) : null"
                     [checked]="selection.isSelected(row)">
            </mat-checkbox>
        </td>
   </ng-container>

   <ng-container matColumnDef="id">
       <th mat-header-cell *matHeaderCellDef ><b>S.No.</b></th>
       <td mat-cell *matCellDef="let element; let i=index">{{i+1}}</td>
   </ng-container>

   <ng-container matColumnDef="name">
       <th mat-header-cell *matHeaderCellDef  mat-sort-header="name">
           <b>Office Name</b>
       </th>
       <td mat-cell *matCellDef="let element"> {{element.name }} </td>
   </ng-container>

   <ng-container matColumnDef="contactPerson">
       <th mat-header-cell *matHeaderCellDef mat-sort-header="contactPerson"><b>Contact Person</b></th>
       <td mat-cell *matCellDef="let element"> {{element.contactPerson}} </td>
   </ng-container>

   <ng-container matColumnDef="phone">
       <th mat-header-cell *matHeaderCellDef mat-sort-header="phone"><b>Mobile No</b></th>
       <td mat-cell *matCellDef="let element"> {{element.phone}} </td>
   </ng-container>

TS代码:

applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}

isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
}

masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.dataSource.data.forEach(row => this.selection.select(row));
}

最佳答案

这似乎是一篇旧文章,但如果其他人需要解决方案,这就是我解决它的方法。 在您的 UI 中发送点击事件:

<mat-checkbox (change)="$event ? masterToggle($event.checked) : null"  [checked]="selection.hasValue() && isAllSelected()"
                 [indeterminate]="selection.hasValue() && !isAllSelected()">  </mat-checkbox>

并且您的后端使用过滤后的数据进行选择:

masterToggle(event: boolean) {
   this.selection.clear();
    
   if(event){
      this.dataSource.filteredData.forEach(row => this.selection.select(row));
   }
}

关于angular - 如何在 isAllselected 函数的数据源中仅获取过滤后的数据,即。 Angular 7 中的复选框列表函数 "isAllSelected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60664916/

相关文章:

html - 数据悬停 angular2 不工作

angular - Ionic 3 图像延迟加载器

Typescript 泛型扩展类和接口(interface)

asp.net - 如何在 VS2015 中引用 Nuget 中的 d.ts 文件?

angular - Angular Material 垫台中的cdkDragHandle不起作用

angularjs - 带有 ui-router 的 Angular Material md-tabs 不加载初始选项卡

javascript - Angular2 加载文件请求过多

angular - 在angular2模板ngFor中声明一个计数变量

JavaScript/ typescript : Created Doubly-Sorted Tree from List

css - Angular Material 中的复选框边框颜色样式(md-checkbox)