Angular UI Components Documentation
This tutorial shows jqxGrid in an Angular 22 application sorting on the server. When the user clicks a column header, the grid asks the API for the rows in the new order, and MySQL does the sorting. That matters when the table is too big to send to the browser in full.
The back end is Node 22 with Express 5 and mysql2/promise, reading the orders table of the Northwind sample database. It uses the same project layout, database user and .env file as the server-side paging tutorial; start there if you have not set them up.
When the data adapter's source has a sort callback, the grid does not sort in the browser. It calls the callback, which reloads the data, and the adapter adds two parameters to the request:
GET /api/orders?sortdatafield=ship_city&sortorder=asc
sortorder is asc, desc or empty when the user removes the sort.
Values in a query can be placeholders, but column names and ASC/DESC cannot. So the server never copies sortdatafield into the SQL. It looks the name up in a fixed list of sortable columns and uses the list's entry; anything else falls back to the default order.
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();
// Grid field name -> SQL column. Only these can be sorted.
const SORTABLE = new Map([
['order_date', 'o.order_date'],
['shipped_date', 'o.shipped_date'],
['ship_name', 'o.ship_name'],
['ship_address', 'o.ship_address'],
['ship_city', 'o.ship_city'],
['ship_country_region', 'o.ship_country_region']
]);
function orderBy(query) {
const column = SORTABLE.get(String(query.sortdatafield ?? ''));
if (!column) {
return 'ORDER BY o.id';
}
const direction = query.sortorder === 'desc' ? 'DESC' : 'ASC';
// Add the key so rows with equal values keep a stable order.
return `ORDER BY ${column} ${direction}, o.id`;
}
app.get('/api/orders', async (req, res) => {
const [rows] = await pool.query(
`SELECT o.id, o.order_date, o.shipped_date, o.ship_name, o.ship_address, o.ship_city, o.ship_country_region
FROM orders AS o
${orderBy(req.query)}
LIMIT ?`,
[1000]
);
res.json(rows);
});
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}`);
});
The only text that reaches the SQL comes from the SORTABLE map and the two literals ASC and DESC. A request such as sortdatafield=id;DROP TABLE orders finds no entry and gets the default order. The row limit is a placeholder like any other value.
Try it:
curl "http://localhost:3000/api/orders?sortdatafield=ship_city&sortorder=desc"
Set up the client as in the paging tutorial: ng new client, ng add jqwidgets-ng and a proxy.conf.json that forwards /api to port 3000.
The source's sort callback calls the grid's updatebounddata('sort') method, which sends a new request with the sort parameters. The grid is reached through a signal query.
client/src/app/app.ts:
import { Component, viewChild } from '@angular/core';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
readonly myGrid = viewChild.required<jqxGridComponent>('myGrid');
readonly source = {
datatype: 'json',
url: '/api/orders',
id: 'id',
cache: false,
datafields: [
{ name: 'id', type: 'number' },
{ name: 'order_date', type: 'date' },
{ name: 'shipped_date', type: 'date' },
{ name: 'ship_name', type: 'string' },
{ name: 'ship_address', type: 'string' },
{ name: 'ship_city', type: 'string' },
{ name: 'ship_country_region', type: 'string' }
],
// Called instead of sorting in the browser.
sort: () => {
this.myGrid().updatebounddata('sort');
}
};
readonly dataAdapter = new jqx.dataAdapter(this.source);
readonly columns = [
{ text: 'Order Date', datafield: 'order_date', cellsformat: 'd', width: 120 },
{ text: 'Shipped Date', datafield: 'shipped_date', cellsformat: 'd', width: 120 },
{ text: 'Ship Name', datafield: 'ship_name', width: 200 },
{ text: 'Address', datafield: 'ship_address', width: 200 },
{ text: 'City', datafield: 'ship_city', width: 120 },
{ text: 'Country', datafield: 'ship_country_region' }
];
}
client/src/app/app.html:
<jqxGrid #myGrid
[theme]="'material'"
[width]="900"
[height]="450"
[source]="dataAdapter"
[columns]="columns"
[sortable]="true">
</jqxGrid>
The grid field names match the JSON column names, so the sortdatafield the grid sends is a key of the server's SORTABLE map. If you rename fields with map, use the grid's names as the map keys.
cd northwind-grid/server
npm run dev
cd northwind-grid/client
ng serve --open
Click a column header to sort ascending, again for descending, and a third time to clear the sort. Each click sends one request, which you can see in the browser's network panel.
Sorting, paging and filtering all send their parameters in the same request. To support them together, build the WHERE, ORDER BY and LIMIT parts in one handler and return { TotalRows, Rows } as the paging tutorial does.
Related: grid sorting.