Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:th): fix table th filter & style bug #1674

Merged
merged 1 commit into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/table/demo/custom-filter-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class NzDemoTableCustomFilterPanelComponent {

search(): void {
const filterFunc = (item) => {
return (this.searchAddress.length ? this.searchAddress.some(address => item.address.indexOf(address.name) !== -1) : true) &&
return (this.searchAddress.length ? this.searchAddress.some(address => item.address.indexOf(address) !== -1) : true) &&
(item.name.indexOf(this.searchValue) !== -1);
};
const data = this.data.filter(item => filterFunc(item));
Expand Down
35 changes: 28 additions & 7 deletions components/table/nz-th.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Component,
ElementRef,
EventEmitter,
HostBinding,
Input,
Output,
Renderer2,
Expand Down Expand Up @@ -55,6 +56,11 @@ export class NzThComponent {
/* tslint:disable-next-line:no-any */
@Output() nzFilterChange = new EventEmitter<any[] | any>();

@HostBinding('class.ant-table-column-has-filters')
get hasFiltersClass(): boolean {
return this.nzShowSort || this.nzShowFilter;
}

@Input()
set nzShowSort(value: boolean) {
this._showSort = toBoolean(value);
Expand Down Expand Up @@ -157,16 +163,30 @@ export class NzThComponent {
this.nzSortChange.emit(this.nzSort);
}

get filterList(): NzThItemInterface[] {
return this.multipleFilterList.filter(item => item.checked).map(item => item.value);
}

/* tslint:disable-next-line:no-any */
get filterValue(): any {
const checkedFilter = this.singleFilterList.find(item => item.checked);
return checkedFilter ? checkedFilter.value : null;
}

updateFilterStatus(): void {
if (this.nzFilterMultiple) {
this.hasFilterValue = this.filterList.length > 0;
} else {
this.hasFilterValue = isNotNil(this.filterValue);
}
}

search(): void {
this.updateFilterStatus();
if (this.nzFilterMultiple) {
const filterList = this.multipleFilterList.filter(item => item.checked).map(item => item.value);
this.hasFilterValue = filterList.length > 0;
this.nzFilterChange.emit(filterList);
this.nzFilterChange.emit(this.filterList);
} else {
const checkedFilter = this.singleFilterList.find(item => item.checked);
const filterValue = checkedFilter ? checkedFilter.value : null;
this.hasFilterValue = isNotNil(filterValue);
this.nzFilterChange.emit(filterValue);
this.nzFilterChange.emit(this.filterValue);
}
this.hideDropDown();
}
Expand Down Expand Up @@ -204,6 +224,7 @@ export class NzThComponent {
this._filters = value;
this.initMultipleFilterList();
this.initSingleFilterList();
this.updateFilterStatus();
} else {
console.warn('nzFilters only accept type of Array<{ text: string; value: any }>');
}
Expand Down
33 changes: 33 additions & 0 deletions components/table/nz-th.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ describe('nz-th', () => {
});
it('should showSort work', () => {
fixture.detectChanges();
expect(th.nativeElement.classList).not.toContain('ant-table-column-has-filters');
expect(th.nativeElement.querySelector('.ant-table-column-sorter')).toBeNull();
testComponent.showSort = true;
fixture.detectChanges();
expect(th.nativeElement.classList).toContain('ant-table-column-has-filters');
expect(th.nativeElement.querySelector('.ant-table-column-sorter')).toBeDefined();
});
it('should sort change work', () => {
Expand Down Expand Up @@ -167,9 +169,11 @@ describe('nz-th', () => {
});
it('should showFilter work', () => {
fixture.detectChanges();
expect(th.nativeElement.classList).not.toContain('ant-table-column-has-filters');
expect(th.nativeElement.querySelector('.anticon anticon-filter')).toBeNull();
testComponent.showFilter = true;
fixture.detectChanges();
expect(th.nativeElement.classList).toContain('ant-table-column-has-filters');
expect(th.nativeElement.querySelector('.anticon anticon-filter')).toBeDefined();
});
it('should filterChange work', () => {
Expand All @@ -184,6 +188,35 @@ describe('nz-th', () => {
expect(testComponent.nzThComponent.hasFilterValue).toBe(true);
expect(testComponent.filterChange).toHaveBeenCalledWith([ '1' ]);
});
it('should hasFilter change after filters change with multiple', () => {
testComponent.showFilter = true;
fixture.detectChanges();
testComponent.nzThComponent.checkMultiple(testComponent.nzThComponent.multipleFilterList[ 0 ]);
testComponent.nzThComponent.search();
fixture.detectChanges();
expect(testComponent.nzThComponent.hasFilterValue).toBe(true);
testComponent.filters = [
{ text: 'filter1', value: '4' },
{ text: 'filter2', value: '3' }
];
fixture.detectChanges();
expect(testComponent.nzThComponent.hasFilterValue).toBe(false);
});
it('should hasFilter change after filters change with single', () => {
testComponent.showFilter = true;
testComponent.filterMultiple = false;
fixture.detectChanges();
testComponent.nzThComponent.checkSingle(testComponent.nzThComponent.singleFilterList[ 0 ]);
testComponent.nzThComponent.search();
fixture.detectChanges();
expect(testComponent.nzThComponent.hasFilterValue).toBe(true);
testComponent.filters = [
{ text: 'filter1', value: '5' },
{ text: 'filter2', value: '3' }
];
fixture.detectChanges();
expect(testComponent.nzThComponent.hasFilterValue).toBe(false);
});
it('should reset work', () => {
testComponent.showFilter = true;
fixture.detectChanges();
Expand Down