You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/cells.md
+78
Original file line number
Diff line number
Diff line change
@@ -5,3 +5,81 @@ title: Cells Guide
5
5
## API
6
6
7
7
[Cell API](../../api/core/cell)
8
+
9
+
## Cells Guide
10
+
11
+
This quick guide will discuss the different ways you can retrieve and interact with cell objects in TanStack Table.
12
+
13
+
### Where to Get Cells From
14
+
15
+
Cells come from [Rows](../rows). Enough said, right?
16
+
17
+
There are multiple `row` instance APIs you can use to retrieve the appropriate cells from a row depending on which features you are using. Most commonly, you will use the `row.getAllCells` or `row.getVisibleCells` APIs (if you are using column visibility features), but there are a handful of other similar APIs that you can use.
18
+
19
+
### Cell Objects
20
+
21
+
Every cell object can be associated with a `<td>` or similar cell element in your UI. There are a few properties and methods on `cell` objects that you can use to interact with the table state and extract cell values from the table based on the state of the table.
22
+
23
+
#### Cell IDs
24
+
25
+
Every cell object has an `id` property that makes it unique within the table instance. Each `cell.id` is constructed simply as a union of its parent row and column IDs separated by an underscore.
26
+
27
+
```js
28
+
{ id:`${row.id}_${column.id}` }
29
+
```
30
+
31
+
During grouping or aggregation features, the `cell.id` will have additional string appended to it.
32
+
33
+
#### Cell Parent Objects
34
+
35
+
Every cell stores a reference to its parent [row](../rows) and [column](../columns) objects.
36
+
37
+
#### Access Cell Values
38
+
39
+
The recommended way to access data values from a cell is to use either the `cell.getValue` or `cell.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 `cell.renderValue` will return either the value or the `renderFallbackValue` if the value is undefined, whereas `cell.getValue` will return the value or `undefined` if the value is undefined.
40
+
41
+
> Note: The `cell.getValue` and `cell.renderValue` APIs are shortcuts `row.getValue` and `row.renderValue` APIs, respectively.
42
+
43
+
```js
44
+
// Access data from any of the columns
45
+
constfirstName=cell.getValue('firstName') // read the cell value from the firstName column
46
+
constrenderedLastName=cell.renderValue('lastName') // render the value from the lastName column
47
+
```
48
+
49
+
#### Access Other Row Data from Any Cell
50
+
51
+
Since every cell object is associated with its parent row, you can access any data from the original row that you are using in your table using `cell.row.original`.
52
+
53
+
```js
54
+
// Even if we are in the scope of a different cell, we can still access the original row data
Depending on the features that you are using for your table, there are dozens more useful APIs for interacting with cells. See each features' respective API docs or guide for more information.
61
+
62
+
### Cell Rendering
63
+
64
+
You can just use the `cell.renderValue` or `cell.getValue` APIs to render the cells of your table. However, these APIs will only spit out the raw cell values (from accessor functions). If you are using the `cell: () => JSX` column definition options, you will want to use the `flexRender` API utility from your adapter.
65
+
66
+
Using the `flexRender` API will allow the cell to be rendered correctly with any extra markup or JSX and it will call the callback function with the correct parameters.
> Note: The comparison function does not need to take whether or not the column is in descending or ascending order into account. The row models will take of that logic. `sortingFn` functions only need to provide a consistent comparison.
162
+
161
163
Every sorting function receives 2 rows and a column ID and are expected to compare the two rows using the column ID to return `-1`, `0`, or `1` in ascending order. Here's a cheat sheet:
0 commit comments