How to run TreeGrid Angular examples
Angular examples per Angular version
TreeGrid examples package contains simple examples for Angular framework located in
/ExamplesNode/Angular/.
There are always two example sets per Angular version, one creates TreeGrid statically by
<treegrid> and the second one with
-dynamic suffix creates TreeGrid dynamically by
TreeGrid( ) function.
Install and run the Angular examples
-
Go to directory in the unzipped package according to chosen Angular version, e.g. to /ExamplesNode/Angular/V20/
-
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:4200/ to run the examples.
If you have any problems running Angular examples please contact our technical support.
Optionally install and run sample NodeJS server that serves data for the Angular example from SQLLite database.
The server is needed for the most of the Angular 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 Angular project
Installation
1) Copy TreeGrid distribution (/Grid)
Copy TreeGrid required files from TreeGrid examples package to the Angular 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 Angular project.
The destination location can be chosen freely, it just must be accessible for the Angular server for reading.
In Angular 5 and older it must be copied into /src directory or some its subdirectory!
It can be also shared among more Angular 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 Angular 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 Angular 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) Add paths to static files (assets)
Modify
angular.json (or .angular-cli.json in 5.x and older), add to
assets array all the files and directories that will be served directly without processing them by Angular.
Note that there can be more assets sections in angular.js, e.g. for "build" and "test", modify all of them.
In Angular 5 and older the served static files must be inside /src directory or some its subdirectory!
-
Add here path to /Grid directory where are the required TreeGrid files (copied in point 1)), for example add { "glob": "**/*", "input": "../../Grid/", "output": "/Grid/" } to serve directory "../../Grid/" as "Grid/".
The input is relative to root directory with angular.json file, the output is relative to the served root with index.html.
-
Optionally add here path to GridSrc directory if you plan to modify TreeGrid css files and want to link the source css files to TreeGrid for simpler debugging, for example add { "glob": "**/*", "input": "../../GridSrc/", "output": "/GridSrc/" }.
-
Optionally add here path to TreeGrid debug source files directory if you purchased the sources and want to include the TreeGrid plain sources instead of compiled GridE.js file for easy debugging.
-
Add here path to TreeGrid static layout and data files if you use them and they are not located in the assets directory, for example add { "glob": "**/*", "input": "Layouts/", "output": "/Layouts/" } to serve root directory Layouts as root directory Layouts.
3) Include TreeGrid script (GridE.js)
Link TreeGrid script file
GridE.js to the Angular main file
src/index.html into <head> section as
<script src="Grid/GridE.js"></script>,
the path to the GridE.js file is defined in previous point 2).
Or if your Angular 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.
4) Copy TreeGrid API definition (TreeGrid.TypeScript.API.d.ts)
Copy file
TreeGrid.TypeScript.API.d.ts (located in the TreeGrid examples package in
/ExamplesNode/ directory) to the
/src directory.
This file contains TypeScript declaration of all TreeGrid API methods, properties and events.
If you use
isolatedModules, import the TreeGrid.TypeScript.API.d.ts to all files where the TreeGrid API is used.
5) Copy and link TreeGrid component definition (TreeGrid.component.ts)
-
Copy file TreeGrid.component.ts (located in the TreeGrid examples package in /ExamplesNode/ directory) to the /src directory.
-
Link this file to all modules where <treegrid> tag is used.
Import it as import { TreeGridComponent } from '../TreeGrid.component'; (optionally change the path to the file).
And add it to the @NgModule declarations array like @NgModule({ ... declarations: [ TreeGridComponent, ...
It is required only if TreeGrid is created statically by <treegrid> tag and not dynamically by TreeGrid() function.
It is possible to modify this file e.g. to assign specific TreeGrid API events.
Create TreeGrid grid / Gantt / sheet
6) Create TreeGrid statically by HTML tag (<treegrid>)
Create TreeGrid statically somewhere in your page by adding
<treegrid> tag to some html file.
Include <treegrid> tag in some <div> element that will be used as TreeGrid container (called MainTag).
For example add such code
<div style="height:500px;"><treegrid Debug='check' Layout_Url="Layouts/StaticDef.js" Data_Url="Layouts/StaticData.js"></treegrid></div>.
For more information about <treegrid> tag see
Creating TreeGrid.
7) Create TreeGrid dynamically by script (TreeGrid(...))
Or create TreeGrid dynamically from TypeScript by
TreeGrid( ) function. For this way the point 5) is not required.
For example:
var grid = 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 Angular component in point 8) or use global Grids object as
Grids[grid_id] or for the only grid on page use
Grids[0], for example
Grids[0].Reload();.
For more information see
Accessing TreeGrid by API.
8) Link created TreeGrid with Angular component (for API)
The simplest way is to assign a reference to Angular component in TreeGrid object and assign a reference to TreeGrid object in Angular component.
-
Define some property in the Angular 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> tag, do the assignment in Angular component constructor:
export class AppComponent { ... public Grid: any; constructor(...){ ... var Component = this; Grids.OnInit = function(grid){ 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: ... 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: export class AppComponent { ... public Grid: any; ... this.Grid = TreeGrid({Debug:'check', Layout: { Url:"Layouts/StaticDef.js" }, Data: { Url:"Layouts/StaticData.js" } },"MyTag",{Component:this}); ... .
-
After that, the Angular component will be accessible in TreeGrid data event handlers (actions) and formulas as Grid.Component and TreeGrid will be accessible from Angular component code as this.Grid.
Create TreeGrid data (structure and content)
9) 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.
10) 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 located in assets (see point 2)).
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.
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.
11) 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.
TreeGrid({Data:{Url:'testdata.json'},...},...)) or with Data_ prefix in <treegrid> tag (e.g.
<treegrid Data_Url='testdata.json' ... >).
12) 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 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 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 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
13) 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 Angular component (linked in point 8)) 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.
14) 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 Angular component that uses TreeGrid.
For example:
export class AppComponent { ... constructor(...){ ... Grids.OnReady = function(G){ alert(G.id+" grid is ready to render"); } ... } ...
For more information about assigning API events see
TreeGrid API events.
15) 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 Angular component linked with created TreeGrid as described in point 8).
Or grid object can be got by global
Grids object as
Grids[grid_id] or for the only grid on page use
Grids[0], for example
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.