Skip to content

Pagination Remote

This example shows how to use pagination with remote data in the grid. To enable pagination you’ll need to use the PaginationPlugin. This plugin provides the necessary functionality to paginate data within the grid.

Source code
src/components/pagination/PaginationRemote.ts
import { defineCustomElements } from '@revolist/revogrid/loader';
defineCustomElements(); // Define the custom element
import {
PaginationPlugin,
ColumnStretchPlugin,
type PaginationFullConfig
} from '@revolist/revogrid-pro';
import { fetch, type PaginatedResponse } from '../composables/remoteData';
import { currentTheme } from '../composables/useRandomData';
import type { Person } from '../composables/makeData';
const { isDark } = currentTheme();
export function load(parentSelector: string) {
const grid = document.createElement('revo-grid');
/**
* Handles pagination for a grid component.
* Takes in parameters:
* - config (pagination configuration)
* - revogrid (the grid element).
*/
const paginationService = async ({
config: cfg,
revogrid,
}: {
config: PaginationFullConfig;
revogrid: HTMLRevoGridElement;
}) => {
/**
* Set the initial skip and take values
*/
let skip = 0;
let config = { ...cfg };
/**
* Request data from remote server
*/
async function updateGridItems({
skip,
take,
count,
}: {
skip: number;
take: number;
count: boolean;
}) {
// Show loader
revogrid.classList.add('busy');
// Send request to remote server to get data
try {
const url = `/api/users?skip=${skip}&take=${take}&count=${count}`;
const response = await fetch(url);
if (!response.ok) {
const errorData = await response.json();
throw new Error(`Error ${response.status}: ${errorData.message}`);
}
const result: PaginatedResponse<Person> = await response.json();
// Hide loader
revogrid.classList.remove('busy');
return result;
} catch (error) {
// Hide loader
revogrid.classList.remove('busy');
console.error('Failed to fetch users with pagination:', error);
throw error;
}
}
/**
* Event handler for page change and update the skip and take values
*/
grid.addEventListener('pagechange', ({ detail }) => {
/**
* Update the skip and take values
*/
skip = detail.skip;
/**
* Request new data
*/
updateGridItems({
skip: detail.skip * config.itemsPerPage,
take: config.itemsPerPage,
count: false,
}).then((result) => {
revogrid.source = result.data;
});
});
/**
* Request the data source of the grid
*/
const { data, count } = await updateGridItems({
skip,
take: config.itemsPerPage,
count: true,
});
/**
* Update pagination plugin data if count is present
*/
if (count) {
config.total = count;
revogrid.getPlugins().then((plugins) => {
/**
* Find the pagination plugin
*/
const paginationPlugin = plugins.find(
(plugin) => plugin instanceof PaginationPlugin,
);
(paginationPlugin as PaginationPlugin)?.setConfig(config);
});
}
revogrid.source = data;
};
/**
* Set the columns of the grid
*/
grid.columns = [
{
name: 'First Name',
prop: 'firstName',
},
{
name: 'Last Name',
prop: 'lastName',
},
{
name: 'Relationship',
prop: 'status',
}
];
/**
* Set the additional data for the grid
*/
grid.additionalData = {
/**
* Add stretch config to the grid
*/
stretch: 'all',
};
// Define pagination plugin
grid.plugins = [PaginationPlugin, ColumnStretchPlugin];
/**
* Run pagination service
*/
paginationService({
config: {
itemsPerPage: 10,
initialPage: 0,
total: 0,
buttonCount: 5,
},
revogrid: grid,
});
grid.theme = isDark() ? 'darkCompact' : 'compact';
grid.hideAttribution = true;
document.querySelector(parentSelector)?.appendChild(grid);
}