Home > Enterprise >  How do you add a JSON filter before fetching data for ag-grid?
How do you add a JSON filter before fetching data for ag-grid?

Time:04-06

I am trying to pull data from a JSON file into an AG-Grid. I am able to pull all of the data into the table, but I want to apply a filter before the data gets into the table. I want to be able to use one of the fields to determine if that row should be added to the AG-Grid table.

Currently using this for all data:

  fetch('https://raw.githubusercontent.com/tjbreshears/VBelo/main/outputs/teams.json')
      .then(response => response.json())
      .then(data => {
          gridOptions.api.setRowData(data);
      });

I want to add something like:

jsonObject.filter(obj=> obj.eligible == "1");

CodePudding user response:

You could apply the filter to the data, thereby creating a new array, and then use that filtered array to set the data in the grid.

 
fetch('https://raw.githubusercontent.com/tjbreshears/VBelo/main/outputs/teams.json')
      .then(response => response.json())
      .then(data => {
          filteredData = data.filter(obj=> obj.eligible == "1");
          gridOptions.api.setRowData(filteredData);
      });
  • Related