Angular UI Components Documentation
JqxGrid can be bound to multiple types of data collections including arrays, xml, json, tsv, csv or remote data. To data bind the Grid to a data source you need to set its source property to point to an instance of jqxDataAdapter.
The source object represents a set of key/value pairs.
pager: function (pagenum, pagesize, oldpagenum) {
}
sort: function (column, direction) {
}
filter: function (filters, recordsArray) {
}
addrow: function (rowid, rowdata, position, commit) {
// synchronize with the server - send insert command
// call commit with parameter true if the synchronization with the server is successful
//and with parameter false if the synchronization failed.
commit(true);
}
deleterow: function (rowid, commit) {
// synchronize with the server - send delete command
// call commit with parameter true if the synchronization with the server is successful
//and with parameter false if the synchronization failed.
commit(true);
}
updaterow: function (rowid, newdata, commit) {
// synchronize with the server - send update command
// call commit with parameter true if the synchronization with the server is successful
// and with parameter false if the synchronization failed.
commit(true);
}
source: any = {
datatype: "jsonp",
datafields: [
{ name: 'countryName' },
{ name: 'name' },
{ name: 'population', type: 'float' },
{ name: 'continentCode' }
],
url: "http://ws.geonames.org/searchJSON",
processdata: function (data) {
data.featureClass = "P";
data.style = "full";
data.maxRows = 50;
}
};source: any = {
datatype: "jsonp",
datafields: [
{ name: 'countryName' },
{ name: 'name' },
{ name: 'population', type: 'float' },
{ name: 'continentCode' }
],
url: "http://ws.geonames.org/searchJSON",
data: {
featureClass: "P",
style: "full",
maxRows: 50
},
formatdata: function (data) {
return "my data";
}
};The example code below illustrates how to create jqxDataAdapter from a source object:
dataAdapter: any = new jqx.dataAdapter(this.source, {
loadComplete: function (data) {
// data is loaded.
},
loadError: function (xhr, status, error) {
// data is not loaded.
}
});If you bind the Grid to remote data source using asynchronous requests( that is by default when you specify url in the source object and you didn't set the async field to false), then make sure that you call any method or set a property once the data is loaded. To ensure that you call your code when the Grid is loaded with data, use the Grid's ready callback function or bind to the 'bindingcomplete' event before the Grid's initialization and call your code inside the event handler.
The data member is array. The datatype member is set to "array".
<jqxGrid [theme]="'material'" [width]="'100%'" [source]="dataAdapter"
[columns]="columns">
</jqxGrid>import { Component } from '@angular/core';
import { generatedata } from '../sampledata/generatedata';
@Component({ selector: 'app-root', templateUrl: './app.html' })
export class App {
source: any = {
localdata: generatedata(50),
datatype: 'array',
datafields: [
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
]
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'Name', datafield: 'firstname', width: 120 },
{ text: 'Last Name', datafield: 'lastname', width: 120 },
{ text: 'Product', datafield: 'productname', width: 180 },
{ text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
{
text: 'Unit Price',
datafield: 'price',
width: 90,
cellsalign: 'right',
cellsformat: 'c2'
},
{ text: 'Total', datafield: 'total', cellsalign: 'right', cellsformat: 'c2' }
];
}In the source initialization, you need to set the following:
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.html' })
export class App {
source: any = {
datatype: 'xml',
datafields: [
{ name: 'CompanyName', map: 'm\\:properties>d\\:CompanyName', type: 'string' },
{ name: 'ContactName', map: 'm\\:properties>d\\:ContactName', type: 'string' },
{ name: 'ContactTitle', map: 'm\\:properties>d\\:ContactTitle', type: 'string' },
{ name: 'City', map: 'm\\:properties>d\\:City', type: 'string' },
{ name: 'PostalCode', map: 'm\\:properties>d\\:PostalCode', type: 'string' },
{ name: 'Country', map: 'm\\:properties>d\\:Country', type: 'string' }
],
root: 'entry',
record: 'content',
id: 'm\\:properties>d\\:CustomerID',
url: '../sampledata/customers.xml'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'Company Name', datafield: 'CompanyName', width: 250 },
{ text: 'Contact Name', datafield: 'ContactName', width: 150 },
{ text: 'Contact Title', datafield: 'ContactTitle', width: 180 },
{ text: 'City', datafield: 'City', width: 120 },
{ text: 'Postal Code', datafield: 'PostalCode', width: 90 },
{ text: 'Country', datafield: 'Country', width: 100 }
];
}When you bind the Grid to JSON data, you need to set the source object's datatype to 'json'.
If you want to bind the Grid to JSONP data, then you need to set the source object's datatype to 'jsonp'.
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.html' })
export class App {
source: any = {
datatype: 'json',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'type', type: 'string' },
{ name: 'calories', type: 'int' },
{ name: 'totalfat', type: 'string' },
{ name: 'protein', type: 'string' }
],
id: 'id',
url: '../sampledata/beverages.txt'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'Name', datafield: 'name', width: 250 },
{ text: 'Beverage Type', datafield: 'type', width: 250 },
{ text: 'Calories', datafield: 'calories', width: 180 },
{ text: 'Total Fat', datafield: 'totalfat', width: 120 },
{ text: 'Protein', datafield: 'protein', minwidth: 120 }
];
}When you bind the Grid to TSV data, you need to set the source object's datatype to 'tab'.
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.html' })
export class App {
source: any = {
datatype: 'tab',
datafields: [
{ name: 'Year', type: 'int' },
{ name: 'HPI', type: 'float' },
{ name: 'BuildCost', type: 'float' },
{ name: 'Population', type: 'float' },
{ name: 'Rate', type: 'float' }
],
url: '../sampledata/homeprices.txt'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'Year', datafield: 'Year' },
{ text: 'HPI', datafield: 'HPI', cellsformat: 'f2' },
{ text: 'Build Cost', datafield: 'BuildCost', cellsformat: 'f2' },
{ text: 'Population', datafield: 'Population', cellsformat: 'f2' },
{ text: 'Rate', datafield: 'Rate', cellsformat: 'f5', minwidth: 100 }
];
}When you bind the Grid to CSV data, you need to set the source object's datatype to 'csv'.
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.html' })
export class App {
source: any = {
datatype: 'csv',
datafields: [
{ name: 'Date', type: 'date' },
{ name: 'S&P 500', type: 'float' },
{ name: 'NASDAQ', type: 'float' }
],
url: '../sampledata/nasdaq_vs_sp500.txt'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'Date', datafield: 'Date', cellsformat: 'D', width: 250 },
{ text: 'S&P 500', datafield: 'S&P 500', width: 300, cellsformat: 'f' },
{ text: 'NASDAQ', datafield: 'NASDAQ', cellsformat: 'f' }
];
}To refresh the Grid, you need to simply set its 'source' property again.
<jqxGrid #myGrid [theme]="'material'" [width]="'100%'" [source]="dataAdapter"
[columns]="columns">
</jqxGrid>
<div style="height: 18%; margin-top: 5px">
<jqxButton [theme]="'material'" (onClick)="refreshBtnOnClick()">Refresh</jqxButton>
</div>import { Component, viewChild } from '@angular/core';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
import { generatedata } from '../sampledata/generatedata';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
readonly myGrid = viewChild.required<jqxGridComponent>('myGrid');
source: any = {
localdata: generatedata(50),
datafields: [
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
],
datatype: 'array'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ text: 'First Name', dataField: 'firstname', width: 130 },
{ text: 'Last Name', dataField: 'lastname', width: 130 },
{ text: 'Product', dataField: 'productname', width: 180 },
{ text: 'Quantity', dataField: 'quantity', width: 80, cellsalign: 'right' },
{
text: 'Unit Price',
dataField: 'price',
width: 90,
cellsalign: 'right',
cellsformat: 'c2'
},
{ text: 'Total', dataField: 'total', cellsalign: 'right', minwidth: 100, cellsformat: 'c2' }
];
refreshBtnOnClick(): void {
this.source.localdata = generatedata(50);
// passing `cells` to the `updatebounddata` method will refresh only the cells values when the new rows count is equal to the previous rows count.
this.myGrid().updatebounddata('cells');
}
}