Games JavaScript joelf React sencha Sponsored

How to Build React 16 Web Apps with the Sencha Grid — SitePoint

Build Custom Dashboards with MongoDB, Azure & Serverless Functions — SitePoint

This text was initially revealed on Sencha. Thanks for supporting the companions who make SitePoint potential.

React 16 is the first model of React constructed on prime of React’s new core structure, codenamed “Fiber”. React 16 is designed from the floor up to help asynchronous rendering, which permits processing giant element timber with out blocking the foremost execution thread. It helps quite a few key options resembling catching exceptions utilizing error boundaries, returning a number of elements from render, lowered file measurement and help for MIT license.

In case you’re creating a data-driven net software with React 16, likelihood is you’ll use a grid or spreadsheet-like interface sooner or later to show knowledge in your customers. Relying on the context, your customers might anticipate the grid in your software to be able to:

  • Scrolling with a hard and fast header
  • Sorting by clicking on a column header
  • Displaying and hiding particular columns
  • Paging, grouping, and summarization
  • Modifying knowledge in cells
  • Exporting to Excel
  • Drilling down/row enlargement

A grid could be certainly one of the trickiest and most complicated UI elements to construct in React as a result of lots of the mandatory options require each vital React experience, in addition to the willingness and skill to dig down into the DOM. Luckily, the ExtReact Grid supplies all of those capabilities and extra.

On this article, we’re going to create an instance utilizing the Sencha ExtReact Grid that exhibits details about Shares and equities corporations. If you’d like to code a grid utilizing an HTML desk or one other third-party element, you may need to do one thing like deal with clicking on a column header to type, or clicking on a divider between a column header to resize, or perhaps sliding a pager and doing a fetch for the subsequent web page of knowledge. With ExtReact Grid, these functionalities are inbuilt. Need to attempt it your self? Get began with a 30-day free trial of ExtReact immediately — enroll right here.

Let’s get began with constructing an software utilizing ExtReact Grid.

Getting Began with ExtReact App Era

To get began on creating a React software with ExtReact elements, please comply with the steps under:

Be sure to have a Node setting arrange

First, ensure you have Node eight.11+ and NPM 6+ arrange in your system. You possibly can obtain the newest Node model from the Node website. In case you’ve already put in Node, you possibly can simply examine the node and npm variations through the use of these instructions:

node -v
npm -v

Get your login credentials for the ExtReact npm repo

ExtReact npm packages are hosted on Sencha’s personal npm repo. You log in to that repo as soon as to get entry to all ExtReact packages. To get login credentials, go to the ExtReact 30-Day Free Trial web page and fill out the type. We’ll ship you an e-mail with login particulars in addition to some hyperlinks to assets corresponding to the docs and pattern tasks.

Login to ExtReact npm repo and get ExtReact app generator

The subsequent step is to log in to Sencha’s personal npm repo, which hosts the ExtReact packages. Use your npm login (offered in the e-mail) to affiliate the repo with the @sencha scope, and enter the credentials when prompted:

npm login — registry= — scope=@sencha

The subsequent step is to set up ExtReact generator package deal.

npm set up -g @sencha/ext-react-gen

Create your first React App

Run the Yeoman generator to create your first ExtReact app:

ext-react-gen app your-app-name-here -i

The generator will ask you to identify your app, identify the npm package deal, and choose a theme. The default Materials theme (based mostly on Google’s Materials design tips) is an effective selection as a beginning theme. Choose “Generate an Empty App”. The generator may even immediate you to create a brand new listing in your undertaking. The generator will then obtain and create your pattern software, together with related dependencies.

Run your React App

In the generator output, you will see that steps to run your software. It’s so simple as altering to your new software listing and operating the software utilizing:

npm begin

This can hearth up the app, your empty React app will simply present up with the title of the app. The primary element (e.g. StocksGrid) in the software has one container at the root, which is marked as full display, format is about to match, which suggests it’ll stretch its baby to fill it.

See the code up to this step on GitHub.

Including a Grid to the software

Add Shares Knowledge

We’ll be including an instance knowledge set, referred to as shares.json to the software. It’s a pretty big knowledge set, round 10,000 rows in json, and every row represents an organization or ticker image — so we’ve the identify of the firm, ticker image, sector, industries they’re in, and an array of ticks that are the final 5 gross sales of that inventory. That is the knowledge we’re going to show in our grid. In a real-world software, all of this knowledge can be returned on the back-end. We’re going to load it statically for this pattern software moderately than undergo all of the mechanics of how to construct a back-end relaxation API. Nevertheless it’s going to be loaded in the very same approach you’d fetch from an actual back-end.

Making a Primary Grid

In the StockGrid element render technique, we’re going to return a grid with columns.

To place columns in our grid, we use a column element, and it takes a knowledge index that’s the similar as the identify area of the shares knowledge. It takes a textual content prop that’s the column header textual content, after which we will additionally give the column a width, like a hard and fast width or a flex or a mixture of flex and minimal or most as nicely. We’ll add column elements for firm identify, image, ticks, sector, and business. Create a brand new StocksGrid class with Grid as proven under:

<Grid >
<Column dataIndex=”name” textual content=”Name” width=300 />
<Column dataIndex=”symbol” textual content=”Symbol” />
<Column dataIndex=”ticks” textual content=”Trend” />
<Column dataIndex=”sector” textual content=”Sector” width=200 />
<Column dataIndex=”industry” textual content=”Industry” width=350 />

Now, add StockGrid to Class App as proven under:

export default class App extends Element
return (
<StocksGrid />

See the code up to this step on GitHub.It is possible for you to to see the net software with empty Grid on npm begin.

Binding Inventory Knowledge with Grid

A grid in ExtReact is a knowledge desk that pulls in and renders knowledge from an Ext Knowledge Retailer. In ExtReact, our retailer implementation is a knowledge construction that permits you to type and filter knowledge for a grid or elements (like lists or charts).

We will now begin by loading the shares knowledge and making a retailer. Once more, grids all the time seize their knowledge from the retailer, and a few of the interactions with grid will set off occasions on the retailer, like reloading or sorting or paging. So to do this, we’ll create our retailer right here.

The Ext knowledge retailer is totally different from the flux retailer. What makes the grid and the retailer just a little totally different from the commonplace React strategy is that the two are tightly built-in. Sometimes, you’ll be able to cross knowledge instantly to a retailer, or a retailer can pull knowledge by itself from a back-end utilizing a proxy. With ExtReact Grid, you get interactive performance like filtering, sorting, paging, grouping, and summarization with out having to truly code it.

For this instance, we’re passing the knowledge instantly to the retailer from the Shares knowledge file. You can too create a retailer with a proxy config — having a proxy permits us to do all types of nice issues like distant paging, filtering, and sorting. We set autoload to true, so it mechanically masses the grid. The info isn’t notably sorted by any standards, so we’re going to have it type on the client-side by specifying the identify property.

this.retailer = new Ext.knowledge.Retailer(
knowledge: shares,
autoLoad: true,
sorters: [
property: ‘name’
replace: this.onRecordUpdated


In the Grid, assign the retailer config to the retailer that was created.

<Grid retailer=this.retailer>


Now, we have now a grid with all the knowledge as proven under:

With this easy code, you get numerous options free of charge — reminiscent of sorting — which permits you to click on on any column header and it mechanically types (see the image column in the instance under).

On this case, the sorting is completed on the shopper aspect. But when we carried out an actual back-end API, we might configure the proxy to do distant sorting on the back-end and use an “order by clause” in the database to do a kind.

You additionally get resizable columns without spending a dime. So although we set a width on these columns, if the consumer needs to see one thing or shut one thing, he can do this by dragging the column from aspect to aspect.

You additionally get a pleasant grouping function too. So if we needed to group by business, lets say group by this subject, and it’ll group all of the knowledge by the worth of business, and it provides you with a pinned header as you scroll down for every of the groupings.

Grouping by Industry

You’ll discover that this knowledge is rendering fairly shortly for 10,000 data. The rationale it renders so shortly is as a result of it’s utilizing what we name buffered rendering. So regardless of the proven fact that this desk seems to be full, and you may scroll down and it retains going and going, on preliminary loading it solely renders a bit of bit greater than what you’re truly seeing when it comes to the “view port height.”

As you scroll down, it’s truly changing the content material of the cells with the newer data as you web page down in the retailer. So it’s truly conserving the DOM parts as a lot as potential, and holding the DOM small, retains reminiscence consumption small and ensures excessive efficiency.

See the code up to this step on GitHub.

Styling Your Grid

There are a number of methods to type the Grid, so the knowledge is simpler to analyze.

Utilizing Cell prop

Let’s check out controlling the styling of cells. We will make the Identify daring — the greatest method to do that’s through the use of the cell prop. The cell takes quite a few configs that controls what the cell seems to be like. We’ll throw a method config on there, after which we’ll say fontWeight equals daring.

cell= type: fontWeight:’daring’

Including a Button in a Row

Now, we’ll do one thing extra relevant to the actual world. Let’s say we would like to have a button that we will click on to purchase considered one of these shares on the left-hand aspect column. To try this, we will add a column, and this time we’re not going to add a knowledge index as a result of it doesn’t correspond to any specific merchandise in the subject, or any area in the retailer. We’re going to add a WidgetCell with a Button. We’ll do some styling — we’re going to put motion spherical UI on it, so the button could have a spherical motion look:

<Column >
<Button ui =”round action”
handler = this.buyHandler
textual content = “Buy”/>

The purchase handler that we’re going to use could be very easy. Whenever you click on the purchase button, we’re simply going to use the assumption referred to as ext.toast that may show slightly toast message at the prime that claims the image of the inventory that you simply’re shopping for. Once more, we’ve got our column that we’re going to render for every row. We’re going to render this button, after which once you click on it, it’s going to name this perform:

buyHandler = (button) =>
let gridrow = button.up(‘gridrow’),
document = gridrow.getRecord();
Ext.toast(`Purchase $report.get(‘identify’)`)

Adding button in a grid

You’ll be able to see from this instance which you could principally embed any ExtReact element inside a grid cell, and it’s absolutely interactive. It features as a full React element.

See the code up to this step on GitHub.

Including a Developments Sparkline Chart

In Inventory Knowledge, we now have this array of ticks on the final 5 Inventory gross sales. Let’s embed that as a Sparkline chart inside the grid. We’re going to use widgetcell to render the ExtReact element inside a grid cell.

<Column dataIndex=”ticks”
textual content=”Trend”
cell =
xtype: ‘widgetcell’,
forceWidth: true,
xtype: ‘sparklineline’,
‘Worth: y:quantity(“0.00”)’


As you employ your mouse to hover over totally different factors in the line graph, it’s going to show the Y worth formatted with two decimal factors.

Trends chart in a grid

See the code up to this step on GitHub.

Exporting Knowledge to Excel

One among the widespread necessities in data-intensive purposes is to export the knowledge to Excel. ExtReact makes it straightforward to do this through the use of the plugins prop for a grid.

gridexporter: true,


To make it straightforward to name the export performance, we’ll add a number of extra elements. We’ll add titlebar and dock titlebar at prime of grid and put a menu inside. Whenever you click on on the “export” button, you’ll have the choice to export both to Excel or CSV.

<TitleBar docked=”top” title=”Stocks”>
<Button align=”right” textual content=”Export”>
<Menu indented=false>
<MenuItem textual content=”Excel”
this.export.bind(this, ‘excel07’)/>
<MenuItem textual content=”CSV”
this.export.bind(this, ‘csv’)/>

The export handler will cross alongside the sort of the export and extension of the filename:

export = (sort) => this.grid.cmp.saveDocumentAs(
sort, title: ‘Shares’ );

Be sure to have the exporter in the package deal.json dependencies. For instance:

“@sencha/ext-exporter”: “~6.6.0″

Set up the dependency.

npm set up
npm begin

The Exporter plugin allows knowledge export to numerous file codecs. It helps native XSLX, Excel XML in addition to HTML and CSV/TSV (comma/tab separated worth) codecs.

Exporting grid data

See the code up to this step on GitHub.

Including an Modifying Functionality to a Grid

We will make the grid extra like a spreadsheet by including the functionality to edit the knowledge. So as to add that functionality, we’ll want to add one other plugin referred to as gridcellediting. By including this plugin and marking columns that may be edited as editable, you now have grid knowledge that may be edited by double-clicking on any grid cell. You’ll be able to proceed to edit the grid by tabbing by way of the grid cells.

Including grid cell modifying plugin:

gridcellediting: true

Making “Name” editable:

<Column dataIndex=”name” textual content=”Name” width=300 cell= fashion: fontWeight:’daring’ editable/>

Edit capability in a Grid

Dealing with Edit Occasions

If you would like to do one thing particular after modifying the grid cell, you possibly can pay attention to that occasion on the retailer for knowledge modifications. You do this by including a listener config and a listener for “update event”.

The “update event” will cross numerous parameters together with retailer, up to date report, the object that describes the operation that occurred, after which passes an array of modified subject names. You’ll add that in the handler. On this case, we’re simply displaying a toast message. In the real-world software, you’d truly apply enterprise logic reminiscent of persisting change in the database.

replace: this.onRecordUpdated

onRecordUpdated = (retailer, report, operation, modifiedFieldNames) =>
const area = modifiedFieldNames[0];
$subject up to date to $document.get(area)`)

Including a Choose Choice to a Grid Cell

If you need to add a “Select” choice to a Grid cell, you are able to do that utilizing one other ExtReact element referred to as SelectField. You simply add the SelectField ExtReact element in the required Column.

<Column dataIndex=”sector” textual content=”Sector” width=200 editable>
<SelectField choices=sectors/>

Adding select option to a grid

See the code up to this step on GitHub.

Optimizing Cross-platform Expertise

This works nicely for the desktop expertise, however as you reduce the display measurement for the cell phone expertise, the cell modifying will not be the greatest expertise for modifying. For small display units, it’s your decision to select a unique modifying type.

The platformconfig choice permits you to specify conduct for desktop or cellular. You possibly can set any prop to a special worth based mostly on platformConfig and right here we’re setting the plugin based mostly on the platform. On this instance, we’ll use gridcellediting when the software is on desktop. When the software is on cellular, we’ll use grideditable which offers a greater method to edit knowledge on cellular units.

gridexporter: true,
gridcellediting: true

gridexporter: true,
grideditable: true

See the code up to this step on GitHub.

You need to use the ExtReact 6.6 grid and add spreadsheet-like interface simply in your data-driven net software utilizing React 16. With ExtReact, you need to use any Ext JS trendy element together with grid, tree grid, pivot grid, charts, D3 visualizations and extra — no customization required. You get to construct an optimized and minimized package deal that accommodates solely the elements you’re utilizing in your app. You’ll be able to construct immersive consumer engagement through the use of layouts and adaptive design options that appears nice on each desktop and cellular units. As a reminder, you will get began with ExtReact at no cost at this time with a 30-day trial — enroll right here.