DEV Community

Cover image for The Definitive Guide to JavaScript Datagrids
Chelsea Devereaux for MESCIUS inc.

Posted on • Originally published at Medium

The Definitive Guide to JavaScript Datagrids

DataGrids have been one of the most common visual software elements since the advent of UIs. They appeal to the instinct that drives all of us to take in and understand as much data as possible as quickly as possible. DataGrids are more powerful than ever now that they run in JavaScript.

JavaScript is a ubiquitous framework for building modern web applications. It offers language for enterprise developers to build applications that can run on any device with a browser.

JavaScript is a powerful language but does not include UI components or, more specifically, a JavaScript DataGrid. However, there are open-source and third-party options developers can leverage. A JavaScript DataGrid is a custom JavaScript component similar to traditional DataGrids, but that can be used in web applications.

Why DataGrids?

Even if you’re not a developer, you’re probably familiar with the paradigm of a DataGrid. Grouping and sorting data isn’t merely for the meticulous; it’s human nature. DataGrids appeal to the instinct that drives us to organize, absorb, and understand as much data as possible as efficiently as possible.

The usefulness and universality of DataGrids have allowed them to permeate throughout time, but not without some amount of evolution. With the advent of screens in the digital age, the DataGrid has changed more than ever.

Now, grids are more useful than ever, not only for displaying data but also for editing it. Imagine creating a sales report or monthly budget using Microsoft Word. Microsoft Excel would be the natural choice for most users with automatic conveniences, such as sorting, filtering, and autocomplete, plus advanced features like custom formulas.

Beyond the helpful and time-saving features, most users would naturally choose Excel because it’s what they first experienced when it came to DataGrids on a computer. Excel is familiar. Any good UX designer or business analyst will tell you educating users is difficult and expensive. One of the most significant advantages of DataGrids, in general, is that they are intuitive.

Although Excel has become the most common example of software using a DataGrid, the paradigm has increased throughout the software world. As web apps continue to replace the traditional native applications that we’re used to, it only makes sense that DataGrids would also make their way to web app UIs.

At MESCIUS, we’ve found that industries across the board are including DataGrid UI components in new web app projects.

Whether you’re building an app to track manufacturing stats, analyze financial data, or create sales reports, we’re willing to bet you’ve already considered the intuitiveness a DataGrid would add. As we’ll see, Wijmo’s JavaScript DataGrid can maximize the bang for your buck by providing the best DataGrid UX with the best performance available.

FlexGrid
JavaScript DataGrid components, like FlexGrid, provide natural intuitive interfaces between humans and data.

Tables vs. DataGrids

If you aren’t using a DataGrid, then you are likely using an HTML table element in your application to display lists of data. HTML tables are an excellent tool for basic data display, but they quickly fall short when your end users expect Excel-like features in your app.

Even relatively simple features, like dynamic sorting and filtering, are not built into HTML tables. The list of DataGrid features that tables do not include is too long to define.

You might think HTML tables perform better than DataGrids, but that is only true in specific scenarios. Tables can outperform DataGrids when there are a limited number of rows and columns. The simple reason is that HTML tables require DOM to be rendered for all columns, rows, and cells. DataGrids, on the other hand, can virtualize rendering so that only visible cells are rendered and recycled as the view changes. So, when working with large volumes of data, DataGrids outperform tables by far.

When choosing between Tables and DataGrids, we think it comes down to two key factors. If either or both are true, then you need a DataGrid:

  1. Do your users want to interact (organize, analyze, edit, etc.) with your data in a way that is similar to Excel?
  2. Do you need to bind to thousands of records?

Again, we think if either of these is true, then a DataGrid is the right choice for you. If not, then a simple HTML table should suffice.

The Top Features of a JavaScript DataGrid

A JavaScript DataGrid provides various functionalities to the applications that contain them. The primary uses include displaying, editing, and analyzing data. Let’s look at the top features for each use case and how we define them in a DataGrid API.

Top Displaying Features

JavaScript DataGrid display features help users read and understand the data more quickly and efficiently. Developers can improve how the raw data is presented to the user by making it more readable with cell formatting, merging, and column bands. Users can make the data easier to view with features like column reordering, resizing, and scrolling.

Collapsible row details
JavaScript FlexGrid with collapsible row details

A more complete list of top display features includes:

  • Cell formatting — Format numbers and dates such as rounding numbers, displaying percentages, and displaying dates as month names.
  • Cell merging — Adjacent cells containing the same values are merged into one larger cell, either automatically by the application or manually by the user.
  • Custom cell rendering — Add custom UI elements within a DataGrid cell, such as buttons, images, or virtually anything that can be rendered in JavaScript. This is sometimes referred to as a Cell Template.
  • Column bands — A visual grouping of related columns, such as grouping Street, City, and Postal Code under an “Address” band.
  • Column reordering and resizing — At runtime, users can change the order and size of columns to suit their needs.
  • Scrolling — Scrolling is essential to reach additional data rows and columns that don’t fit on the screen.
  • Collapsible row details — Display less important information about a record within a collapsible panel on each row.
  • Frozen columns and rows — Affix any number of rows and columns to prevent them from scrolling out of view.
  • Right-to-Left — Adjust the columns and text to read from right to left, a critical feature for some cultures.
  • Themes and styles — Provide more than one aesthetic to deliver modern-looking or company-branded UIs. A rich UI control provides extensive style customization.

Top Editing Features

JavaScript DataGrid editing features enable users to insert, update, and even delete data at the underlying source. While extra code is typically required to complete the connection and pass updates to the data source, the DataGrid control is responsible for accepting the input from the user in the most efficient and intuitive ways possible.

Input validation errors
JavaScript FlexGrid with input validation errors

For example, when you need to make your data editable by a user, you can build a JavaScript application containing a DataGrid. Users can navigate the DataGrid cell by cell and insert or edit the values. Additional editing features include adding new rows, input validation, clipboard support, importing data from external files, and even specialized cell editors such as calendars for dates, checkboxes for Boolean values, etc. A more complete list of top editing features includes:

  • Cell navigation and editing by keyboard — Allow users to navigate and edit cells through the keyboard. We often follow Microsoft Excel behavior using a combination of the tab, arrow, control and enter keys to perform all editing operations.
  • Read-only cells and columns — Make any column or cell non-editable.
  • Input masking and validation — Prevent the user from entering invalid information, such as poorly formatted emails, IDs, phone numbers, locations, and date ranges.
  • Select cells — Allow users to select or highlight any number of rows or individual cells. Cell selection is often used for additional drill-down or export functions so that the developer may restrict the selection based on the application requirements.
  • Add new rows — Allow users to add new records to the data source through the DataGrid itself. While many UI designers choose a separate input form for this operation, it’s helpful to have this feature in the DataGrid for quicker solutions.
  • Delete rows — Allow users to delete records from the data source through the DataGrid itself. This can be supported with the delete key or a button embedded in a cell.
  • Custom cell editors — Specify a particular editor for certain columns and data types. For example, editing colors is best implemented with a color picker rather than a textbox.
  • Clipboard support — Enable users to use their keyboards to cut, copy, and paste commands to edit the DataGrid.
  • Importing data — Import tabular data from common sources like Microsoft Excel, CSV, JSON, and XML.

Top Analyzing Features

While displaying and editing data are the most common use cases, the true potential of a JavaScript DataGrid lies in its analytical features. DataGrids make great analysis tools, especially for creating “live” reports, and are often included in dashboards.

tree-grid subtotals
JavaScript FlexGrid control with tree-grid subtotals

Advanced JavaScript DataGrids typically include analytical features like sorting, grouping, and filtering. These actions allow the user to drill-down, slice, and aggregate the entire data set to discover trends and insights in the data set. For example, sorting and grouping allow the user to organize the data meaningfully to find the highest or lowest records. Filtering is an essential feature that lets the user search and find specific records. A more complete list of top analyzing features includes:

  • Sorting by column — Allow users to sort the entire DataGrid by a single column’s values in ascending or descending order. This is typically performed by clicking the column header and displaying an up or down arrow.
  • Sorting by multiple columns — Allow users to perform a sort across multiple columns simultaneously. This is typically done by clicking the column header while simultaneously pressing a key.
  • Grouping by column — Group the data within the DataGrid by a single column, which organizes the data similarly to sorting but with added header rows to separate each group.
  • Drag-and-drop Grouping — Allow users to perform grouping at runtime by dragging any number of column headers into a grouping panel or by grouping through a menu.
  • Excel-like Filtering — Provide filters within each column header to enable conditional and value-based filters for the user.
  • Filter row — Provide a filter input box for each column in a fixed row at the top or bottom of the DataGrid.
  • Full-text filtering (search box) — Provide filtering functionality to the DataGrid from an external text box. This is commonly referred to as “searching” as it feels like a Google search box for your DataGrid.
  • Subtotals and aggregates — When a DataGrid is grouped, it may optionally display aggregated subtotals for each group. The aggregated values are typically displayed in the group header row. Totals may also be calculated for the entire data set and displayed at the bottom footer of the DataGrid.
  • Conditional formatting — Apply special formatting to cells based on their value. A common example is shading cells with different colors based on various thresholds.
  • Custom calculations — Provide additional calculations that may not be present in the raw data. For example, if your data set includes column fields for “Price” and “Discount,” the DataGrid could display a custom column with a calculation of Price*Discount.

How to Build a JavaScript DataGrid

Follow these steps to get started using a third-party JavaScript DataGrid, such as the Wijmo FlexGrid component.

To start, we will assume you have an existing JavaScript application.

Step 1: Install Packages from npm

To use the JavaScript FlexGrid, we will need to add the dependent packages from npm. This command will install all of the required packages.

npm install @mescius/wijmo.purejs.all

Enter fullscreen mode Exit fullscreen mode

Step 2: Add Host Element

In our HTML, we add a <div> element to host the JavaScript DataGrid.

<div id="myGrid" style="height: 500px;">
</div>
Enter fullscreen mode Exit fullscreen mode

Step 3: Import and Initialize the Grid

Now that we have the required packages and host element, we can import the grid module and initialize our DataGrid in JavaScript.

import * as wjcGrid from '@mescius/wijmo.grid';
let grid = new wjcGrid.FlexGrid('#myGrid');
Enter fullscreen mode Exit fullscreen mode

Step 4: Import Required CSS File

For FlexGrid to look and work correctly, you need to load Wijmo CSS files into your application. The styles are shipped in the @mescius/wijmo.styles npm package.

You can load styles in your application by adding this import statement to the top of the styles.css file:

@import '@mescius/wijmo.styles/wijmo.css';
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the JavaScript DataGrid

That’s it! Just run your application and see your JavaScript DataGrid in action. Your grid will already support sorting, editing, selection, and much more.

Run JavaScript datagrid

How to Optimize JavaScript DataGrid Performance

As discussed earlier, features are important, but performance is arguably an even more significant benefit of DataGrids when working with large volumes of data. Let’s look at some techniques that can improve DataGrid performance.

  • Modular architecture — The best DataGrids are built using a module design pattern, so only key features are included in the core grid, allowing developers to pick and choose which specialized features to include. By only including what is needed, our applications will stay lean.
  • Leverage virtualization — Virtualization gives DataGrids the ability to outperform HTML tables so that not all cells need to be created as DOM elements. Because of this, it’s essential to choose a DataGrid that supports virtualization to leverage this feature. We want to ensure that the DataGrid has a height and width set so that it is scrollable. This scrolling is what allows virtualization to render only cells needed inside the ViewPort and recycle cells as the user scrolls.
  • Defer updates during configuration — DataGrids are built to rerender when changes are made, so we need to consider that when configuring them. For example, if we have code that is making many changes to the grid, we want to defer rendering until we are done making the updates. The most common use case for this is when the grid is configured or major data changes are being made simultaneously.

Specialized JavaScript DataGrids

The sky is typically the limit regarding web development, so it’s no surprise that there’s still a lot more you can do with a JavaScript DataGrid. Developers and teams alike can keep adding more and more features to a single DataGrid library, but at a certain point, it makes more sense to branch some features off into a specialized DataGrid control. Some popular examples of specialized DataGrids include:

Pivot Grid

A DataGrid that displays a cross-tab view of one or more dimensions with built-in aggregation and extensive runtime configuration.

Pivot Grid
JavaScript PivotGrid showing aggregate data for analysis

Transposed Grid

A DataGrid that renders rows horizontally instead of vertically. This is a very specialized use case.

Transposed Grid
JavaScript Transposed Grid

MultiRow Grid

A DataGrid that renders single records in multiple rows of cells. This style of grid is very popular in Japan.

MultiRow Grid
JavaScript MultiRow Grid

Conclusion

The HTML table is good for simply viewing and editing tabular data in your JavaScript applications, but for more advanced features, such as built-in analysis and data operations, you will want to explore third-party JavaScript DataGrids. You can use the feature list from this article as a checklist when you compare different third-party DataGrids. Try it for yourself with Wijmo’s FlexGrid JavaScript DataGrid samples.

Top comments (0)