v0.5.0
GitHub

reactivePagination

The reactivePagination function creates a pagination plugin for table instances, enabling efficient navigation through large datasets by dividing data into manageable pages.

Signature

			function reactivePagination<T>(
	options?: PaginationOptions
): TablePlugin<T, PaginationState, 'pagination'>;
		

Parameters

ParameterTypeDescription
optionsPaginationOptionsOptional configuration for the pagination behavior

PaginationOptions

PropertyTypeDefaultDescription
pageSizenumber10Number of items per page
pagenumber0Initial page index (0-based)

Return Value

Returns a TablePlugin that adds pagination functionality when passed to the use method of the table.

Basic Usage

			<script lang="ts">
	import { reactiveTable, reactivePagination } from 'svelte-reactive-table';

	const data = [
		/* Your data array */
	];
	const columns = [
		/* Your column definitions */
	];

	// Create a table with pagination plugin
	const table = reactiveTable(data, columns).use(
		reactivePagination({
			pageSize: 10,
			page: 0
		})
	);

	// Access the pagination API through table.plugins
	const { pagination } = table.plugins;
</script>

<table>
	<!-- Table headers -->
	<tbody>
		{#each table.rows as row}
			<tr>
				{#each row.cells as cell}
					<td>{cell.value}</td>
				{/each}
			</tr>
		{/each}
	</tbody>
</table>
		

Pagination Properties

When pagination is enabled, these properties are available:

PropertyTypeDescription
pagination.pagenumberCurrent page index (0-based)
pagination.pageSizenumberNumber of items per page
pagination.pageCountnumberTotal number of pages
pagination.isFirstPagebooleanWhether current page is the first page
pagination.isLastPagebooleanWhether current page is the last page
pagination.hasPreviousPagebooleanWhether there is a previous page available
pagination.hasNextPagebooleanWhether there is a next page available

Pagination Methods

These methods are available on the pagination plugin state:

MethodReturn TypeDescription
setPage(page: number)voidGo to a specific page
setPageSize(size: number, resetPage?: boolean)voidChange page size (with optional page reset)
goToNextPage()booleanGo to next page (returns success status)
goToPreviousPage()booleanGo to previous page (returns success status)
goToFirstPage()voidGo to the first page
goToLastPage()voidGo to the last page

Example Controls

			<div class="pagination">
	<button onclick={() => pagination.goToPreviousPage()} disabled={!pagination.hasPreviousPage}>
		Previous
	</button>

	<span>Page {pagination.page + 1} of {pagination.pageCount}</span>

	<button onclick={() => pagination.goToNextPage()} disabled={!pagination.hasNextPage}>
		Next
	</button>

	<select bind:value={pagination.pageSize}>
		<option value={5}>5 per page</option>
		<option value={10}>10 per page</option>
		<option value={25}>25 per page</option>
	</select>
</div>
		
			<script>
	// Navigate to next page with error handling
	function handleNextPage() {
		if (!pagination.goToNextPage()) {
			console.log('Already on last page');
		}
	}

	// Jump to specific page
	function jumpToPage(pageNumber) {
		pagination.setPage(pageNumber - 1); // Convert to 0-based
	}

	// Change page size and maintain position when possible
	function changePageSize(newSize) {
		pagination.setPageSize(newSize, false); // Don't reset to first page
	}
</script>
		

TypeScript Support

			import { reactiveTable, reactivePagination } from 'svelte-reactive-table';

type User = {
	id: number;
	name: string;
	age: number;
};

const table = reactiveTable<User>(users, columns).use(reactivePagination({ pageSize: 10 }));

// TypeScript will infer the correct pagination state type
const { pagination } = table.plugins;