reactabular-virtualized
works in conjunction with reactabular-sticky
and provides virtualization. The idea is that instead of rendering all table cells, it renders only ones visible at the viewport. This improves performance significantly with larger datasets.
If you want extra logging during development, set
window.LOG_VIRTUALIZED = true
at console.
Example:
/*import React from 'react';import * as Sticky from 'reactabular-sticky';import * as Virtualized from 'reactabular-virtualized';import * as resolve from 'table-resolver'; import { generateRows } from './helpers';*/ const columns = property: 'id' props: style: minWidth: 50 header: label: 'Index' cell: formatters: value rowIndex <span>rowIndex</span> property: 'name' props: style: minWidth: 300 header: label: 'Name' property: 'age' props: style: minWidth: 100 header: label: 'Age' property: 'company' props: style: minWidth: 400 header: label: 'Company' property: 'product' props: style: minWidth: 400 header: label: 'Product' ; const schema = type: 'object' properties: id: type: 'string' name: type: 'string' product: type: 'string' company: type: 'string' age: type: 'integer' required: 'id' 'name' 'product' 'company' 'age';// Resolving indices is an optional step. You can skip it if you don't// rely on rowIndex anywhere. But if you do, it's good to calculate and// include to the data. Reactabular's rendering logic is able to pick it// up by convention (`_index` field).const rows = resolve; Component { ; thisstate = rows columns ; thistableHeader = null; thistableBody = null; } { // We have refs now. Force update to get those to Header/Body. this; } { return <div> <div> <label>Scroll to index: </label> <input ="text" = /> </div> <Table.Provider ="pure-table pure-table-striped" = = > <Sticky.Header = = = /> <Virtualized.Body = ="id" = = = = /> </Table.Provider> </div> ; } <VirtualizedTable />
Scrolling within a container
By passing in a function as container
which returns a reference to a container to Virtualized.Body
, you can use the scrollbar of a surrounding element as the scroll for virtualization. This might be useful if you want to render multiple containers in the same scrolling area, or if you want the table to scroll along with the rest of your page.
/*import React from 'react';import * as Sticky from 'reactabular-sticky';import * as Virtualized from 'reactabular-virtualized';import * as resolve from 'table-resolver'; import { generateRows } from './helpers';*/ const columns = property: 'id' props: style: minWidth: 50 header: label: 'Index' cell: formatters: value rowIndex <span>rowIndex</span> property: 'name' props: style: minWidth: 300 header: label: 'Name' property: 'age' props: style: minWidth: 100 header: label: 'Age' property: 'company' props: style: minWidth: 400 header: label: 'Company' property: 'product' props: style: minWidth: 400 header: label: 'Product' ; const schema = type: 'object' properties: id: type: 'string' name: type: 'string' product: type: 'string' company: type: 'string' age: type: 'integer' required: 'id' 'name' 'product' 'company' 'age';// Resolving indices is an optional step. You can skip it if you don't// rely on rowIndex anywhere. But if you do, it's good to calculate and// include to the data. Reactabular's rendering logic is able to pick it// up by convention (`_index` field).const rows = resolve; Component { return <div> <div = => 1 2 3 </div> </div> ; } <VirtualizedTable />
Scrolling to Index
Virtualized.Body
ref
exposes scrollTo
method for scrolling through index. If you want to scroll based on some field value, search the dataset first and pass the resulting index here.
Define the Height of Table
Please note height
of <Virtualized.Body>
must be defined. If height
is not defined, style.maxHeight
will be used. In below example, we use style.maxHeight
instead of height
,
/*import React from 'react';import * as Sticky from 'reactabular-sticky';import * as Virtualized from 'reactabular-virtualized'; import { generateRows } from './helpers';*/ const columns = property: 'id' props: style: minWidth: 350 header: label: 'Index' property: 'name' props: style: minWidth: 300 header: label: 'Name' ;const schema = type: 'object' properties: id: type: 'string' name: type: 'string' required: 'id' 'name';const rows = ; Component { ; thisstate = rows columns ; thistableHeader = null; thistableBody = null; } { // We have refs now. Force update to get those to Header/Body. this; } { return <div> <Table.Provider ="pure-table pure-table-striped" = = > <Sticky.Header = = = /> <Virtualized.Body = ="id" = = = /> </Table.Provider> </div> ; } <VirtualizedTable />
Using relative column widths
You can define column widths as percentages of the table's width, like so:
const columns = property: 'id' props: style: width: "35%" ...
To force rows to stretch to the full width of the tbody
container, however, a flex
-based styling workaround is required. This StackOverflow answer suggests the configuration below. If this does not work for you, due to other styling constraints or concerns about browser compatibility, you may consider the other answers posted there, or you may need to take care of your column sizing and resizing in JavaScript.
} }}