Filter using masks no n2-smart-table

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 should return the same record when searching for the filter .

I searched through various sources but could not find a solution.

Thank you in advance!

    
asked by anonymous 14.08.2018 / 14:27

1 answer

1

There is an item in the documentation that can be prepared a custom function for filter in question it says that the value has to be searched without the dots, dash and slash, so inside the settings in the columns set item within the field referring to cnpj filterFunction same as 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 making it easier to search there and with the includes function checked, whether the values are contained, regardless of whether they are typed with or without formatting. / p>

References:

14.08.2018 / 15:47