Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A React table component.

License

NotificationsYou must be signed in to change notification settings

rsuite/rsuite-table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A React table component.

npmGitHub ActionsCoverage Status

Features

  • Support virtualized.
  • Support fixed header, fixed column.
  • Support custom adjustment column width.
  • Support for custom cell content.
  • Support for displaying a tree form.
  • Support for sorting.
  • Support for expandable child nodes
  • Support for RTL

Preview

  • Fixed Column

  • Custom Cell

  • Tree Table

  • Expandable

More Examples

Install

# use npmnpm i rsuite-table# or use yarnyarn add rsuite-table# or use pnpmpnpm add rsuite-table

Usage

import{Table,Column,HeaderCell,Cell}from'rsuite-table';import'rsuite-table/lib/less/index.less';// or 'rsuite-table/dist/css/rsuite-table.css'constdataList=[{id:1,name:'a',email:'a@email.com',avartar:'...'},{id:2,name:'b',email:'b@email.com',avartar:'...'},{id:3,name:'c',email:'c@email.com',avartar:'...'}];constImageCell=({ rowData, dataKey, ...rest})=>(<Cell{...rest}><imgsrc={rowData[dataKey]}width="50"/></Cell>);constApp=()=>(<Tabledata={dataList}><Columnwidth={100}sortablefixedresizable><HeaderCell>ID</HeaderCell><CelldataKey="id"/></Column><Columnwidth={100}sortableresizable><HeaderCell>Name</HeaderCell><CelldataKey="name"/></Column><Columnwidth={100}sortableresizable><HeaderCell>Email</HeaderCell><Cell>{(rowData,rowIndex)=>{return<ahref={`mailto:${rowData.email}`}>{rowData.email}</a>;}}</Cell></Column><Columnwidth={100}resizable><HeaderCell>Avartar</HeaderCell><ImageCelldataKey="avartar"/></Column></Table>);

API

<Table>

PropertyType (Default)Description
affixHeaderboolean, numberAffix the table header to a specified position on the page.
affixHorizontalScrollbarboolean, numberAffix the table's horizontal scrollbar to a specified position on the page.
autoHeightbooleanAutomatically expand the table's height based on the number of data rows, without displaying a vertical scrollbar.
borderedbooleanDisplay table borders.
cellBorderedbooleanDisplay cell borders.
children(components: { Cell, HeaderCell, Column, ColumnGroup }) => ReactNode | ReactNodeRender props that receive parameterized Cell, HeaderCell, Column, and ColumnGroup components, making TypeScript usage more convenient.
data *RowData[]Table data.
defaultExpandAllRowsbooleanExpand all rows by default.
defaultExpandedRowKeysstring[]Specify the initially expanded rows by their keys.
defaultSortType'desc' | 'asc'Default sort type.
expandedRowKeysstring[]Specify the expanded rows by their keys (Controlled).
fillHeightbooleanForce the table's height to match its parent container's height. Cannot be used withautoHeight.
headerHeightnumber (40)Table header height.
heightnumber (200)Table height.
hoverboolean (true)Enable row hover effects.
isTreebooleanDisplay the table as a tree structure.
loadingbooleanShow a loading state.
locale{emptyMessage: string, loading: string}Messages for empty data and loading states.
maxHeightnumberMaximum table height.
minHeightnumber (0)Minimum table height.
onExpandChange(expanded: boolean, rowData: RowData) => voidCallback function triggered when a tree table node is expanded or collapsed.
onRowClick(rowData: RowData, event: SyntheticEvent) => voidCallback function triggered when a row is clicked, returning the row data.
onRowContextMenu(rowData: RowData, event: SyntheticEvent) => voidCallback function triggered by a context menu event, returning the row data.
onScroll(scrollX: object, scrollY: object) => voidCallback function for scrollbar scroll events.
onSortColumn(dataKey: string, sortType: string) => voidCallback function triggered when the sort order changes, returning the column key and sort type.
renderEmpty(info: ReactNode) => ReactNodeCustom content to display when there is no data.
renderLoading(loading: ReactNode) => ReactNodeCustom content to display during data loading.
renderRow(children?: ReactNode, rowData?: RowData) => ReactNodeCustom row element renderer.
renderRowExpanded(rowData?: RowData) => ReactNodeCustom content to display in an expanded row.
renderTreeToggle(icon: ReactNode, rowData: RowData, expanded: boolean) => ReactNodeCustom toggle icon for expanding/collapsing tree nodes.
rowClassNamestring, (rowData: RowData, rowIndex: number) => stringAdd an optional custom class name to rows.
rowExpandedHeightnumber (100), (rowData?: RowData) => numberSet the height of expanded rows.
rowHeightnumber (46), (rowData: RowData) => numberRow height.
rowKeystring ('key')Unique key for each row, derived from data.
rtlbooleanEnable right-to-left layout.
shouldUpdateScrollboolean, (event) => ({ x, y }) (true)Determine whether to update the scroll position after the table size changes.
showHeaderboolean (true)Display the table header.
sortColumnstringName of the column to sort by.
sortType'desc' | 'asc'Sort type (Controlled).
virtualizedbooleanEfficiently render large datasets.
widthnumberTable width.
wordWrapboolean | 'break-all' | 'break-word' | 'keep-all'Control text wrapping behavior within cells.

<Column>

PropertyType(Default)Description
align'left' | 'center' | 'right'Sets the text alignment within the column.
colSpannumberMerges cells within the column when thedataKey value for the merged cells isnull orundefined.
fixedboolean | 'left' | 'right'Fixes the column to the left or right side of the table.
flexGrownumberAutomatically adjusts the column width based on the value offlexGrow. Cannot be used withresizable andwidth properties.
fullTextbooleanDisplays the full text of the cell content when the mouse hovers over it.
minWidthnumber(200)Sets the minimum width of the column when usingflexGrow.
onResize(columnWidth?: number, dataKey?: string) => voidCallback function triggered after the column width changes.
resizablebooleanAllows the column width to be resized.
rowSpan(rowData: RowData) => numberMerges rows in the specified column.
sortablebooleanEnables sorting on the column.
treeColbooleanIndicates that the column is part of a tree structure.
verticalAlign'top' | 'middle' | 'bottom'Sets the vertical alignment of content within the column.
widthnumberSpecifies the column width.

sortable is used to define whether the column is sortable, but depending on whatkey sort needs to set adataKey inCell.The sort here is the service-side sort, so you need to handle the logic in the ' Onsortcolumn ' callback function of<Table>, and the callback function returnssortColumn,sortType values.

<ColumnGroup>

PropertyType(Default)Description
align'left' | 'center' | 'right'Sets the text alignment within the column group.
fixedboolean | 'left' | 'right'Fixes the column group to the left or right side of the table.
groupHeaderHeightnumberSets the height of the group header. The default value is 50% of the table'sheaderHeight.
headerReactNodeSpecifies the content to be displayed as the group header.
verticalAlign'top' | 'middle' | 'bottom'Sets the vertical alignment of content within the column group.

<HeaderCell>

PropertyType(Default)Description
childrenReactNodeSpecifies the content to be displayed in the column header.
renderSortIcon(sortType) => ReactNodeCustomizes the rendering of sort icons on column headers.

<Cell>

PropertyType(Default)Description
childrenReactNode | ((rowData: RowData, rowIndex?: number) => ReactNode)The content to be displayed in the cell.
dataKeystringThe key used for data binding and sorting.
rowDataRowDataThe data associated with the current row.
rowIndexnumberThe index of the current row.

There are three ways to use<Cell>, as follows:

  • 1.Associate the fields in the data withdataKey.
<Columnwidth="{100}"align="center"><HeaderCell>Name</HeaderCell><CelldataKey="name"/></Column>
  • 2.Customize a<Cell>.
constNameCell=({ rowData, ...props})=>(<Cell{...props}><ahref={`mailto:${rowData.email}`}>{rowData.name}<a></Cell>);<Columnwidth={100}align="center"><HeaderCell>Name</HeaderCell><NameCell/></Column>
  • 3.Customize functions directly within the<Cell>.
<Columnwidth={100}align="center"><HeaderCell>Name</HeaderCell><Cell>{(rowData,rowIndex)=>{return<ahref={`mailto:${rowData.email}`}>{rowData.name}</a>;}}</Cell></Column>

(For nested data read this:#158)

Table ref

PropertyTypeDescription
bodyHTMLDivElementThe body element of the table
rootHTMLDivElementThe root element of the table
scrollLeft(left:number)=>voidSet the number of pixels for horizontal scrolling of the table
scrollPosition{top:number,left:number}The scroll position of the table
scrollTop(top:number)=>voidSet the number of pixels for vertical scrolling of the table

Type safety

We can pass generic type parameters to Table, Cell etc. for better type-safety when using typescript.

Passing a render prop to Table is recommended when using TS, as this will ensure thatthe right generic type parameter is automatically propagated to the Cell component.

constproducts:Product[]=[{name:'Pineapple'}];<Table<Product,string>ref={table}data={products}>{({ Column, HeaderCell, Cell})=>(<><Column><HeaderCell>Name</HeaderCell>{/* No need for passing explicit type parameter to Cell */}<Cell>{row=>row.name}</Cell></Column></>)}</Table>;

In fact, the type parameter from table can be inferred from the data passed to it, so the type parameter to Table can also be skipped.

constproducts:Product[]=[{name:'Pineapple'}];<Tabledata={products}>{({ Column, HeaderCell, Cell})=>(<><Column><HeaderCell>Name</HeaderCell><Cell>{row=>row.name}</Cell></Column></>)}</Table>;

When writing reusable components, it is recommended to make your components generic as well. For example:

interfaceImageCellProps<TKeyextendsstring,TRowextendsRecord<TKey,string>>{rowData:TRow;dataKey:TKey;// ... any other props}constImageCell=<TKeyextendsstring,TRowextendsRecord<TKey,string>>({  rowData,  dataKey,  ...rest}:ImageCellProps<TKey,TRow>)=>(<Cell<TRow,TKey>{...rest}><imgsrc={rowData[dataKey]}width="50"/></Cell>);

[8]ページ先頭

©2009-2025 Movatter.jp