Vue UI Components Documentation
This tutorial adds the jQWidgets Vue Grid to a Vue 3.5 and TypeScript project created with create-vue. The grid lists contacts, sorts, filters and pages them, and shows which row is selected.
Earlier versions of this page used Vue CLI, which is in maintenance mode; create-vue and Vite replace it.
Answer Yes to TypeScript when create-vue asks, then install jQWidgets:
npm create vue@latest my-app
cd my-app
npm install
npm install jqwidgets-scripts
Two additions to vite.config.ts make jqwidgets-scripts versions before 27.1.0 work with Vite, and do no harm in 27.1.0. Without them, npm run dev stops with Cannot set properties of undefined (setting 'Globalize') as soon as a Grid, Calendar, DateTimeInput, Form or Scheduler is on the page, and npm run build fails with Unexpected token Semicolon. With 27.1.0 the dev server works without the plugin, and the build fails only with the light-ie and dark-ie themes. Keep the rest of the file create-vue wrote; the additions are the plugin and the css setting:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, type Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// Before jqwidgets-scripts 27.1.0: globalize.js, which the Grid, Calendar, DateTimeInput, Form and
// Scheduler components load, hands its code `this` as the window. The dev server serves it as
// an ES module, where `this` is undefined; this passes the window instead.
const jqwidgetsGlobalize: Plugin = {
name: 'jqwidgets-globalize-window',
transform(code, id) {
if (id.includes('jqwidgets-scripts/jqwidgets/globalization/globalize.js')) {
return code.replace(/\}\(this\)\);/, '}(window));')
}
},
}
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
jqwidgetsGlobalize,
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
// Some jQWidgets themes still contain Internet Explorer CSS hacks (*zoom: 1; in 27.1.0 only
// light-ie and dark-ie), which Vite's
// CSS minifier rejects: errorRecovery drops those lines instead of stopping the build.
css: { lightningcss: { errorRecovery: true } },
})
The plugin changes one line of globalize.js, the culture library those five components load: before 27.1.0 it was written for a classic script, where this is the window, and the dev server serves it as an ES module, where this is undefined. The production build is not affected. In 27.1.0 the file finds the window itself, and the plugin changes nothing. errorRecovery lets Vite's CSS minifier drop the Internet Explorer hacks (*zoom: 1) that some themes still contain (in 27.1.0 light-ie and dark-ie), instead of stopping the build.
Replace src/main.ts:
import { createApp } from 'vue';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.base.css';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.tailwind-blue-dark.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;
}
The grid reads its rows through a jqx.dataAdapter. The Grid component loads the jQWidgets core, which defines jqx as a global, so the script only declares its type. Replace src/App.vue:
<script setup lang="ts">
import { ref } from 'vue';
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;
const contacts = [
['Maria Anders', 'Sales Representative', 'Berlin', 'Germany'],
['Ana Trujillo', 'Owner', 'México D.F.', 'Mexico'],
['Antonio Moreno', 'Owner', 'México D.F.', 'Mexico'],
['Thomas Hardy', 'Sales Representative', 'London', 'UK'],
['Christina Berglund', 'Order Administrator', 'Luleå', 'Sweden'],
['Hanna Moos', 'Sales Representative', 'Mannheim', 'Germany'],
['Frédérique Citeaux', 'Marketing Manager', 'Strasbourg', 'France'],
['Martín Sommer', 'Owner', 'Madrid', 'Spain'],
['Laurence Lebihan', 'Owner', 'Marseille', 'France'],
['Elizabeth Lincoln', 'Accounting Manager', 'Tsawassen', 'Canada'],
['Victoria Ashworth', 'Sales Representative', 'London', 'UK'],
['Patricio Simpson', 'Sales Agent', 'Buenos Aires', 'Argentina'],
['Francisco Chang', 'Marketing Manager', 'México D.F.', 'Mexico'],
['Yang Wang', 'Owner', 'Bern', 'Switzerland'],
['Pedro Afonso', 'Sales Associate', 'São Paulo', 'Brazil'],
['Elizabeth Brown', 'Sales Representative', 'London', 'UK'],
['Sven Ottlieb', 'Order Administrator', 'Aachen', 'Germany'],
['Janine Labrune', 'Owner', 'Nantes', 'France'],
['Ann Devon', 'Sales Agent', 'London', 'UK'],
['Roland Mendel', 'Sales Manager', 'Graz', 'Austria']
];
// A plain constant, not a ref: Vue does not need to track the adapter.
const dataAdapter = new jqx.dataAdapter({
localdata: contacts,
datatype: 'array',
datafields: [
{ name: 'ContactName', type: 'string', map: '0' },
{ name: 'Title', type: 'string', map: '1' },
{ name: 'City', type: 'string', map: '2' },
{ name: 'Country', type: 'string', map: '3' }
]
});
const columns = [
{ text: 'Contact Name', datafield: 'ContactName', width: 240 },
{ text: 'Contact Title', datafield: 'Title', width: 240 },
{ text: 'City', datafield: 'City', width: 150 },
{ text: 'Country', datafield: 'Country' }
];
const selected = ref('none');
// The rowselect event carries the row's data in event.args.row.
function onRowSelect(event: any) {
selected.value = event.args.row?.ContactName ?? 'none';
}
</script>
<template>
<main>
<JqxGrid
theme="tailwind-blue-dark"
:width="800"
:autoheight="true"
:source="dataAdapter"
:columns="columns"
:pageable="true"
:pagesize="10"
:sortable="true"
:filterable="true"
:showfilterrow="true"
:columnsresize="true"
@rowselect="onRowSelect"
/>
<p>Selected contact: {{ selected }}</p>
</main>
</template>
Options are props with the same names as in the JavaScript API, and events keep their names too: rowselect is @rowselect.
npm run dev
Open http://localhost:5173. Click a column header to sort, type in the filter row to filter, and select a row to see its name below the grid.
Put a template ref on the grid to call its methods. This example exports the rows to Excel:
<script setup lang="ts">
import { ref } from 'vue';
import JqxGrid from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxgrid.vue';
declare const jqx: any;
// The grid methods this component calls.
interface GridMethods {
exportdata: (dataType: string, fileName?: string) => void;
}
const myGrid = ref<GridMethods | null>(null);
const dataAdapter = new jqx.dataAdapter({
localdata: [
{ name: 'Maria Anders', city: 'Berlin' },
{ name: 'Thomas Hardy', city: 'London' }
],
datatype: 'array',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'city', type: 'string' }
]
});
const columns = [
{ text: 'Name', datafield: 'name', width: 200 },
{ text: 'City', datafield: 'city' }
];
function exportToExcel() {
myGrid.value?.exportdata('xlsx', 'contacts');
}
</script>
<template>
<JqxGrid ref="myGrid" theme="tailwind-blue-dark" :width="400" :autoheight="true" :source="dataAdapter" :columns="columns" />
<button type="button" @click="exportToExcel">Export to Excel</button>
</template>
Next: Add a Chart to a Vue 3 app, Grid getting started, Grid API. Live Grid demos: jqwidgets.com/vue.