-
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
close #1872 add spec and fix bug
- Loading branch information
Wendell
committed
Jul 28, 2018
1 parent
8e7a6cb
commit cc5698f
Showing
6 changed files
with
226 additions
and
7 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 }` 属性来默认启用一个筛选器。 | ||
|
||
## en-US | ||
|
||
you can enable a filter by default by setting a `filter` object's property: `{ byDefault: true }` |
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 = []; | ||
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 = [ ...this.data ]; | ||
|
||
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