-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter): add support for custom filters (#825)
- Loading branch information
1 parent
f9da8de
commit b4713d9
Showing
11 changed files
with
194 additions
and
51 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
43 changes: 43 additions & 0 deletions
43
src/app/pages/examples/custom-edit-view/custom-filter.component.ts
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,43 @@ | ||
import {Component, OnChanges, OnInit, SimpleChanges} from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { debounceTime, distinctUntilChanged, skip } from 'rxjs/operators'; | ||
|
||
import { DefaultFilter } from '../../../../ng2-smart-table'; | ||
|
||
@Component({ | ||
template: ` | ||
<input | ||
#number | ||
[ngClass]="inputClass" | ||
[formControl]="inputControl" | ||
class="form-control" | ||
[placeholder]="column.title" | ||
type="number"> | ||
`, | ||
}) | ||
export class CustomFilterComponent extends DefaultFilter implements OnInit, OnChanges { | ||
inputControl = new FormControl(); | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
ngOnInit() { | ||
this.inputControl.valueChanges | ||
.pipe( | ||
distinctUntilChanged(), | ||
debounceTime(this.delay), | ||
) | ||
.subscribe((value: number) => { | ||
this.query = value !== null ? this.inputControl.value.toString() : ''; | ||
this.setFilter(); | ||
}); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (changes.query) { | ||
this.query = changes.query.currentValue; | ||
this.inputControl.setValue(this.query); | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/ng2-smart-table/components/filter/custom-filter.component.ts
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,48 @@ | ||
import { | ||
Component, | ||
ComponentFactoryResolver, Input, | ||
OnChanges, | ||
OnDestroy, | ||
SimpleChanges, | ||
ViewChild, | ||
ViewContainerRef | ||
} from '@angular/core'; | ||
|
||
import { FilterDefault } from './filter-default'; | ||
|
||
@Component({ | ||
selector: 'custom-table-filter', | ||
template: `<ng-template #dynamicTarget></ng-template>`, | ||
}) | ||
export class CustomFilterComponent extends FilterDefault implements OnChanges, OnDestroy { | ||
@Input() query: string; | ||
customComponent: any; | ||
@ViewChild('dynamicTarget', { read: ViewContainerRef }) dynamicTarget: any; | ||
|
||
constructor(private resolver: ComponentFactoryResolver) { | ||
super(); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (this.column && !this.customComponent) { | ||
const componentFactory = this.resolver.resolveComponentFactory(this.column.filter.component); | ||
this.customComponent = this.dynamicTarget.createComponent(componentFactory); | ||
|
||
// set @Inputs and @Outputs of custom component | ||
this.customComponent.instance.query = this.query; | ||
this.customComponent.instance.column = this.column; | ||
this.customComponent.instance.source = this.source; | ||
this.customComponent.instance.inputClass = this.inputClass; | ||
this.customComponent.instance.filter.subscribe((event: any) => this.onFilter(event)); | ||
} | ||
if (this.customComponent) { | ||
this.customComponent.instance.ngOnChanges(changes); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this.customComponent) { | ||
this.customComponent.destroy(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/ng2-smart-table/components/filter/default-filter.component.ts
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,38 @@ | ||
import {Component, Input} from '@angular/core'; | ||
|
||
import {FilterDefault} from "./filter-default"; | ||
|
||
@Component({ | ||
selector: 'default-table-filter', | ||
template: ` | ||
<ng-container [ngSwitch]="column.getFilterType()"> | ||
<select-filter *ngSwitchCase="'list'" | ||
[query]="query" | ||
[ngClass]="inputClass" | ||
[column]="column" | ||
(filter)="onFilter($event)"> | ||
</select-filter> | ||
<checkbox-filter *ngSwitchCase="'checkbox'" | ||
[query]="query" | ||
[ngClass]="inputClass" | ||
[column]="column" | ||
(filter)="onFilter($event)"> | ||
</checkbox-filter> | ||
<completer-filter *ngSwitchCase="'completer'" | ||
[query]="query" | ||
[ngClass]="inputClass" | ||
[column]="column" | ||
(filter)="onFilter($event)"> | ||
</completer-filter> | ||
<input-filter *ngSwitchDefault | ||
[query]="query" | ||
[ngClass]="inputClass" | ||
[column]="column" | ||
(filter)="onFilter($event)"> | ||
</input-filter> | ||
</ng-container> | ||
`, | ||
}) | ||
export class DefaultFilterComponent extends FilterDefault { | ||
@Input() query: string; | ||
} |
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,23 @@ | ||
import { Output, EventEmitter, Input } from '@angular/core'; | ||
|
||
import { Column } from '../../lib/data-set/column'; | ||
import { DataSource } from '../../lib/data-source/data-source'; | ||
|
||
export class FilterDefault { | ||
|
||
@Input() column: Column; | ||
@Input() source: DataSource; | ||
@Input() inputClass: string = ''; | ||
|
||
@Output() filter = new EventEmitter<any>(); | ||
|
||
query: string = ''; | ||
|
||
onFilter(query: string) { | ||
this.source.addFilter({ | ||
field: this.column.id, | ||
search: query, | ||
filter: this.column.getFilterFunction(), | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './ng2-smart-table.module'; | ||
export { ViewCell } from './components/cell/cell-view-mode/view-cell'; | ||
export { DefaultEditor, Editor } from './components/cell/cell-editors/default-editor'; | ||
export { DefaultFilter, Filter } from './components/filter/filter-types/default-filter' | ||
export { Cell } from './lib/data-set/cell'; | ||
export { LocalDataSource } from './lib/data-source/local/local.data-source'; | ||
export { ServerDataSource } from './lib/data-source/server/server.data-source'; |
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