Ag-grid valueFormatter and Column Filter

2 min read 05-10-2024
Ag-grid valueFormatter and Column Filter


Mastering Ag-Grid: Value Formatters and Column Filters for Enhanced Data Display

Ag-Grid is a powerful JavaScript data grid that empowers developers to display and interact with large datasets efficiently. One of its key strengths lies in its flexibility and customizability, allowing you to tailor the grid's appearance and functionality to your specific needs. This article will delve into two key features: value formatters and column filters, explaining how they work and providing practical examples to guide you in maximizing their potential.

Understanding Value Formatters and Column Filters

Imagine you have a spreadsheet with a column containing numbers representing currency values. You might want to display them with a dollar sign and comma separators for readability. This is where value formatters come in handy. They allow you to transform raw data into user-friendly representations.

Column filters, on the other hand, enable users to quickly narrow down data based on specific criteria. For example, you could filter the currency column to show only values greater than $1000.

Practical Implementation: Value Formatters

const columnDefs = [
  {
    field: 'price',
    headerName: 'Price',
    valueFormatter: function(params) {
      return '{{content}}#39; + params.value.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
    },
  },
  // ... other column definitions
];

In this example, the valueFormatter function is used to format the price column. It appends a dollar sign, rounds the value to two decimal places, and adds commas as separators for thousands.

Here's a breakdown of the valueFormatter function:

  • params.value: Contains the raw data value for the current cell.
  • .toFixed(2): Rounds the value to two decimal places.
  • replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'): Adds commas to the number string for thousands separation.

Implementing Column Filters

const columnDefs = [
  {
    field: 'category',
    headerName: 'Category',
    filter: 'agTextColumnFilter', // Apply text filter
  },
  // ... other column definitions
];

const gridOptions = {
  // ... other grid options
  defaultColDef: {
    filter: true, // Enable filtering for all columns
  },
};

In this example, the category column is explicitly set to use the agTextColumnFilter. Alternatively, you can enable filtering for all columns using the defaultColDef option.

Ag-Grid provides several built-in filter types:

  • agTextColumnFilter: For filtering text values.
  • agNumberColumnFilter: For filtering numeric values.
  • agDateColumnFilter: For filtering date values.
  • agSetColumnFilter: For filtering based on a predefined set of values.

You can further customize filters by adding custom filter params:

const columnDefs = [
  {
    field: 'price',
    headerName: 'Price',
    filter: 'agNumberColumnFilter',
    filterParams: {
      filterOptions: ['equals', 'lessThan', 'greaterThan'], // Specify allowed filter options
    },
  },
  // ... other column definitions
];

Conclusion: Unleash the Power of Ag-Grid

Value formatters and column filters are essential components for enhancing the user experience within Ag-Grid. By leveraging these features, you can transform raw data into a visually appealing and easily navigable grid, empowering users to extract valuable insights from their data. Remember to explore the comprehensive documentation and examples provided by Ag-Grid to unlock the full potential of these features.

References: