I need to filter the table data according to the value I set in the input on keyup
event. As there are multiple columns, filtered data should be displayed accordingly.
In my html I have below
<tr>
<td *ngFor="let t of tableHeader">
<input type="text" [id]="t.key" (keyup)="inputChange($event)">
</td>
</tr>
My TS file is as below
public inputChange(value) {
var prevData = this.dataSource;
if (this.selectedId == '') {
prevData = this.result;
}
else if (this.selectedId != '' && this.selectedId != value.target.id) {
prevData = this.result;
}
if (this.selectedId != '' && filterValue.target.value == '') {
this.result = prevData;
return;
}
this.selectedId = value.target.id;
this.inputValue = value.target.value;
var filteredData = this.result.filter(x => x[this.selectedId] && x[this.selectedId].toString().toLowerCase().includes(this.inputValue.toLowerCase()));
this.result = filteredData;
}
So far I have done this and get data filtered accordingly, but when I remove a value from a particular filter input, it loads the data source.
Expected: to display the previously filtered data instead of the data source.
To achieve this I would use below steps using rxjs
datasource
as an Observable
dataSource = [
{
firstname: "James",
lastname: "Amstrong",
othernames: "Angel",
age: 35
},
{
firstname: "John",
lastname: "Peter",
othernames: "Ava",
age: 43
},
...
]
We will convert this datasource
to an observable using of
from 'rxjs'
Observable
tableHeader = [
{
key: "firstname"
},
{
key: "lastname"
},
...
];
filterKeyValues = this.tableHeader.map(({ key }) => ({ key, value: "" }));
filterSubject$ = new BehaviorSubject(this.filterKeyValues)
filter$ = this.filterSubject$.asObservable()
This simply generates an Observable
with the structure
Observable<[{ key: firtname, value: ''}, { key: lastname, value: ''}, ...]>
Observable
as the new filtered datadataSource$ = combineLatest([
this.filter$, of(this.dataSource)
]).pipe(
map(([filter, dataSource]) =>
dataSource.filter(item =>
filter.every((value) =>
(new RegExp(String(value.value).toLowerCase())).test(String(item[value.key]).toLowerCase())
)
)
)
)
inputChange
public inputChange(event: any, key) {
this.filterKeyValues.find(({key: testKey}) => testKey === key).value = event.target.value
this.filterSubject$.next(this.filterKeyValues)
}
async
to display the contents<table>
<tr>
<td *ngFor="let t of tableHeader">
<input type="text" [id]="t.key" (keyup)="inputChange($event, t.key)">
</td>
</tr>
<tr *ngFor="let item of dataSource$ | async">
<td>{{ item.firstname }}</td>
<td>{{ item.lastname }}</td>
<td>{{ item.othernames }}</td>
<td>{{ item.age }}</td>
</tr>
</table>
We are calling the next value. this will force a change detection and datasorce$
Observable
will be reevaluated causing an update in the UI
I have made a Demo Here to illustrate this approach
that is exactly how i wanted, Thankyou @Owen Kelvin.