Search code examples
angularrouteparams

Get params Angular


I have problem with params. I am using filter method to get every key, value from different componet (form).

The problem is with result in the end.

 filterTable(filters: { data: any; isReset: boolean }) {
    let params = new HttpParams();

    for (const key in filters.data) {
      if (Object.prototype.hasOwnProperty.call(filters.data, key)) {
        const jsonData = JSON.stringify({ drilldown: { [key]: { op: 'EQ', value: filters.data[key] } } });
        params = params.append('filter', jsonData);
      }
    }

enter image description here

Is there way how could i not add in query params key with null value?


Solution

  • If you want to remove the entire filter, just add this condition to your if : filters.data[key]

    for (const key in filters.data) {
        if (Object.prototype.hasOwnProperty.call(filters.data, key) && filters.data[key]) {
            const jsonData = JSON.stringify({ drilldown: { [key]: { op: 'EQ', value: filters.data[key] } } });
            params = params.append('filter', jsonData);
        }
    }