Learn to customize JavaScript grid in less than 10 minutes

Implement custom filtering logic, cell editor and renderer

Everything is a customizable component in JavaScript grid

Customers love our JavaScript Datagrid component by ag-Grid for its wide array of customization options. At ag-Grid we believe that developers should be able to extend default functionality to meet their business requirements. That’s why most of the functionality of our JavaScript grid is already component based. You can easily extend the default functionality by creating your custom components and integrating them into the grid.

In the previous article on how to get started with Javascript grid we’ve integrated ag-Grid into a sample application. If you haven’t worked through the tutorial and don’t have a sample application now, you can download it from this github repository.

The grid used in the application rendered the following data on the screen:

In this tutorial, we’ll provide more options for a user to work with the data in the “Price” column. We’re about to learn how to customize the grid with custom filters, cell renderers and cell editors. And we’ll have it all implemented and integrated in less than 10 minutes.

Custom cell renderer

First we’re going to implement a custom cell renderer to format numbers so that they are shown according to a user’s locale. Here’s how the numbers are displayed on my computer:

Custom cell editor

Since the “Price” field is numeric, it can’t contain non numeric characters. By default, when a cell is editable, users can type in any character. So we’ll implement a custom cell editor to restrict the input to numbers:

Custom filter

And we’ll finish this tutorial by implementing a custom filter. Our new filter will provide UI to type in price range and filter out cars that fall out of that range:

So let’s see how we can do that.

You can download the sample that we’ll be building in this article from github repository.

Customization through components

Our JavaScript grid is customized by implementing JavaScript components. A component is a simple a JavaScript class that implements methods for communication between ag-Grid and the component. For example, a cell editor should implement the getValue method that ag-Grid uses to obtain the value from our component and update the data. A custom column filter should implement the doesFilterPass method that processes values and so on. These required methods are defined by interfaces described in the docs.

The two methods that most components need to implement are init and getGui. The former is used to receive parameters from ag-Grid, and the latter is called by ag-Grid to get the view to renderer.

Custom cell renderer

The job of the grid is to lay out the cells. By default the grid will create the cell values using simple text. If you want more complex HTML inside the cells you need to implement cell renderers. We want to format our number according to a user’s locale so we need a custom component. The component will take advantage of the built-in function toLocaleString to format values.

Define custom component

The first task is to implement a component class. We need to define the init method that is used to receive the value of a cell through parameters. To show the formatted value we’ll use a simple span element. So, create a new file number-formatter.js and put the code for our component inside:

It’s a simple component with just two methods init and getGui that returns a span element containing formatted text.

Register the component

Now that we have our component, we need to tell ag-Grid about it. All custom components should be listed in the components configuration option:

Specify the renderer for the column

We’re almost done. The only thing that’s left is to specify our component as a cell renderer for the “Price” column. We can do that in the column definition:

Now if you run the application you should see the price formatted:

Custom cell editor

Our JavaScript datagrid provides rich editing capabilities. It resembles a spreadsheet allowing you to edit data inline. Just press F2 or double-click on a cell and ag-Grid activates the edit mode. You don’t need to provide a custom editor for simple string editing. But when we need to implement a custom logic, like restricting input to numbers, we need to create our own cell editor.

Enabling editing

Before we start working on a custom component that will act as an editor, we need to enable editing for the Price column:

Define custom component

Similarly to a custom cell renderer, we need to define a component to act as a cell editor. Let’s start with the basic implementation that will just render an input element. ag-Grid will show the input when a user activates the edit mode. To get the value from the grid we’ll use init method. Here’s the implementation:

Now we need to implement some required methods.

Once editing is finished by pressing Enter or removing the focus from the input, ag-Grid needs to get a value from our editor. To do that, it calls the getValue method on our component that should return the result of the editing:

Now we’re ready to implement the functionality to filter out non numeric characters. To do that, we need to add an event listener to the input and check each item typed in by the user. We also need to add the keyDown listener to prevent losing focus on the input when the user presses navigation keys. Here’s how we do it:

All right, we now have almost everything set up. Here’s the last thing we need to do. We want to set focus to the input in our custom cell editor as soon as the user activates the edit mode. Conveniently, the afterGuiAttached method that can be used for that purpose:

So here is the full code for you to copy/paste it:

Register the component

Similarly to a custom cell renderer, we need to register our custom cell editor in the components and specify it as an editor for the “Price” column:

And that’s it. We’re all good now to start working on our last task of implementing a custom filter.

Custom column filter

Filtering is one of the most useful features of data grids. It allows users to zoom in on a particular set of records. Our JavaScript datagrid provides a simple string filtering out of the box. But when you need you custom filtering logic types, you can easily implement it with a custom filter component. The component we we’ll be working on filters out cars based on a price range.

Enabling filtering

Before we start working on our filter component, we need to enable filtering in the grid:

Define custom component

To implement a custom filter we follow the familiar customization approach and define a component. Our filter will be rendered as an input with a button to apply the filter. Let’s create this DOM structure in the init method:

Our filter allows users to specify a range for a car price in the form of lower boundary — upper boundary:

We’ll introduce the filter property to store the current filtering condition in the component’s state. We also want to pre-fill the input with the current filtering condition when it’s shown. To do that, we’ll assign the value to input when we create it. Here’s the code that implements this:

Then we need to process user input and save it to the component’s state. To do that, we’ll register an event listener on the form and process input when the form is submitted using Apply button. Also, whenever there’s a change in the filtering condition, we not only need to update the state, but also notify ag-Grid about the change. We can do that by calling the filterChangedCallback that ag-Grid provides for us through the parameters in the agInit method:

We’re done now with the user interface.

Next, we need to implement the doesFilterPass function that performs filtering. It’s called by ag-Grid to determine whether a value passes the current filtering condition or not. The other function called by ag-Grid that we need to implement is isFilterActive. It determines whether the filter has any filtering condition to apply.

Let’s implement this functionality on our component:

Notice that we also receive the valueGetter function through parameters from ag-Grid. We then use it to retrieve the current value of a cell.

Filter presets

ag-Grid implements API that can be used to activate and deactivate filters on demand. Usually this API is triggered by some UI element, like button:

For this functionality to work, our custom component should implement two methods — setModel and getModel. ag-Grid calls them when the filter is activated and to obtain the current filtering condition. Here’s how we implement these methods in code:

We’re almost ready. The last thing we need to do is bring focus to input. To do that we’ll use the familiar afterGuiAttached method:

Complete implementation

So here is the full code of our component:

Register the component

Once we have our component ready, we need to register it in the components and specify it as a filter for the “Price” column:

Build Your Own JavaScript App With ag-Grid!

I hope that the examples above clearly demonstrated how easy it is to customise and configure our JavaScript grid.

Now take the next step and start using ag-Grid in your project!

You’ll see it for yourself how fast and extensible the grid is and what makes it the best JavaScript datagrid in the world.

Join the millions of developers and thousands of companies who use ag-Grid to provide the best possible experience to their users.

The latest update of the grid is out now.

Learn more about Version 19 of ag-Grid.


We are ag-Grid and our mission is to build the best datagrid in the world

Max Koretskyi aka Wizard

Written by

I find patterns in frameworks and explain them. Angular & React contributor. Dev Evangelist at @ag_grid. Chief Inspiration Officer at @AngularInDepth. GDE, MVP.



We are ag-Grid and our mission is to build the best datagrid in the world

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade