Skip to content

fix: compute correct level when child precedes parent in tree rows#677

Open
spike-rabbit wants to merge 1 commit intomainfrom
fix/group-rows-by-parents-level
Open

fix: compute correct level when child precedes parent in tree rows#677
spike-rabbit wants to merge 1 commit intomainfrom
fix/group-rows-by-parents-level

Conversation

@spike-rabbit
Copy link
Copy Markdown
Member

@spike-rabbit spike-rabbit commented Apr 28, 2026

What kind of change does this PR introduce? (check one with "x")

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

groupRowsByParents (projects/ngx-datatable/src/lib/utils/tree.ts) assigns level = parent.level + 1 in a single forward pass. If a child precedes its parent in the input array, parent.level is undefined, the child gets NaN, and the cell template renders margin-left: NaNpx — affected rows have no tree indent.

What is the new behavior?

Levels are resolved on first touch with memoized recursion, so order in the input array no longer matters. The algorithm stays O(n); cycles are broken via a per-recursion visited set. Root order, child order, orphan and self-reference handling are preserved.

Added tree.spec.ts with unit tests covering ordered/unordered/deep trees, orphans, cycles, pass-through, and collapsed parents.

Does this PR introduce a breaking change? (check one with "x")

  • Yes
  • No

Other information:

npm run lint and npm run test:unit pass locally.

@spike-rabbit spike-rabbit requested a review from a team as a code owner April 28, 2026 13:04
@gemini-code-assist
Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@spike-rabbit spike-rabbit force-pushed the fix/group-rows-by-parents-level branch from 4a0badf to d0674d9 Compare April 28, 2026 13:09
@spike-rabbit
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the groupRowsByParents utility to correctly calculate row levels regardless of their order in the input array and adds a comprehensive test suite. A critical issue was identified in the cycle detection logic where cyclic dependencies could prevent any root nodes from being identified, potentially resulting in an empty output; a revised implementation with proper backtracking was suggested to resolve this.

Comment thread projects/ngx-datatable/src/lib/utils/tree.ts
@spike-rabbit spike-rabbit force-pushed the fix/group-rows-by-parents-level branch from 7793e60 to ea0db93 Compare April 28, 2026 13:31
@fh1ch fh1ch requested a review from Copilot April 30, 2026 13:08
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes tree-row level calculation in ngx-datatable when a child row appears before its parent, preventing NaN levels (and broken indentation) by resolving levels via memoized parent recursion and adding dedicated unit coverage.

Changes:

  • Reworked groupRowsByParents to resolve row.level via recursive parent traversal with memoization and cycle detection.
  • Added tree.spec.ts unit tests covering ordered/unordered trees, deep nesting, orphans, cycles, passthrough behavior, and collapsed-parent flattening.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
projects/ngx-datatable/src/lib/utils/tree.ts Replaces single forward-pass level assignment with memoized recursive resolution + cycle breaking.
projects/ngx-datatable/src/lib/utils/tree.spec.ts Adds unit tests to validate level computation and flattening behavior across edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread projects/ngx-datatable/src/lib/utils/tree.ts
Comment thread projects/ngx-datatable/src/lib/utils/tree.ts Outdated
Previously `groupRowsByParents` computed `row.level` in a single
forward pass, so a child appearing before its parent in the input
array got `undefined + 1 = NaN`. The cell template then rendered
`margin-left: NaNpx` and the row had no tree indent.

Resolve each node's level on first touch, recursing into the parent
when needed. Each node is resolved at most once per invocation, so
the algorithm stays O(n).
@spike-rabbit spike-rabbit force-pushed the fix/group-rows-by-parents-level branch from ea0db93 to 4bb3c18 Compare May 4, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants