Home > Enterprise >  How can i filter more data in filter javascript?
How can i filter more data in filter javascript?

Time:10-25

How can I filter more data from the ngx-datatable? I have this filter that brings me the data only from the "Name" column, but I also need to filter the data from other columns, such as "id", or "phone"

filterUpdate(event) {
  const val = event.target.value.toLowerCase();

  const filterData = this.rows.filter((item) => {
    const filterData = item.nombre.toLowerCase().indexOf(val) !== -1 || !val;
    return filterData;
  });
    
  // update the rows
  this.filterRows = filterData;

  console.log(filterData);
}

CodePudding user response:

If you want to filter all the columns you can do something like this:

const filterData = this.rows.filter((item) => {
    let include = false;
    for (objValue in item) {
        if (objValue.toLowerCase().includes(val) include = true;
    }
    return include
});

You will loop through all the colums with a for ... in ... loop, and you check if your val is included in any of the column. If it is, the filter keep the object, otherwise it will reject it.

If you want to check only some column you can simply add those column in your if while defining filterData, an example follows:

const filterData = this.rows.filter((item) => {
    const filterData = item.nombre.toLowerCase().includes(val) || item.phone.toLowerCase().includes(val) || ...;
    return filterData;
});
  • Related