[Table] - Add example with multiple filtering columns
Bug, feature request, or proposal:
Proposal.
What is the expected behavior?
It would be good to see an example showing multiple filters (in this case inputs/selects) to filter rows/cells in md-table.
You can see this as an example.
What is the current behavior?
Currently the filter example just shows how to filter multiple columns by a single input.
@andrewseguin I haven't come across any examples on how to accomplish this yet... any leads?
This shows two separate text filters, but the concept would be the same for selects, checkboxes, etc.
https://plnkr.co/edit/oQOYQgW0vCx8tfAgb1w9?p=preview
EDIT: and here's a version filtering with a slider
https://plnkr.co/edit/KZChr8jw1XJCCeNiDmUj?p=preview
thanks! this has helped me sort out my issue, I couldn't tell from the sample code that it was just a filter in a filter, but the code in that plunker you sent is well formatted, I could easily understand it. Thanks again
Hi,
actually the mat(cdk)-table doesn't seem to be as flexible as promised. I want to have a second header-row above the normal header-row where i can have text-input fields for filtering each column This doesn't seem to be possible as of right now ?
Regards, Nick
@nickwinger , try this, this is working for me :
<ng-container matColumnDef="entity">
<mat-header-cell *matHeaderCellDef>
<div fxFlexFill>
<span translate="front.monitoring.quarterly.entity"></span>
<mat-form-field floatPlaceholder="never">
<mat-icon matPrefix>search</mat-icon>
<input matInput [formControl]="entityFilter">
</mat-form-field>
</div>
</mat-header-cell>
<mat-cell *matCellDef="let history">{{ history.entity }}</mat-cell>
</ng-container>
@Spanja i don't know what you are trying to tell me ? Where is the "second" header row in here ?? This is only a filter header, actually i have the normal header with sorting also
My bad I misunderstood, I thought you wanted a second row in the header instead of a second header.
trying to implement multiple columns filtering using the filterPredicate. The limitation i saw when trying to perform filtering on multiple columns is that the "this" pointer is that of the MatTableDataSource and not that of my component. if the filterPredicate pass back the context of my component, then i can have a simpler solution. At this point, the work around is have a form based input to accept search values for each column or to have another table above the main mat-table.
solve it! woot!!! first, as i said earlier, there is a limitation in the filteredPredicate, i tried to put in the component "this" pointer into each data item which solved my problem of accessing the "this" pointer inside the filterPredicate function. but still trying to manipulate this.dataSource.filteredData is too buggy.
i ended up rolling my own filter function. the issue itself deals with a 2nd header. I solved it by putting a footer instead as this is our requirement. implementing another top header is similar.
Sharing my poc. hope this is useful to others.
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="f1">
<mat-header-cell *matHeaderCellDef mat-sort-header>Field 1 </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.f1}} </mat-cell>
</ng-container>
<ng-container matColumnDef="f2">
<mat-header-cell *matHeaderCellDef mat-sort-header>Field 2</mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.f2}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns" ></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
</mat-table>
<div class="eps-footer-row mat-header-row" role="row">
<div class="eps-footer-cell mat-header-cell mat-column-f1" role="columnheader">
<input (keyup)="applyColumnFilter('f1', $event.target.value)" placeholder="search Field 1" />
</div>
<div class="eps-footer-cell mat-header-cell mat-column-f2" role="columnheader">
<input (keyup)="applyColumnFilter('f2', $event.target.value)" placeholder="search Field 2" />
</div>
</div>
<mat-paginator [pageSizeOptions]="[5, 25, 50, 100]"></mat-paginator>
Updated: I also styled the two css elements; "eps-footer-row" and "eps-footer-cell". no magic here, just background colors and color
I also did it this way now as a workaround, but it is not perfect when it comes to horizontal scrolling and responsivness. It is a hack and not a real second mat-header-row.
interesting to see why u need the horizontal scrolling. what is your requirement? pls share a simplified view , maybe we can input some ideas
Why i need horizontal scrolling ? If you want to display a lot of data columns, that just don't fit the browser width, what should happen ? -> of course horizontal scrolling. The mat-table should be flexible, it should of course support horizontal scrolling (like any data-table) I have set minimum widths on different data-columns, so there appears a horizontal scrollbar...
from a technical solution, yes, the horizontal scrolling would be do fine.
but .... my UX expert would strongly throw her eyes up :p
her usual analogy - u won't put more than 8 points in your powerpoint slides, so why would u put more than 8 columns.
no dispute on what u r trying to achieve. from a better UX, I agree with my UX expert. yr call
Lol, my UX expert says the same, but he agreed with me:
- The Table should be so flexibel
- some heavy Data applications just have to support horizontal scrolling, not everything has to look super cool pretty. Sometime data visualization is just technical... Think of fixing the first 2 or 3 columns and then just scroll right to compare data... My argument, why do we support vertical scrolling then ? Why do woman hate white socks ?
Cheers from a non UX expert ;-D
first question: why do women hate white socks? hahahahha. i am curious. :p btw, i do love your idea suggestion : "fixing the first 2 or 3 columns". I would love to see this feature implemented.
maybe a suggestion back to the developers of this group that we can do it via an option e.g.
fixedColumns: {
leftColumns: [0,1] // fixed the first and 2nd column - zero index
rightColumns: [0] // fixed the right hand 1st column
}
very good suggestion. (Y)
@erickyi2006 I am using mat-menu, mat-select, mat-input everywhere to meet my requirements for the mat-table like context menu, column filter etc;
For instance here is the gif:

tnx @irowbin , good demo u got there. looks v configurable. well done. is there a question for me?
Wonderful, @irowbin! Could you provide some sort of example with mat-select in md-table' header?
@astra1 here is some snippet from my code:
<!-- repeat this to all columns -->
<ng-container cdkColumnDef="Column1">
<mat-header-cell *cdkHeaderCellDef>
<!-- instead of adding mat-sort-header directive to the mat-header-cell, i've move it on span tag to separate sorting and filtering icon .-->
<span mat-sort-header>Column1 </span>
<!-- i've applied css rules to the icon-->
<span (click)="onFilterClick('col1')" class="filter-icon">
<mat-icon>keyboard_arrow_down</mat-icon>
</span>
<filter-template [openFilter]="true" *ngIf="templateFor === 'col1' "></filter-template>
</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.Column1}}</mat-cell>
</ng-container>
// mat-table component.ts
onFilterClick(templateFor:string){
this.templateFor = templateFor;
}
// inside the filter-template component.ts
@ViewChild('filterMenu') triggerMenu: MatMenuTrigger;
@Input() openFilter: boolean;
ngOnChanges(){
if(this.openFilter) {
this.triggerMenu.openMenu()
}
}
<!-- filter-template component.html -->
<span #filterMenu="matMenuTrigger" [matMenuTriggerFor]="menu" id="filter-panel">
<mat-menu #menu="matMenu" [overlapTrigger]="false">
<div (click)="$event.stopPropagation()" (keyup)="$event.stopPropagation()" (keydown)="$event.stopPropagation()">
<form autocomplete="off" (ngSubmit)="onFilterSubmit()" [formGroup]="filterForm">
<mat-form-field style="width: auto;">
<mat-select formControlName="op" placeholder="Condition:">
<mat-option *ngFor="let con of conditions" [value]="condi.id">
{{con.text}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field *ngIf="!forFirstValue">
<input formControlName="val1" matInput placeholder="Value">
</mat-form-field>
<mat-form-field class="font-sm" *ngIf="forSecondValue">
<input formControlName="val2" matInput placeholder="Second Value">
</mat-form-field>
<button [disabled]="hasNoValue" matRipple type="submit"></button>
</form>
</div>
</mat-menu>
</span>
I hope you'll get some clue from this snippet. 😆
thanks to @irowbin . I do miss something, get an ERROR TypeError: Cannot read property 'templateRef' of undefined.