pranami
pranami

Reputation: 1415

Multiple filters functionality not working properly

I have an array of objects in the below format. If IsEnabled is 0, the value shown in UI is Disabled and for IsEnabled value 1, the value is Enabled.

[
    {
      AlertID: "A1"
      DisplayName: "B1"
      Group: "C1"
      IsEnabled: 0
    },
    {
      AlertID: "A2"
      DisplayName: "B2"
      Group: "C2"
      IsEnabled: 0
    },
  ]

I have a function for filtering out the data from all the 4 columns. When I try to filter out the values from IsEnabled, the other filters doesn't work. Filtering only the Enabled and Disabled alone works fine.

multiFilter(data: any[], filters) {
    const filterableColumns = Object.keys(this.sortedsearchkeys);
    const result = filterableColumns.reduce((acc, field) => {
      if (Array.isArray(filters[field]) && filters[field].length) {
        if(filters.IsEnabled[0] === 'Enabled') {
                return acc.filter((item) => {
                           return item.IsEnabled == 1
                        });
            }
        else if(filters.IsEnabled[0] === 'Disabled') {
                    return acc.filter((item) => {
                              return item.IsEnabled == 0
                            });
                }
                
       return acc.filter((item) => {
          return filters[field]?.length && filters[field].indexOf(item[field]) > -1
        });
      } 
      else {
        return acc;
      }
    }, data);
    return result;
  }

I am not able to figure out how to filter out the Enabled/Disabled along with the other 2 columns.

Upvotes: 0

Views: 85

Answers (1)

trincot
trincot

Reputation: 349906

You check for IsEnabled in each iteration, irrespective of what field is. Instead you should check whether field really is about IsEnabled and only then perform the logic that is specific to this field:

if (field == "IsEnabled") {
    let target = ["Disabled", "Enabled"].indexOf(filters.IsEnabled[0]);
    return acc.filter((item) => {
        return item.IsEnabled == target;
    });
}

Upvotes: 1

Related Questions