Skip to content
Source
Sign inSign up
TanStack/table

Add `getLeafHeaders()` method to skip group headers #6579

Opensarathkumarsasi(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/6579/headopened

Showing 3 changed files with 23 additions and 20 deletions.

x
0 of 3 viewed, 3 left
1 addition and 0 deletionsdocs/guide/headers.mdModified
View file
@@ -58,6 +58,7 @@
- `isPlaceholder`: A boolean flag that is true if the header is a placeholder header. Placeholder headers fill the rows above a shallow leaf column's real header so that every header row accounts for every visible column. Render them as empty cells to keep the header grid aligned, or use `header.rowSpan` to merge each placeholder chain into one vertically spanning header cell.
- `placeholderId`: The unique identifier for the placeholder header.
- `subHeaders`: The array of sub/child headers that belong to this header. Will be empty if the header is a leaf header.
+- `getLeafHeaders()`: The leaf headers nested under this header, in left-to-right order. Group and placeholder headers are excluded, so a leaf header returns just itself. Use `table.getFlatHeaders()` if you need every header, group headers included.
​
> [!NOTE]
> `header.index` refers to its index within the header group (row of headers), i.e. its position from left to right. It is not the same as `header.depth`, which refers to the header group "row index".
7 additions and 4 deletionspackages/table-core/src/core/headers/coreHeadersFeature.utils.tsModified
View file
@@ -19,18 +19,21 @@
header: Header_Header<TFeatures, TData, TValue>,
leafHeaders: Array<Header<TFeatures, TData, TValue>>,
): void {
+ if (!header.subHeaders.length) {
+ leafHeaders.push(header as Header<TFeatures, TData, TValue>)
+ return
+ }
+​
for (let i = 0; i < header.subHeaders.length; i++) {
collectLeafHeaders(header.subHeaders[i]!, leafHeaders)
-​
- leafHeaders.push(header as Header<TFeatures, TData, TValue>)
​
/**
* Walks a header tree and collects all descendant leaf headers.