Angular UI Components Documentation
This tutorial shows jqxGrid in an Angular 22 application loading one page of rows at a time from a Node.js API. Because only the visible page crosses the network, the grid stays fast however large the table is.
The back end is Node 22 with Express 5 and the mysql2/promise driver, reading the Northwind sample database in MySQL. The API returns JSON, and every query uses placeholders, so request values never become part of the SQL text.
GET /api/customers?pagenum=2&pagesize=10. It adds the paging parameters itself.{ "TotalRows": 91, "Rows": [...] }. TotalRows lets the pager show the number of pages.northwind-grid/
server/ the Express API
client/ the Angular application
You need a MySQL 8 server (a local install, a Docker container or a hosted instance) with the MySQL version of the Northwind database loaded. This tutorial uses its customers table:
| Column | Content |
|---|---|
id | Primary key |
company | Company name |
first_name, last_name | Contact name |
job_title | Contact title |
city, country_region | Location |
If your table has other column names, change them in the SELECT below and in the grid's datafields.
Create a database user for the API that can only read, rather than using root:
mysql -u root -p -e "CREATE USER 'grid_reader'@'localhost' IDENTIFIED BY 'change-me'; GRANT SELECT ON northwind.* TO 'grid_reader'@'localhost';"
mkdir server
cd server
npm init -y
npm install express@5 mysql2
Edit server/package.json so Node treats the files as ES modules and has a start script:
{
"name": "northwind-api",
"private": true,
"type": "module",
"scripts": {
"start": "node --env-file=.env server.js",
"dev": "node --env-file=.env --watch server.js"
},
"dependencies": {
"express": "^5.1.0",
"mysql2": "^3.14.0"
}
}
Keep the connection settings out of the code, in server/.env. Node 22 reads this file with --env-file; add it to .gitignore.
DB_HOST=localhost
DB_USER=grid_reader
DB_PASSWORD=change-me
DB_NAME=northwind
PORT=3000
server/server.js:
import express from 'express';
import mysql from 'mysql2/promise';
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: 10
});
const app = express();
// Read a whole number from the query string and keep it within limits.
function toInt(value, fallback, min, max) {
const number = Number.parseInt(value, 10);
return Number.isNaN(number) ? fallback : Math.min(max, Math.max(min, number));
}
app.get('/api/customers', async (req, res) => {
const pagesize = toInt(req.query.pagesize, 10, 1, 100);
const pagenum = toInt(req.query.pagenum, 0, 0, 100000);
const [[{ total }]] = await pool.query('SELECT COUNT(*) AS total FROM customers');
const [rows] = await pool.query(
`SELECT company, first_name, last_name, job_title, city, country_region
FROM customers
ORDER BY id
LIMIT ? OFFSET ?`,
[pagesize, pagenum * pagesize]
);
res.json({ TotalRows: total, Rows: rows });
});
// Express 5 passes errors from async handlers here.
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: 'The data could not be loaded.' });
});
const port = Number(process.env.PORT ?? 3000);
app.listen(port, () => {
console.log(`API listening on http://localhost:${port}`);
});
Notes on the code:
? placeholders are filled by mysql2, which escapes each value. pagenum and pagesize are also parsed as numbers and clamped, so a request cannot ask for a million rows.async handlers and passes them to the error middleware; there is no need for try/catch in every route. The client gets a generic message, and the details stay in the server log./api calls to this server, and in production both are served from one origin.Start it and check the response:
npm run dev
curl "http://localhost:3000/api/customers?pagenum=0&pagesize=5"
From northwind-grid/, create the client and add jQWidgets:
ng new client
cd client
ng add jqwidgets-ng
Create client/proxy.conf.json, so the dev server forwards /api to the Express server:
{
"/api": {
"target": "http://localhost:3000",
"secure": false
}
}
In angular.json, set "proxyConfig": "proxy.conf.json" in the options of the serve target. The Angular development server explains the proxy.
client/src/app/app.ts:
import { Component } from '@angular/core';
import { jqxGridModule } from 'jqwidgets-ng/jqxgrid';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
readonly source: any = {
datatype: 'json',
url: '/api/customers',
root: 'Rows',
cache: false,
datafields: [
{ name: 'CompanyName', map: 'company', type: 'string' },
{ name: 'FirstName', map: 'first_name', type: 'string' },
{ name: 'LastName', map: 'last_name', type: 'string' },
{ name: 'Title', map: 'job_title', type: 'string' },
{ name: 'City', map: 'city', type: 'string' },
{ name: 'Country', map: 'country_region', type: 'string' }
],
// Tell the grid how many rows exist in total, so the pager can count pages.
beforeprocessing: (data: { TotalRows: number }) => {
this.source.totalrecords = data.TotalRows;
}
};
readonly dataAdapter = new jqx.dataAdapter(this.source);
readonly columns = [
{ text: 'Company Name', datafield: 'CompanyName', width: 240 },
{ text: 'First Name', datafield: 'FirstName', width: 120 },
{ text: 'Last Name', datafield: 'LastName', width: 120 },
{ text: 'Title', datafield: 'Title', width: 180 },
{ text: 'City', datafield: 'City', width: 120 },
{ text: 'Country', datafield: 'Country' }
];
// In virtual mode the grid asks for the rows of the current page.
readonly rendergridrows = (params: { data: unknown[] }): unknown[] => params.data;
}
client/src/app/app.html:
<jqxGrid
[theme]="'material'"
[width]="900"
[autoheight]="true"
[source]="dataAdapter"
[columns]="columns"
[pageable]="true"
[pagesizeoptions]="['10', '20', '50']"
[virtualmode]="true"
[rendergridrows]="rendergridrows">
</jqxGrid>
map connects each grid field to a column name in the JSON, so the grid can use its own field names.
Start the API in one terminal and the Angular app in another:
cd northwind-grid/server
npm run dev
cd northwind-grid/client
ng serve --open
Open http://localhost:4200/ and move between pages. The browser's network panel shows one small request per page.
Build the client with ng build and let Express serve the result, so the page and the API share one origin. Add an import at the top of server.js:
import path from 'node:path';
Then add these lines after the API routes and before the error middleware:
const browserDir = path.resolve(import.meta.dirname, '../client/dist/client/browser');
app.use(express.static(browserDir));
// Any other GET request gets the Angular app, so client-side routes work on reload.
app.get('/{*path}', (req, res) => {
res.sendFile(path.join(browserDir, 'index.html'));
});
Express 5 names wildcards, so the catch-all route is written /{*path} rather than *.
mysql2/promise reads MySQL with parameterised queries.jqx.dataAdapter, and beforeprocessing passes the total row count to the pager./api, so the API needs no CORS settings.Related: server-side sorting, server-side filtering and grid paging.