Filter using N2-smart-table masks

Asked

Viewed 34 times

1

I’m using Angular 6 with N2-smart-table. I want to search a string with and without mask (like a Cpf or cnpj).

Ex.: 28871154000178 and 28.871.154/0001-78 must return the same record when searching for the filter.

inserir a descrição da imagem aqui

I searched through several sources, but couldn’t find a solution.

Grateful from now on!

1 answer

1


There is an item in the documentation that can be prepared a custom filter function, in the question says that the value must be searched without the points, dash and bar, so inside the settings in the item columns configure within the field for cnpj the filterFunction equal below:

settings = {
    columns: {
      id: {
        title: 'ID'
      },
      name: {
        title: 'Full Name'
      },
      cnpj: {
        title: 'Cnpj',
        filterFunction(cell: any, search?: string): boolean {
          const searchNumber = search.replace(/\D/g, '');
          const cellNumber = cell.replace(/\D/g, '');
          return cellNumber.includes(searchNumber);
        }
      },
      email: {
        title: 'Email'
      }
    }
};

In function:

filterFunction(cell: any, search?: string): boolean {
  const searchNumber = search.replace(/\D/g, '');
  const cellNumber = cell.replace(/\D/g, '');
  return cellNumber.includes(searchNumber);
}

values are passed to two other variables without the dots, dash and bar facilitating the search and with the function includes is checked whether the values are contained, regardless of whether they are typed with formatting or without.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.