package.src.utils.getPaginationRowModel.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of table-core Show documentation
Show all versions of table-core Show documentation
Headless UI for building powerful tables & datagrids for TS/JS.
The newest version!
import { Table, RowModel, Row, RowData } from '../types'
import { memo } from '../utils'
import { expandRows } from './getExpandedRowModel'
export function getPaginationRowModel(opts?: {
initialSync: boolean
}): (table: Table) => () => RowModel {
return table =>
memo(
() => [
table.getState().pagination,
table.getPrePaginationRowModel(),
table.options.paginateExpandedRows
? undefined
: table.getState().expanded,
],
(pagination, rowModel) => {
if (!rowModel.rows.length) {
return rowModel
}
const { pageSize, pageIndex } = pagination
let { rows, flatRows, rowsById } = rowModel
const pageStart = pageSize * pageIndex
const pageEnd = pageStart + pageSize
rows = rows.slice(pageStart, pageEnd)
let paginatedRowModel: RowModel
if (!table.options.paginateExpandedRows) {
paginatedRowModel = expandRows({
rows,
flatRows,
rowsById,
})
} else {
paginatedRowModel = {
rows,
flatRows,
rowsById,
}
}
paginatedRowModel.flatRows = []
const handleRow = (row: Row) => {
paginatedRowModel.flatRows.push(row)
if (row.subRows.length) {
row.subRows.forEach(handleRow)
}
}
paginatedRowModel.rows.forEach(handleRow)
return paginatedRowModel
},
{
key: process.env.NODE_ENV === 'development' && 'getPaginationRowModel',
debug: () => table.options.debugAll ?? table.options.debugTable,
}
)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy