DataGrids are one of the most powerful, popular components in enterprise applications. Almost all of our customers use FlexGrid in their applications. Grids can be very complicated and are usually too cumbersome to develop on your own, which is why many people turn to FlexGrid (or other grid components) instead of rolling out their own. When you use FlexGrid, you can spend your time building an application instead of creating and maintaining a complex data grid.
When you use FlexGrid, you can spend your time building an application instead of creating and maintaining a complex data grid.
When selecting a JavaScript data grid, make sure you ask: how flexible and customizable is this grid? Naturally, different applications require vastly different grid implementations. Most grids are somewhat flexible, but FlexGrid is built to be (it's in the name, after all). Our philosophy with FlexGrid is to make the core grid very small and very fast... Then we make it extensible it can be customized to any need without bloating the core.
One of the most common ways to customize a grid is to create custom cells. Custom cells bring a grid’s data to life, making it more than just a data table. Here are some examples of custom cell rendering:
FlexGrid has many different ways to create custom cells.
Let’s take a look at these different implementations in more detail.
The formatItem event occurs when an element representing a cell has been created. This event can be used to format cells for display. This event is fire for all cells (including headers), not just data cells. So this event is very useful for formatting any cell in the entire grid.
The formatItem event provides a FormatItemEventArgs class as its arguments, which contains everything you need to customize the cell being rendered (and more).
Let’s take a look at a simple example of customizing cells using formatItem.
In this example, we will do some conditional formatting to change the color of the cell based on its value. This will be a visual indicator if sales are too low or high in the Sales column of our grid.
You can look through the boilerplate code in the sample, but let’s look at the formatItem event handler.
formatItem: function(s, e) {
//check if it is a data cell, then check to see if it is the 'sales' column
if (e.panel == s.cells && s.columns[e.col].binding == 'sales') {
//get the data item related to the row
var item = s.rows[e.row].dataItem;
//if the item.sales are below 1000, add a 'low' class, otherwise remove the class if it exists
wijmo.toggleClass(e.cell, 'low', item.sales < 1000);
//if the item.sales are above 5000, add a 'high' class, otherwise remove the class if it exists
wijmo.toggleClass(e.cell, 'high', item.sales > 5000);
}
}
A couple of interesting things about this code:
Now that you know the basics of formatItem, let’s look at some more complicated implementations.
This sample extends the previous sample to add some calculated columns and custom icons on top of the conditional formatting.
This example adds custom controls into the cells to display sparklines and star ratings. These are just examples, and anything could be rendered inside each cell.
This sample shows how to add buttons into cells that can perform custom actions. In this instance, the buttons actually change the row to edit mode when clicked. This shows how easy it is to change the editing behavior of the grid from its default (single-cell editing) behavior.
Read the formatItem documentation or explore more custom cell samples in Learn Wijmo.
This section could (and will) be an article all on its own, but we'll take a quick look at it now. Cell Templates are something we added to our Angular, AngularJS and Knockout interops that enable you to declare cell templates in markup. This allows us to easily create custom cells in markup without having to use any event handlers.
Cell Templates are our favorite way of creating custom cells. Let’s take a look at an example to see why.
Cell Templates are our favorite way of creating custom cells.
In this simple Angular example, we'll add a button to a column. The beauty of this sample is that it's all done in Angular markup.
Here's the entire markup for the grid. You can see the second column uses a cell template, and the third column uses a cell edit template:
<wj-flex-grid [itemsSource]="data" style="height:300px">
<wj-flex-grid-column [header]="'Country'" [binding]="'country'"></wj-flex-grid-column>
<wj-flex-grid-column [header]="'Template'">
<ng-template wjFlexGridCellTemplate [cellType]="'Cell'" let-cell="cell">
<button class="btn btn-default" (click)="clickItem(cell.item.country)">
<img src="http://flagpedia.net/data/flags/mini/{{cell.item.abb}}.png"/>
Click Me!
</button>
</ng-template>
</wj-flex-grid-column>
<wj-flex-grid-column [header]="'Downloads'" [binding]="'downloads'" [format]="'n0'">
<ng-template wjFlexGridCellTemplate [cellType]="'CellEdit'" let-cell="cell">
<wj-input-number [(value)]="cell.value" [step]="1"></wj-input-number>
</ng-template>
</wj-flex-grid-column>
</wj-flex-grid>
Some interesting things about this sample:
In this sample, you'll see the full power of our Angular Cell Templates. It uses all the different cell template types to customize every cell in the grid.
Last, and certainly most difficult, is the CellFactory approach. You'd use this only if you need to completely take over how the grid renders every cell. We do not recommend this unless you really need it (and some do).
We aren’t going to go into the code for this implementation since it's so complicated. But rest easy knowing that if you really want to take over rendering the FlexGrid, you can do so. Look over the code in our CellFactory sample if you want to see how to follow this approach on your own.
Now you’ve seen the different ways to implement custom cells in FlexGrid: formatItem event, Angular Cell Templates and Cell Factory. We suggest using the formatItem event or Cell Templates if you're working with Angular. The samples above should help you get started on customizing your own FlexGrid.