Search code examples
javascriptangularag-gridag-grid-angular

In ag-grid, how do I show custom options in column set filter?


My data islike this :

gridOptions.api.setRowData([
    {
      name: 'First'
    },
    {
      name: 'Second',
      isDeleted: true,
    },
    {
      name: 'Third',
      isDeleted: false,
    }
  ])
});

I want to show the Filter options, Deleted and Not Deleted.

I want the option "First" to grouped under "Not deleted" since the filed isDeleted doesn't exist.

https://plnkr.co/edit/o3sN3GodqQumVe09?preview

In this code, if I remove the "isDeleted" value, I see 3 options in the filter: "No", "No", "Yes".

I want to show 2 options only.How can I do that?


Solution

  • Have a look in FilterValueGetters (https://www.ag-grid.com/javascript-data-grid/value-getters/#filter-value-getters). If the value of the field is null then you have to set it manually

    I've adjusted your plunker to demonstrate https://plnkr.co/edit/yHaamucNQoNVzY0F

    const columnDefs = [
      { field: 'name' },
      { field: 'isDeleted',
          filter: 'agSetColumnFilter',
          valueFormatter: boolToHuman,
          filterValueGetter: (params) => {
            let value = params.data[params.colDef.field];
            if (value === null || value === undefined){
              value = false;
            }
            return myBoolToHuman(value)
           }
       }
    ];
    
    function myBoolToHuman(params) {
      return (params === true || params === 'true') ? 'Yes' : 'No';
    }
    

    Hope that helps