ng2-smart-table icon indicating copy to clipboard operation
ng2-smart-table copied to clipboard

Add filter when column type is Number

Open sw-dev-code opened this issue 4 years ago • 6 comments

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?

sw-dev-code avatar Sep 26 '20 07:09 sw-dev-code

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);
}


vicenthy avatar Oct 09 '20 21:10 vicenthy

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.

sw-dev-code avatar Oct 09 '20 21:10 sw-dev-code

https://akveo.github.io/ng2-smart-table/#/examples/using-filters

vicenthy avatar Oct 09 '20 22:10 vicenthy

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(),
},

sw-dev-code avatar Oct 09 '20 23:10 sw-dev-code

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>

vicenthy avatar Oct 13 '20 00:10 vicenthy

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.

yusheng-jia avatar Apr 24 '23 06:04 yusheng-jia