Zuet
Zuet

Reputation: 593

How to filter array table in ng-zorro table?

I'm new to Angular.

I have a demo array like this:

for (let index = 1; index <= 100; index++) {
  this.listOfData.push([
    `Name ${index}`,
    `Math.floor(Math.random() * 100)`,
    'Employee',
  ]);
}

And I try to create a filter table with search function:

onSearch = (value) => {
  const output = this.listOfData.filter((arr) =>
    arr.some((item) => {
      item.toLowerCase().includes(value);
    })
  );
  console.log('output: ', output);
  this.listOfDisplayData = output;
};

This is my demo

Upvotes: 1

Views: 722

Answers (1)

Joosep Parts
Joosep Parts

Reputation: 6235

So basically your data item is [[string,string,string],...] when you start filtering with filter you must return true for it to include and false to filter it out.

This should fix it

  onSearch = (value) => {
    const output = this.listOfData.filter((arr) =>
      arr.some((item) => {
        if (item.toLowerCase().includes(value)) {
          return true;
        } else {
          return false;
        }
      })
    );
    console.log('output: ', output);
    this.listOfDisplayData = output;
  }

Upvotes: 1

Related Questions