Skip to content

Commit c7c4419

Browse files
authored
docs: rows guide (#5515)
1 parent 8630fee commit c7c4419

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

docs/config.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@
106106
"label": "Rows",
107107
"to": "guide/rows"
108108
},
109-
{
110-
"label": "Headers",
111-
"to": "guide/headers"
112-
},
113109
{
114110
"label": "Cells",
115111
"to": "guide/cells"
112+
},
113+
{
114+
"label": "Header Groups",
115+
"to": "guide/headers-groups"
116+
},
117+
{
118+
"label": "Headers",
119+
"to": "guide/headers"
116120
}
117121
],
118122
"frameworks": [

docs/guide/header-groups.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Header Groups Guide
3+
---
4+
5+
## API
6+
7+
[Header Group API](../../api/core/header-group)
8+
9+
## Header Groups Guide

docs/guide/rows.md

+90
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,93 @@ title: Rows Guide
55
## API
66

77
[Row API](../../api/core/row)
8+
9+
## Rows Guide
10+
11+
This quick guide will discuss the different ways you can retrieve and interact with row objects in TanStack Table.
12+
13+
### Where to Get Rows From
14+
15+
There are multiple `table` instance APIs you can use to retrieve rows from the table instance.
16+
17+
#### table.getRow
18+
19+
If you need to access a specific row by its `id`, you can use the `table.getRow` table instance API.
20+
21+
```js
22+
const row = table.getRow(rowId)
23+
```
24+
25+
#### Row Models
26+
27+
The `table` instance generates `row` objects and stores them in useful arrays called ["Row Models"](../row-models). This is discussed in much more detail in the [Row Models Guide](../row-models), but here are the most common ways you may access the row models.
28+
29+
##### Render Rows
30+
31+
```jsx
32+
<tbody>
33+
{table.getRowModel().rows.map(row => (
34+
<tr key={row.id}>
35+
{/* ... */}
36+
</tr>
37+
))}
38+
</tbody>
39+
```
40+
41+
##### Get Selected Rows
42+
43+
```js
44+
const selectedRows = table.getSelectedRowModel().rows
45+
```
46+
47+
### Row Objects
48+
49+
Every row object contains row data and many APIs to either interact with the table state or extract cells from the row based on the state of the table.
50+
51+
#### Row IDs
52+
53+
Every row object has an `id` property that makes it unique within the table instance. By default the `row.id` is the same as the `row.index` that is created in the row model. However, it can be useful to override each row's `id` with a unique identifier from the row's data. You can use the `getRowId` table option to do this.
54+
55+
```js
56+
const table = useReactTable({
57+
columns,
58+
data,
59+
getRowId: originalRow => originalRow.uuid, //override the row.id with the uuid from the original row's data
60+
})
61+
```
62+
63+
> Note: In some features like grouping and expanding, the `row.id` will have additional string appended to it.
64+
65+
#### Access Row Values
66+
67+
The recommended way to access data values from a row is to use either the `row.getValue` or `row.renderValue` APIs. Using either of these APIs will cache the results of the accessor functions and keep rendering efficient. The only difference between the two is that `row.renderValue` will return either the value or the `renderFallbackValue` if the value is undefined, whereas `row.getValue` will return the value or `undefined` if the value is undefined.
68+
69+
```js
70+
// Access data from any of the columns
71+
const firstName = row.getValue('firstName') // read the row value from the firstName column
72+
const renderedLastName = row.renderValue('lastName') // render the value from the lastName column
73+
```
74+
75+
> Note: `cell.getValue` and `cell.renderValue` are shortcuts for the `row.getValue` and `row.renderValue` APIs, respectively.
76+
77+
#### Access Original Row Data
78+
79+
For every row object, you can access the original corresponding `data` that was passed to the table instance via the `row.original` property. None of the data in the `row.original` will have been modified by the accessors in your column definitions, so if you were doing any sort of data transformations in your accessors, those will not be reflected in the `row.original` object.
80+
81+
```js
82+
// Access any data from the original row
83+
const firstName = row.original.firstName // { firstName: 'John', lastName: 'Doe' }
84+
```
85+
86+
### Sub Rows
87+
88+
If you are using either grouping or expanding features, your rows may contain sub-rows or parent row references. This is discussed in much more detail in the [Expanding Guide](../expanding), but here is a quick overview of useful properties and methods for working with sub-rows.
89+
90+
- `row.subRows`: An array of sub-rows for the row.
91+
- `row.depth`: The depth of the row (if nested or grouped) relative to the root row array. 0 for root level rows, 1 for child rows, 2 for grandchild rows, etc.
92+
- `row.parentId`: The unique ID of the parent row for the row (The row that contains this row in its subRows array).
93+
- `row.getParentRow`: Returns the parent row for the row, if it exists.
94+
95+
### More Row APIs
96+
97+
Depending on the features that you are using for your table, there are dozens more useful APIs for interacting with rows. See each features' respective API docs or guide for more information.

0 commit comments

Comments
 (0)