Angular UI Components Documentation
This tutorial shows jqxGrid in an Angular 22 application filtering on the server. The user types in the grid's filter row, the grid sends the filter conditions to the API, and MySQL returns only the matching rows.
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. The front end also uses Angular's HttpClient to load the list of countries for a drop-down filter.
When the data adapter's source has a filter callback, the grid sends each filter as a numbered group of parameters:
| Parameter | Example | Meaning |
|---|---|---|
filterscount | 2 | Number of filters |
filterdatafield0 | ship_city | The field to filter |
filtercondition0 | CONTAINS | The comparison |
filtervalue0 | on | The value the user typed |
filteroperator0 | 0 | How it joins the next filter on the same field: 0 is AND, 1 is OR |
All of these parameters come from the browser, so none of them may be copied into SQL. The server uses two fixed lists:
? placeholder. Unknown conditions are ignored.The values go only into the placeholder list. For LIKE conditions, the server also escapes % and _ in the value, so a user who types 50% searches for that text rather than a pattern.
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 filtered.
const FILTERABLE = new Map([
['ship_name', 'ship_name'],
['ship_address', 'ship_address'],
['ship_city', 'ship_city'],
['ship_country_region', 'ship_country_region']
]);
// Escape LIKE wildcards so they match literally.
const like = (value) => value.replace(/[\\%_]/g, '\\$&');
// Condition name -> SQL fragment and its parameters.
const CONDITIONS = {
CONTAINS: (v) => ['LIKE ?', [`%${like(v)}%`]],
DOES_NOT_CONTAIN: (v) => ['NOT LIKE ?', [`%${like(v)}%`]],
STARTS_WITH: (v) => ['LIKE ?', [`${like(v)}%`]],
ENDS_WITH: (v) => ['LIKE ?', [`%${like(v)}`]],
EQUAL: (v) => ['= ?', [v]],
NOT_EQUAL: (v) => ['<> ?', [v]],
GREATER_THAN: (v) => ['> ?', [v]],
LESS_THAN: (v) => ['< ?', [v]],
GREATER_THAN_OR_EQUAL: (v) => ['>= ?', [v]],
LESS_THAN_OR_EQUAL: (v) => ['<= ?', [v]],
NULL: () => ['IS NULL', []],
NOT_NULL: () => ['IS NOT NULL', []]
};
function buildWhere(query) {
const count = Math.min(Number.parseInt(query.filterscount ?? '0', 10) || 0, 20);
const groups = new Map();
for (let i = 0; i < count; i++) {
const column = FILTERABLE.get(String(query[`filterdatafield${i}`] ?? ''));
const condition = CONDITIONS[String(query[`filtercondition${i}`] ?? '')];
if (!column || !condition) {
continue;
}
const [sql, params] = condition(String(query[`filtervalue${i}`] ?? ''));
const group = groups.get(column) ?? { sql: '', params: [], join: ' AND ' };
group.sql += (group.sql ? group.join : '') + `${column} ${sql}`;
group.params.push(...params);
group.join = query[`filteroperator${i}`] === '1' ? ' OR ' : ' AND ';
groups.set(column, group);
}
if (groups.size === 0) {
return { where: '', params: [] };
}
const parts = [...groups.values()];
return {
where: 'WHERE ' + parts.map((group) => `(${group.sql})`).join(' AND '),
params: parts.flatMap((group) => group.params)
};
}
app.get('/api/orders', async (req, res) => {
const { where, params } = buildWhere(req.query);
const [rows] = await pool.query(
`SELECT id, ship_name, ship_address, ship_city, ship_country_region
FROM orders
${where}
ORDER BY id
LIMIT ?`,
[...params, 1000]
);
res.json(rows);
});
// Distinct countries, for the grid's drop-down filter.
app.get('/api/countries', async (req, res) => {
const [rows] = await pool.query(
'SELECT DISTINCT ship_country_region AS country FROM orders WHERE ship_country_region IS NOT NULL ORDER BY country'
);
res.json(rows.map((row) => row.country));
});
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}`);
});
Filters on the same field are joined with the operator the user chose; filters on different fields are joined with AND. The number of filters is capped, so one request cannot build a huge query.
Try it:
curl "http://localhost:3000/api/orders?filterscount=1&filterdatafield0=ship_city&filtercondition0=STARTS_WITH&filtervalue0=Bo&filteroperator0=0"
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 grid's data adapter makes its own requests, but the country list is loaded with HttpClient. Register it in client/src/app/app.config.ts:
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideRouter(routes),
provideHttpClient(withFetch())
]
};
The source's filter callback calls updatebounddata('filter'), which sends a new request with the filter parameters. The country list arrives as an Observable; toSignal turns it into a signal, and the columns are computed from it, so the Country column's drop-down fills in when the list arrives.
client/src/app/app.ts:
import { Component, computed, inject, viewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { toSignal } from '@angular/core/rxjs-interop';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
private readonly http = inject(HttpClient);
readonly myGrid = viewChild.required<jqxGridComponent>('myGrid');
readonly countries = toSignal(this.http.get<string[]>('/api/countries'), { initialValue: [] });
readonly source = {
datatype: 'json',
url: '/api/orders',
id: 'id',
cache: false,
datafields: [
{ name: 'id', type: 'number' },
{ name: 'ship_name', type: 'string' },
{ name: 'ship_address', type: 'string' },
{ name: 'ship_city', type: 'string' },
{ name: 'ship_country_region', type: 'string' }
],
// Called instead of filtering in the browser.
filter: () => {
this.myGrid().updatebounddata('filter');
}
};
readonly dataAdapter = new jqx.dataAdapter(this.source);
readonly columns = computed(() => [
{ text: 'Ship Name', datafield: 'ship_name', width: 220 },
{ text: 'Address', datafield: 'ship_address', width: 220 },
{ text: 'City', datafield: 'ship_city', width: 150 },
{
text: 'Country',
datafield: 'ship_country_region',
filtertype: 'list',
filteritems: this.countries()
}
]);
}
client/src/app/app.html:
<jqxGrid #myGrid
[theme]="'material'"
[width]="850"
[height]="450"
[source]="dataAdapter"
[columns]="columns()"
[filterable]="true"
[showfilterrow]="true">
</jqxGrid>
The text columns get a text box in the filter row, which sends CONTAINS conditions; the Country column gets a list, which sends EQUAL.
cd northwind-grid/server
npm run dev
cd northwind-grid/client
ng serve --open
Type part of a city name in the filter row, then pick a country. Each change sends one request with all active filters, and the grid shows what the server returns.
To filter, sort and page in one grid, build all three parts in one handler. Run the COUNT(*) with the same WHERE clause and parameters as the page query, so TotalRows counts only the matching rows.
Related: grid filtering.