Vue UI Components Documentation
This tutorial builds a Vue Grid that loads one page of rows at a time from a Node.js API. Use server-side paging when a table is too large to send to the browser at once: the grid asks for the current page, and the server returns only those rows plus the total count.
The stack is Node 22 with Express 5 and mysql2/promise on the server, and Vue 3.5 with <script setup> and TypeScript, built with Vite, in the browser.
Create a Vue app with create-vue (answer Yes to TypeScript), add jQWidgets, and add the two server packages. The API lives in a server folder inside the same project.
npm create vue@latest my-app
cd my-app
npm install
npm install jqwidgets-scripts
npm install express mysql2
create-vue sets "type": "module" in package.json, so the server files can use import as well. The finished project looks like this:
my-app/
├── server/
│ ├── db.js # MySQL connection pool
│ ├── seed.js # creates and fills the customers table
│ └── server.js # Express API
├── src/
│ ├── App.vue # the grid
│ ├── jqwidgets-vue3.d.ts
│ └── main.ts
├── index.html
├── package.json
└── vite.config.ts
You need a MySQL 8 (or MariaDB) server on your machine. XAMPP, a Docker container or a local install all work. Create an empty database:
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS northwind CHARACTER SET utf8mb4"
Keep the connection settings in one module, server/db.js. It reads them from environment variables and falls back to a local root user, so no password is written into the code:
// server/db.js
import mysql from 'mysql2/promise';
// A pool reuses connections between requests.
export const pool = mysql.createPool({
host: process.env.DB_HOST ?? 'localhost',
user: process.env.DB_USER ?? 'root',
password: process.env.DB_PASSWORD ?? '',
database: process.env.DB_NAME ?? 'northwind',
connectionLimit: 10
});
With Node 22 you can keep these variables in a .env file and start scripts with node --env-file=.env. Add .env to .gitignore.
Create server/seed.js. It creates a customers table and fills it with 250 rows, enough to page through:
// server/seed.js
import { pool } from './db.js';
const firstNames = ['Maria', 'Ana', 'Antonio', 'Thomas', 'Christina', 'Hanna', 'Frédérique', 'Martín', 'Laurence', 'Elizabeth'];
const lastNames = ['Anders', 'Trujillo', 'Moreno', 'Hardy', 'Berglund', 'Moos', 'Citeaux', 'Sommer', 'Lebihan', 'Lincoln'];
const titles = ['Sales Representative', 'Owner', 'Order Administrator', 'Marketing Manager', 'Accounting Manager'];
const places = [
['Berlin', 'Germany'], ['México D.F.', 'Mexico'], ['London', 'UK'], ['Luleå', 'Sweden'], ['Mannheim', 'Germany'],
['Strasbourg', 'France'], ['Madrid', 'Spain'], ['Marseille', 'France'], ['Tsawassen', 'Canada'], ['Buenos Aires', 'Argentina']
];
await pool.query(`CREATE TABLE IF NOT EXISTS customers (
id INT AUTO_INCREMENT PRIMARY KEY,
company VARCHAR(100) NOT NULL,
contact_name VARCHAR(100) NOT NULL,
contact_title VARCHAR(50) NOT NULL,
address VARCHAR(100) NOT NULL,
city VARCHAR(50) NOT NULL,
country VARCHAR(50) NOT NULL
)`);
await pool.query('TRUNCATE TABLE customers');
const rows = [];
for (let i = 0; i < 250; i++) {
const [city, country] = places[i % places.length];
rows.push([
`${lastNames[(i * 3) % lastNames.length]} Trading ${i + 1}`,
`${firstNames[i % firstNames.length]} ${lastNames[(i * 7) % lastNames.length]}`,
titles[i % titles.length],
`${100 + i} Market Street`,
city,
country
]);
}
// "VALUES ?" with an array of arrays inserts all rows in one statement.
await pool.query('INSERT INTO customers (company, contact_name, contact_title, address, city, country) VALUES ?', [rows]);
console.log(`Inserted ${rows.length} customers`);
await pool.end();
node server/seed.js
In virtual mode the grid asks for one page at a time. Each request carries pagenum (zero based) and pagesize, and the grid expects the total row count back so it can draw the pager. Create server/server.js:
// server/server.js
import express from 'express';
import { pool } from './db.js';
const app = express();
const PORT = 3000;
function toInt(value, fallback) {
const number = Number.parseInt(value, 10);
return Number.isNaN(number) ? fallback : number;
}
app.get('/api/customers', async (req, res) => {
// Clamp the values: they come from the query string and cannot be trusted.
const pagenum = Math.max(0, toInt(req.query.pagenum, 0));
const pagesize = Math.min(100, Math.max(1, toInt(req.query.pagesize, 10)));
const [[{ total }]] = await pool.query('SELECT COUNT(*) AS total FROM customers');
const [rows] = await pool.query(
`SELECT company, contact_name, contact_title, address, city, country
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: 'Could not read the data' });
});
app.listen(PORT, () => {
console.log(`API listening on http://localhost:${PORT}`);
});
The ? placeholders are filled by mysql2, which escapes the values. Never paste request values into the SQL string yourself.
Start the server and check the response in the browser at http://localhost:3000/api/customers?pagenum=1&pagesize=10:
node --watch server/server.js
The Vite dev server runs on port 5173 and the API on port 3000. A proxy in vite.config.ts forwards /api requests, so the browser sees one origin and you do not need CORS headers:
import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
proxy: {
// During development, requests to /api/... go to the Express server.
'/api': 'http://localhost:3000'
}
}
});
Load the jQWidgets styles in src/main.ts:
import { createApp } from 'vue';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.base.css';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.material-purple.css';
import App from './App.vue';
createApp(App).mount('#app');
The Vue components are plain JavaScript files, so TypeScript's strict mode rejects importing them (TS7016: Could not find a declaration file). Declare them once in a new file, src/jqwidgets-vue3.d.ts:
// src/jqwidgets-vue3.d.ts
declare module 'jqwidgets-scripts/jqwidgets-vue3/*.vue' {
import type { DefineComponent } from 'vue';
const component: DefineComponent<Record<string, any>, Record<string, any>, any>;
export default component;
}
Replace src/App.vue. Three settings turn on server-side paging:
virtualmode tells the grid that the data source holds only the current page.rendergridrows returns the rows to draw; in virtual mode that is simply the data the server sent.beforeprocessing copies TotalRows from the response into source.totalrecords, so the pager knows how many pages exist.<script setup lang="ts">
import JqxGrid from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxgrid.vue';
// Defined globally by the jQWidgets core, which the Grid component imports.
declare const jqx: any;
interface CustomersResponse {
TotalRows: number;
}
const source = {
datatype: 'json',
url: '/api/customers',
root: 'Rows',
cache: false,
totalrecords: 0,
datafields: [
{ name: 'CompanyName', map: 'company', type: 'string' },
{ name: 'ContactName', map: 'contact_name', type: 'string' },
{ name: 'ContactTitle', map: 'contact_title', type: 'string' },
{ name: 'Address', map: 'address', type: 'string' },
{ name: 'City', map: 'city', type: 'string' },
{ name: 'Country', map: 'country', type: 'string' }
],
beforeprocessing: (data: CustomersResponse) => {
source.totalrecords = data.TotalRows;
}
};
// Plain constants, not refs: Vue does not need to track them.
const dataAdapter = new jqx.dataAdapter(source);
const columns = [
{ text: 'Company Name', datafield: 'CompanyName', width: 220 },
{ text: 'Contact Name', datafield: 'ContactName', width: 180 },
{ text: 'Contact Title', datafield: 'ContactTitle', width: 170 },
{ text: 'Address', datafield: 'Address', width: 170 },
{ text: 'City', datafield: 'City', width: 120 },
{ text: 'Country', datafield: 'Country' }
];
const rendergridrows = (params: { data: unknown[] }) => params.data;
</script>
<template>
<JqxGrid
theme="material-purple"
:width="900"
:autoheight="true"
:source="dataAdapter"
:columns="columns"
:pageable="true"
:pagesize="10"
:virtualmode="true"
:rendergridrows="rendergridrows"
/>
</template>
The map setting of each data field reads a column from the JSON rows, so the grid keeps its own field names while the database uses snake_case.
Keep the API running in one terminal and start Vite in a second one:
npm run dev
Open http://localhost:5173. Each click in the pager, or a change of the page size, sends a request such as /api/customers?pagenum=3&pagesize=10; you can watch them in the browser's Network tab.
Run npm run build and serve the dist folder from the same Express app, for example with app.use(express.static('dist')). Then /api and the page share one origin, and neither the proxy nor CORS headers are needed.
Related: server-side sorting, server-side filtering, Grid paging. See the Grid demos at jqwidgets.com/vue.