Table.md

December 26, 2019 · View on GitHub

Table

Table component with fixed headers and windowed rows for improved performance with large data sets. This component expects explicit width and height parameters. Table content can scroll vertically but it is not meant to scroll horizontally.

Prop Types

PropertyTypeRequired?Description
autoHeightBooleanOuter height of Table is set to "auto". This property should only be used in conjunction with the WindowScroller HOC.
childrenColumnOne or more Columns describing the data displayed in this table
classNameStringOptional custom CSS class name to attach to root Table element.
disableHeaderBooleanDo not render the table header (only the rows)
estimatedRowSizeNumberUsed to estimate the total height of a Table before all of its rows have actually been measured. The estimated total height is adjusted as rows are rendered.
gridClassNameStringOptional custom CSS class name to attach to inner Grid element
gridStyleObjectOptional inline style to attach to inner Grid element
headerClassNameStringCSS class to apply to all column headers
headerHeightNumberFixed height of header row
headerRowRendererFunctionResponsible for rendering the table header row given an array of columns. Learn more
headerStyleObjectOptional custom inline style to attach to table header columns.
heightNumberFixed/available height for out DOM element
idStringOptional custom id to attach to root Table element.
noRowsRendererFunctionCallback used to render placeholder content when :rowCount is 0
onColumnClickFunctionCallback invoked when a user clicks on a table column. ({ columnData: any, dataKey: string, event: Event }): void
onHeaderClickFunctionCallback invoked when a user clicks on a table header. ({ columnData: any, dataKey: string, event: Event }): void
onRowClickFunctionCallback invoked when a user clicks on a table row. ({ event: Event, index: number, rowData: any }): void
onRowDoubleClickFunctionCallback invoked when a user double-clicks on a table row. ({ event: Event, index: number, rowData: any }): void
onRowMouseOutFunctionCallback invoked when the mouse leaves a table row. ({ event: Event, index: number, rowData: any }): void
onRowMouseOverFunctionCallback invoked when a user moves the mouse over a table row. ({ event: Event, index: number, rowData: any }): void
onRowRightClickFunctionCallback invoked when a user right-clicks on a table row. ({ event: Event, index: number, rowData: any }): void
onRowsRenderedFunctionCallback invoked with information about the slice of rows that were just rendered: ({ overscanStartIndex: number, overscanStopIndex: number, startIndex: number, stopIndex: number }): void
overscanRowCountNumberNumber of rows to render above/below the visible bounds of the list. This can help reduce flickering during scrolling on certain browsers/devices. See here for an important note about this property.
onScrollFunctionCallback invoked whenever the scroll offset changes within the inner scrollable region: ({ clientHeight: number, scrollHeight: number, scrollTop: number }): void
rowClassNameString or FunctionCSS class to apply to all table rows (including the header row). This value may be either a static string or a function with the signature ({ index: number }): string. Note that for the header row an index of -1 is provided.
rowCountNumberNumber of rows in table.
rowGetterFunctionCallback responsible for returning a data row given an index. ({ index: int }): any
rowHeightNumber or FunctionEither a fixed row height (number) or a function that returns the height of a row given its index: ({ index: number }): number
rowRendererFunctionResponsible for rendering a table row given an array of columns. Learn more
rowStyleObject or FunctionOptional custom inline style to attach to table rows. This value may be either a style object or a function with the signature ({ index: number }): Object. Note that for the header row an index of -1 is provided.
scrollToAlignmentStringControls the alignment scrolled-to-rows. The default ("auto") scrolls the least amount possible to ensure that the specified row is fully visible. Use "start" to always align rows to the top of the list and "end" to align them bottom. Use "center" to align them in the middle of container.
scrollToIndexNumberRow index to ensure visible (by forcefully scrolling if necessary)
scrollTopNumberVertical offset
sortFunctionSort function to be called if a sortable header is clicked. ({ defaultSortDirection: string, event: MouseEvent, sortBy: string, sortDirection: SortDirection }): void
sortByStringData is currently sorted by this dataKey (if it is sorted at all)
sortDirectionSortDirectionData is currently sorted in this direction (if it is sorted at all)
styleObjectOptional custom inline style to attach to root Table element.
tabIndexNumberOptional override of inner Grid tab index default; defaults to 0.
widthNumberWidth of the table

Public Methods

forceUpdateGrid

Forcefully re-render the inner Grid component.

Calling forceUpdate on Table may not re-render the inner Grid since it uses shallowCompare as a performance optimization. Use this method if you want to manually trigger a re-render. This may be appropriate if the underlying row data has changed but the row sizes themselves have not.

getOffsetForRow ({ alignment: ?string, index: ?number })

Gets offset for a given row and alignment.

getScrollbarWidth

Gets the scrollbar width used to pad the table-header.

measureAllRows

Pre-measure all rows in a Table.

Typically rows are only measured as needed and estimated heights are used for cells that have not yet been measured. This method ensures that the next call to getTotalSize() returns an exact size (as opposed to just an estimated one).

recomputeRowHeights (index: number)

Recompute row heights and offsets after the specified index (defaults to 0).

Table has no way of knowing when its underlying list data has changed since it only receives a rowHeight property. If the rowHeight is a number it can compare before and after values but if it is a function that comparison is error prone. In the event that a dynamic rowHeight function is in use and the row heights have changed this function should be manually called by the "smart" container parent.

This method will also force a render cycle (via forceUpdate) to ensure that the updated measurements are reflected in the rendered table.

scrollToPosition (scrollTop: number)

Scroll to the specified offset. Useful for animating position changes.

scrollToRow (index: number)

Ensure row is visible. This method can be used to safely scroll back to a cell that a user has scrolled away from even if it was previously scrolled to.

Class names

The Table component supports the following static class names

PropertyDescription
ReactVirtualized__TableMain (outer) element
ReactVirtualized__Table__headerColumnnHeader cell (similar to thead > tr > th)
ReactVirtualized__Table__headerRowHeader row (similar to thead > tr)
ReactVirtualized__Table__rowTable row (akin to tbody > tr)
ReactVirtualized__Table__rowColumnTable column (akin to tbody > tr > td)
ReactVirtualized__Table__sortableHeaderColumnApplied to header columns that are sortable
ReactVirtualized__Table__sortableHeaderIconSVG sort indicator

headerRowRenderer

This is an advanced property. It is useful for situations where you require additional hooks into Table to render additional custom UI elements. You may want to start by forking the defaultTableHeaderRowRenderer function.

This function accepts the following named parameters:

PropertyDescription
classNameHeader class name
columnsArray of React nodes
styleHeader style object

rowRenderer

This is an advanced property. It is useful for situations where you require additional hooks into Table (eg integration with a library like react-sortable-hoc). If you do override rowRenderer the easiest way is to decorate the default implementation like so:

import {SortableContainer, SortableElement} from 'react-sortable-hoc';
import {defaultTableRowRenderer, Table} from 'react-virtualized';

const SortableTable = SortableContainer(Table);
const SortableTableRowRenderer = SortableElement(defaultTableRowRenderer);

function rowRenderer(props) {
  return <SortableTableRowRenderer {...props} />;
}

function CustomizedTable(props) {
  return <SortableTable rowRenderer={rowRenderer} {...props} />;
}

If you require greater customization, you may want to fork the defaultTableRowRenderer function.

This function accepts the following named parameters:

PropertyDescription
classNameRow-level class name
columnsArray of React nodes
indexRow index
isScrollingBoolean flag indicating if Table is currently being scrolled
onRowClickOptional row onClick handler
onRowDoubleClickOptional row onDoubleClick handler
onRowMouseOverOptional row onMouseOver handler
onRowMouseOutOptional row onMouseOut handler
rowDataRow data
styleRow-level style object
keyUnique key within array of rendered rows

Examples

Below is a very basic Table example. This table has only 2 columns, each containing a simple string. Both have a fixed width and neither is sortable. See here for a more full-featured example including custom cell renderers, sortable headers, and more.

import React from 'react';
import ReactDOM from 'react-dom';
import {Column, Table} from 'react-virtualized';
import 'react-virtualized/styles.css'; // only needs to be imported once

// Table data as an array of objects
const list = [
  {name: 'Brian Vaughn', description: 'Software engineer'},
  // And so on...
];

// Render your table
ReactDOM.render(
  <Table
    width={300}
    height={300}
    headerHeight={20}
    rowHeight={30}
    rowCount={list.length}
    rowGetter={({index}) => list[index]}>
    <Column label="Name" dataKey="name" width={100} />
    <Column width={200} label="Description" dataKey="description" />
  </Table>,
  document.getElementById('example'),
);