ng2-smart-table
ng2-smart-table copied to clipboard
Add filter when column type is Number
With elements type String smart table setFilter function is working fine, but it's not working with elements of type number.
Let's say I want to list all data with temperature of 30. Example of setFilter function:
this.source.addFilter({ field: 'temperature', search: 30 }, true, false);
I'm getting
No data found
Is there any difference when the number type is used?
you need implement a custom search
with onSearch(query: string = "") and on table component (keydown.enter)="onSearch(search.value)"
{
field: "temperature",
search: query,
filter: (data: any, query?: string) => {
return data.toString().includes(query);
}
First of all, thank you @vicenthy.
If understand correctly, I have to modify the temperature field in a way you told me and add the (keydown.enter)="onSearch(search.value)"
in HTML inside <ng2-smart-table>
.
But where I have to add onSearch(query: string = "")
and what I'm writing inside that function?
Regards.
https://akveo.github.io/ng2-smart-table/#/examples/using-filters
I would like to have a filter for each column. Is there an option for a simple convert to String? I've tried the following but without success:
speed: {
title: 'Motor Speed',
valuePrepareFunction: (cell, element) =>
element.speed.toString(),
},
create a function in your component
onSearch(query: string = '') {
// this function makes the customized filter
this.source.setFilter([
// fields we want to include in the search
{
field: 'id',
search: query
},
{
field: 'name',
search: query
},
{
field: 'username',
search: query
},
{
field: 'email',
search: query
}
], false);
// second parameter specifying whether to perform 'AND' or 'OR' search
// (meaning all columns should contain search query or at least one)
// 'AND' by default, so changing to 'OR' by setting false here
}
on your template
<input #search class="search" type="text" placeholder="Search..." (keydown.enter)="onSearch(search.value)">
<ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
when Column is a string type ,this works for me.
With elements type String smart table setFilter function is working fine, but it's not working with elements of type number.
Let's say I want to list all data with temperature of 30. Example of setFilter function:
this.source.addFilter({ field: 'temperature', search: 30 }, true, false);
I'm getting
No data found
Is there any difference when the number type is used?
when Column is a string type ,this works for me.