How to run TreeGrid React examples
TreeGrid examples package contains simple examples for React framework located in
/ExamplesNode/React/.
There are two example sets for plain React without NodeJS created statically and dynamically located in
/basic-* directories
and next six examples for
create-react-app with and without TypeScript.
The examples are for React versions
19,
18 and
17, but they were tested and work without changes also in React version
16 and
15.
Plain React examples (without installation and NodeJS)
-
Copy the whole TreeGrid package to your web server to be accessible by browser.
Especially the /Grid/, /ExamplesNode/React/basic-static/ and /ExamplesNode/React/basic-dynamic/ directories. Preserve the directory structure when copying.
-
Run the below examples links from your http server url in your browser. (Some examples require NodeJS server, see its installation below.)
/ExamplesNode/React/basic-static/Index.html creates TreeGrid statically by HTML tag <treegrid>.
/ExamplesNode/React/basic-dynamic/Index.html creates TreeGrid dynamically by JavaScript function TreeGrid().
If you have any problems running React examples please contact our technical support.
Install and run React examples created by NodeJS template create-react-app (with and without TypeScript)
-
Go to directory /ExamplesNode/React/react-app19/ for React with JavaScript.
Or go to /ExamplesNode/React/react-app-typescript19/ for React with TypeScript.
Or for React 18 / 17 and older use the xxx18 / xxx17 directory.
-
Run here command
npm install to install the node modules and wait until they are installed.
-
Next run
npm start to serve the example and wait until it is compiled (shows message in console: Compiled successfully).
-
Finally navigate your browser to http://localhost:3000/ to run the examples.
If you have any problems running React examples please contact our technical support.
Optionally install and run sample NodeJS server that serves data for the React examples from SQLLite database.
The server is needed for the most of the React examples to serve their data.
-
Go to directory /ExamplesNode/Server/.
-
Run here command
npm install to install the node modules and wait until they are installed.
-
Next run
npm start to start the NodeJS server. The server serves at http://localhost:8000/.
Using TreeGrid in React project
Installation
1) Copy TreeGrid distribution (/Grid)
Copy TreeGrid required files from TreeGrid examples package to the React project.
The TreeGrid examples package can be downloaded from
http://www.treegrid.com/Download#Trial
-
Copy the whole /Grid directory, including all subdirectories, from TreeGrid package to your React project.
The destination location can be chosen freely, it just must be accessible for the React server for reading.
In some types of React it must be copied into specific directory, e.g. in create-react-app template it must be copied into /public directory or some its subdirectory!
It can be also shared among more React projects if the location is accessible for all the project servers.
-
Optionally copy the whole /GridSrc directory, including all subdirectories, from TreeGrid package to your React project.
If you plan to modify TreeGrid css files and want to link the source css files to TreeGrid for simpler debugging.
-
Optionally if you purchased the debug sources, copy the whole TreeGrid debug sources package, including all subdirectories, to your React project.
If you want to include the TreeGrid plain sources instead of compiled GridE.js file for easy debugging.
Remember, the TreeGrid source files must not be accessible outside your company!.
2) Include TreeGrid script (GridE.js)
Link TreeGrid script file
GridE.js to the React main html file, e.g. into
public/index.html, into <head> section as
<script src="Grid/GridE.js"></script>, use correct path to GridE.js file.
Or if your React project displays TreeGrid only conditionally, you can link
GridED.js short script instead of GridE.js main script to download the GridE.js automatically on demand.
3) If you use TypeScript copy TreeGrid TypeScript API definition (TreeGrid.TypeScript.API.d.ts)
Copy file
TreeGrid.TypeScript.API.d.ts (located in the TreeGrid examples package in
/ExamplesNode/React/ directory) to the
/src directory.
This file contains TypeScript declaration of all TreeGrid API methods, properties and events.
If you use
isolatedModules (default in React),
import the
TreeGrid.TypeScript.API.d.ts to all files where the TreeGrid API is used.
Create TreeGrid grid / Gantt / sheet
4) Create TreeGrid statically by HTML tag (<treegrid> / <bdo>)
Create TreeGrid statically somewhere in your page by adding
<treegrid> tag to some html file or in some React component.
-
Include <treegrid> tag in some <div> element that will be used as TreeGrid container (called MainTag). It can be included directly in html code in page or in html code in React component.
For example add such code <div style="height:500px;"><treegrid is='treegrid' debug='check' layout_url="Layouts/StaticDef.js" data_url="Layouts/StaticData.js"></treegrid></div>.
The is attribute is used to avoid React warning about custom tag. All the attribute names and <treegrid> tag name must be lowercase to avoid processing them by React.
-
If used TypeScript, use <bdo> tag instead of <treegrid> tag, in this case the is attribute can be omitted.
You can add also //@ts-ignore line above the <bdo> definition to avoid TypeScript warnings.
-
If the React component is not rendered on page start, call window.StartTreeGrid() on the component mount to process the created <treegrid> / <bdo> tags.
For example componentDidMount() { window.StartTreeGrid(); }
For more information about <treegrid> / <bdo> tag see
Creating TreeGrid.
5) Create TreeGrid dynamically by script (TreeGrid(...))
Or create TreeGrid dynamically from JavaScript or TypeScript by
window.TreeGrid( ) function.
For example:
var grid = window.TreeGrid({Debug:'check', Layout: { Url:"Layouts/StaticDef.js" }, Data: { Url:"Layouts/StaticData.js" } },"MyTag");, where MyTag is an id of <div> tag where TreeGrid will be created (it will be TreeGrid MainTag).
For more information about TreeGrid( ) function see
Creating TreeGrid.
Existing grid can be deleted by
grid.Dispose( ) method. For more information see
Deleting TreeGrid.
To reload grid with new content use
grid.ReloadBody( ) method, to recreate grid completely from new layout and data use
grid.Reload( ) method. For more information see
Reloading TreeGrid.
To access existing grid on page by API link the TreeGrid with React component in point 6) or use global window.Grids object as
window.Grids[grid_id] or for the only grid on page use
window.Grids[0], for example
window.Grids[0].Reload();.
For more information see
Accessing TreeGrid by API.
6) Link created TreeGrid with React component (for API)
The simplest way is to assign a reference to React component in TreeGrid object and assign a reference to TreeGrid object in React component.
-
Define some property in the React component that will contain a reference to the created TreeGrid.
Name it e.g. Grid. If required, initialize it to null or undefined.
-
If TreeGrid is created statically by <treegrid> / <bdo> tag, do the assignment in React component constructor:
class App extends React.Component { ... Grid = null; constructor(){ super(); ... var Component = this; window.Grids.OnInit = function(grid){ grid.Component = Component; Component.Grid = grid; } ... } ...
In TypeScript do similarly:
class App extends React.Component { ... Grid : any; constructor(props : any){ super(props); var Component = this; window.Grids.OnInit = function (grid:any){ grid.Component = Component; Component.Grid = grid; }; ... } ...
The Grids.OnInit can be used only if there is only one TreeGrid on page. For more grids on page assign the event only for particular grid as: ... window.TGSetEvent("OnInit",grid_id,function(grid){ grid.Component = Component; Component.Grid = grid; });
In TreeGrid versions prior 14.1 use OnLoaded event instead of OnInit.
-
If TreeGrid is created dynamically by TreeGrid() function, you can do the assignment directly on TreeGrid() function call. In TreeGrid versions prior 14.1 use the previous way.
For example: class App extends React.Component { ... Grid = null; ... this.Grid = TreeGrid({Debug:'check', Layout: { Url:"Layouts/StaticDef.js" }, Data: { Url:"Layouts/StaticData.js" } },"MyTag",{Component:this});.
-
After that, the React component will be accessible in TreeGrid data event handlers (actions) and formulas as Grid.Component and TreeGrid will be accessible from React component code as this.Grid.
Create TreeGrid data (structure and content)
7) TreeGrid data formats (JSON / XML)
- Download data format
TreeGrid creates the grid from data in XML or JSON format. This format is proprietary for TreeGrid. The XML/JSON data contains all required definitions: configuration, columns, rows, cells, values, etc.
You need to create this XML/JSON data and pass it to TreeGrid. How this XML/JSON data is created depends fully on you. It can be created statically in some file or dynamically by some server or client script.
In this package there is NodeJS server with sample JavaScript code in Server/index.js that creates the JSON data for TreeGrid from SQLLite database or serves static file.
For more information about the TreeGrid XML/JSON data format for download see Layout format and Data format.
The documentation is for XML, the JSON format has the same tags, just different structure, see JSON format
- Upload data format
TreeGrid uploads the changes to server in XML or JSON format, similar to download format. See documentation Upload format
TreeGrid can upload only changes (added/deleted/moved/changed rows and their cells) or the whole data (all rows or even all data), it is controlled Upload_Type.
The server should parse this uploaded data and save it to its data source (database, file, etc.). This server side code is created you.
In this package there is NodeJS server with sample JavaScript code in Server/index.js that parses uploaded TreeGrid JSON data and saves the changes to SQLLite database or to static file.
8) Defining TreeGrid layout (columns and configuration)
TreeGrid layout defines the grid / Gantt structure: configuration, columns (types, formats, widths, ...), toolbars, control rows, Gantt definition, etc.
Simply it contains the whole TreeGrid definition except body rows and their cells. See
Layout format and
JSON format.
TreeGrid layout is usually static JSON or XML file, usually located in
/public or similar directory accessible directly for browser.
But TreeGrid layout can be also dynamically created on server or by API or merged to TreeGrid data.
The layout definition is assigned in Layout object in TreeGrid() method (e.g.
window.TreeGrid({Layout:{Url:'test.json'},...},...)) or with Layout_ prefix in <treegrid> tag (e.g.
<treegrid Layout_Url='test.json' ... >).
For more information about the TreeGrid layouts see especially the
Tutorial examples in this package that describe individual features and how defined and use them.
9) Creating TreeGrid data (rows and cells)
TreeGrid data defines the grid data rows and their cells with values. see
Data format and
JSON format.
TreeGrid data is usually created dynamically on server side from database or any other data source.
But TreeGrid data can be also static JSON / XML file or it can be generated dynamically by API.
The data definition is assigned in Data object in TreeGrid() method (e.g.
window.TreeGrid({Data:{Url:'testdata.json'},...},...)) or with Data_ prefix in <treegrid> tag (e.g.
<treegrid Data_Url='testdata.json' ... >).
10) TreeGrid data communication (AJAX / REST)
There are three usual ways of passing data to TreeGrid.
- Built-in AJAX communication
By default TreeGrid loads its data from server and uploads changes back to server by built-in AJAX.
You just specify the urls where to download and upload and their parameters and TreeGrid communicates directly with server.
For example <treegrid Layout_Url='Layouts/TableDef.js' Data_Url='http://localhost:8000/get?table=TableData&idcol=ID' Upload_Url='http://localhost:8000/set?table=TableData&idcol=ID' Upload_Format='JSON' Upload_Data='Data'>
or window.TreeGrid({ Layout:{Url:'Layouts/TableDef.js'}, Data:{Url:'http://localhost:8000/get?table=TableData&idcol=ID'}, Upload:{Url:'http://localhost:8000/set?table=TableData&idcol=ID', Format:'JSON', Data:'Data'},"MyTag");
For more information about the default AJAX communication see AJAX communication.
- Custom communication
For custom communication (AJAX or another type) define the TreeGrid data sources in the same way as in the previous point a.
And define API event OnCustomAjax. In this event you get all the required information for the communication. Start the custom communication and return true. And after the communication finished call the provided callback function with the result.
For example window.Grids.OnCustomAjax = function(G,IO,data,func){ MyAjax(IO.Url,data,function(result){ if(result<0) func(result,"Error"); else func(0,result); }); return true; }
For more information see OnCustomAjax.
- Direct data
It is possible to create TreeGrid from already loaded or created data.
Pass the data to Data parameter as string or JSON object or to Script parameter as global JavaScript variable name.
For example you can define MyObject = { MyData: {Cols:[{Name:'A'},{Name:'B'}]} };
and window.TreeGrid({Layout:{Script:"MyObject.MyData"},Data:{Data:{Body:[[{id:1,A:10,B:20},{id:2,A:20,B:40}]]}}},"MyTag");
or <treegrid Layout_Script:"MyObject.MyData" Data_Data="{Body:[[{id:1,A:10,B:20},{id:2,A:20,B:40}]]}">
For more information see Directly included data and Data from JavaScript.
TreeGrid events and API
11) Catch TreeGrid XML/JSON events (Actions in data)
The XML/JSON event handlers are defined in TreeGrid Defaults.xml (located in /Grid directory), in layout XML/JSON file or data or in another input XML/JSON data.
The XML/JSON events are mostly mouse and key events like OnClick or OnMouseMove. There are also a few special events like OnChange.
The handlers are defined by
On... attribute in
Actions tag for the whole grid or by
On... attribute assigned to particular column, default column, row, default row, row cell, default row cell.
The handlers are defined as JavaScript code in string. For example
Actions: { ... OnClick:"alert('Clicked cell '+Row.id+','+Col);return 1;" ... } or
Body:[[{... Col1OnClick:"alert('Clicked cell '+Row.id+','+Col);return 1;" ...},...]]
The React component (linked in point 6)) can be accessed in the handler code by
Grid.Component, for example
Actions: { ... OnClick:"Grid.Component.MyProcessClick(Row,Col)" ... }
The handler should return 1 to cancel event propagation or 0 to continue propagation and run next event handlers.
For more information see
TreeGrid mouse events and
TreeGrid key events.
12) Catch TreeGrid API events (in script)
TreeGrid API event handlers are JavaScript callbacks, they are defined as standard JavaScript / TypeScript functions that are called from TreeGrid code when the API event happens.
The API events can be used to run some code after TreeGrid is loaded, ready or rendered or some action is started or finished like data communication, editing, sorting, filtering, mouse actions like click, etc.
The event handlers can be assigned before any TreeGrid is created, e.g. in
constructor of React component that uses TreeGrid.
For example:
class App extends React.Component { ... constructor(...){ ... window.Grids.OnReady = function(G){ alert(G.id+" grid is ready to render"); } ... } ...
For more information about assigning API events see
TreeGrid API events.
13) Using TreeGrid API (API methods)
The most of TreeGrid API methods are methods of (TGrid) grid object.
The grid object can be got by
this.Grid if called in the React component linked with created TreeGrid as described in point 6).
Or grid object can be got by global
Grids object as
window.Grids[grid_id] or for the only grid on page use
window.Grids[0], for example
window.Grids[0].Reload();.
For more information see
Accessing TreeGrid by API.
Many TreeGrid methods require (TRow) row object. The row object can be got by its id by
GetRowById API method or by iterating rows, see
Row iteration.
The column are passed to TreeGrid methods by their names as strings. The columns can be also iterated see
Column iteration.