Skip to content

Pagination

Enable PaginationPlugin to efficiently manage large datasets by breaking them into smaller, more manageable pages, enhancing both performance and user experience. Pagination allows you to divide your dataset into discrete pages, each containing a specified number of rows. Users can then navigate through these pages rather than scrolling through a long list of data.

Source code
src/components/pagination/PaginationLocal.ts
import { defineCustomElements } from '@revolist/revogrid/loader';
defineCustomElements(); // Define the custom element
import { currentTheme, useRandomData } from '../composables/useRandomData';
import {
PaginationPlugin,
ColumnStretchPlugin,
type PaginationFullConfig,
} from '@revolist/revogrid-pro';
import type { DataType } from '@revolist/revogrid';
const { createRandomData } = useRandomData();
const { isDark } = currentTheme();
export function load(parentSelector: string) {
const grid = document.createElement('revo-grid');
/**
* Create a random data source for the grid
*/
const source = createRandomData(102);
/**
* Handles pagination for a grid component.
* Takes in three parameters:
* - data (an array of items)
* - config (pagination configuration)
* - revogrid (the grid element).
*/
const paginationService = <T extends DataType>({
data,
config,
revogrid,
}: {
data: T[];
config: PaginationFullConfig;
revogrid: HTMLRevoGridElement;
}) => {
/**
* Set the initial skip and take values
*/
let skip = 0;
/**
* Get the current items from the source array
*/
const getGridItems = () => data.slice(skip, config.itemsPerPage + skip);
/**
* Update the data source of the grid
*/
const setData = () => {
/**
* Get the new items for the grid
*/
const gridDataItems = getGridItems();
revogrid.source = gridDataItems;
};
/**
* 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;
setData();
});
/**
* Set the initial data
*/
setData();
};
/**
* Define pagination configuration
*/
const paginationConfig: PaginationFullConfig = {
itemsPerPage: 10,
initialPage: 0,
total: source.length,
buttonCount: 5,
};
/**
* Run pagination service
*/
paginationService({
data: source,
config: paginationConfig,
revogrid: grid,
});
/**
* Add pagination config to the grid
*/
const additionalData = {
pagination: paginationConfig,
stretch: 'all',
};
/**
* Set the columns of the grid
*/
grid.columns = [
{
name: '🆔 ID',
prop: 'id',
},
{
name: '🍎 Fruit',
prop: 'name',
},
{
name: '💰 Price',
prop: 'price',
},
];
/**
* Set the additional data for the grid
*/
grid.additionalData = additionalData;
// Define pagination plugin
grid.plugins = [PaginationPlugin, ColumnStretchPlugin];
grid.theme = isDark() ? 'darkCompact' : 'compact';
grid.hideAttribution = true;
document.querySelector(parentSelector)?.appendChild(grid);
}

This is especially useful when dealing with large datasets where loading all rows at once could affect performance. It gives you:

  • Improved Performance: By loading only a subset of data at a time, pagination helps maintain grid performance even with large datasets.
  • Enhanced User Experience: Users can easily navigate through data without being overwhelmed by a large number of rows.