-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:table): support default filter (#1893)
close #1872
- Loading branch information
Showing
6 changed files
with
221 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 6 | ||
title: | ||
en-US: Default filter | ||
zh-CN: 默认筛选 | ||
--- | ||
|
||
## zh-CN | ||
|
||
通过设置 filter 对象的 `{ byDefault: true }` 属性来默认启用一个筛选器。注意,你必须同时自行设置过滤后应当展示的列表项,为了保持数据流的清晰和数据的一致性,ng-zorro 不会为你做这项工作。详情请见 demo。 | ||
|
||
## en-US | ||
|
||
you can enable a filter by default by setting a `filter` object's property: `{ byDefault: true }`. Be aware that you should set the filtered table contents by yourself. In order to keep clarity and consistency of data, ng-zorro would not do default filtering for you. Please refer to the demo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-table-default-filter', | ||
template: ` | ||
<nz-table #filterTable [nzData]="displayData"> | ||
<thead (nzSortChange)="sort($event)" nzSingleSort> | ||
<tr> | ||
<th nzShowSort nzSortKey="name" nzShowFilter [nzFilters]="nameList" (nzFilterChange)="filter($event,searchAddress)">Name</th> | ||
<th nzShowSort nzSortKey="age">Age</th> | ||
<th nzShowSort nzSortKey="address" nzShowFilter [nzFilterMultiple]="false" [nzFilters]="addressList" (nzFilterChange)="filter(listOfSearchName,$event)">Address</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr *ngFor="let data of filterTable.data"> | ||
<td>{{data.name}}</td> | ||
<td>{{data.age}}</td> | ||
<td>{{data.address}}</td> | ||
</tr> | ||
</tbody> | ||
</nz-table>` | ||
}) | ||
export class NzDemoTableDefaultFilterComponent { | ||
nameList = [ | ||
{ text: 'Joe', value: 'Joe', byDefault: true }, | ||
{ text: 'Jim', value: 'Jim' } | ||
]; | ||
addressList = [ | ||
{ text: 'London', value: 'London', byDefault: true }, | ||
{ text: 'Sidney', value: 'Sidney' } | ||
]; | ||
sortName = null; | ||
sortValue = null; | ||
listOfSearchName = [ 'Joe', 'London' ]; // You need to change it as well! | ||
searchAddress: string; | ||
data = [ | ||
{ | ||
name: 'John Brown', | ||
age: 32, | ||
address: 'New York No. 1 Lake Park' | ||
}, | ||
{ | ||
name: 'Jim Green', | ||
age: 42, | ||
address: 'London No. 1 Lake Park' | ||
}, | ||
{ | ||
name: 'Joe Black', | ||
age: 32, | ||
address: 'Sidney No. 1 Lake Park' | ||
}, | ||
{ | ||
name: 'Jim Red', | ||
age: 32, | ||
address: 'London No. 2 Lake Park' | ||
} | ||
]; | ||
displayData = [ ]; // You need to change it as well! | ||
|
||
sort(sort: { key: string, value: string }): void { | ||
this.sortName = sort.key; | ||
this.sortValue = sort.value; | ||
this.search(); | ||
} | ||
|
||
filter(listOfSearchName: string[], searchAddress: string): void { | ||
this.listOfSearchName = listOfSearchName; | ||
this.searchAddress = searchAddress; | ||
this.search(); | ||
} | ||
|
||
search(): void { | ||
/** filter data **/ | ||
const filterFunc = item => (this.searchAddress ? item.address.indexOf(this.searchAddress) !== -1 : true) && (this.listOfSearchName.length ? this.listOfSearchName.some(name => item.name.indexOf(name) !== -1) : true); | ||
const data = this.data.filter(item => filterFunc(item)); | ||
/** sort data **/ | ||
if (this.sortName) { | ||
this.displayData = data.sort((a, b) => (this.sortValue === 'ascend') ? (a[ this.sortName ] > b[ this.sortName ] ? 1 : -1) : (b[ this.sortName ] > a[ this.sortName ] ? 1 : -1)); | ||
} else { | ||
this.displayData = data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters