Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeTable: Selection of a node results in the selection of another node under a different parent if data is the same #14536

Closed
mconner opened this issue Jan 11, 2024 · 2 comments
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible

Comments

@mconner
Copy link

mconner commented Jan 11, 2024

Describe the bug

When if two nodes have the same data values (but are not the same instance), and one of them is selected, then the the other node will also be selected.

Environment

Windows 10, but duplicated in stackblitz

Reproducer

https://stackblitz.com/edit/f13fae?file=src%2Fapp%2Fdemo%2Ftree-table-selection-checkbox-demo.html

Angular version

17.0.8

PrimeNG version

17.3.2

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

v20.10.0

Browser(s)

Microsoft Edge for Business Version 120.0.2210.121 (Official build) (64-bit)

Steps to reproduce the behavior

The reproducer is a clone of the Checkbox Selection example: https://primeng.org/treetable#checkbox, however, I got similar results with the multiselect example. I duplicated the Angular data as Angular2 in NodeService.getFileSystemNodesData(), and changed the size of the cli.app child node.

  1. Start the reproducer. Note the instructions at the top that essentially will be duplicated here.
  2. Expand the "Applications" Node, and then both the "Angular" and "Angular2" child nodes.
  3. Select the "Angular" node (click the checkbox). You should see something like the following:
    image

Note that while node "Angular2" was never selected, some of its children were, in addition to those of "Angular". It appears that this is because the children's data are equivalent. The "cli.app" in "Angular2" was not selected because I modified its attributes to not match those of "cli.app" under "Angular".

I thought that including rowTrackBy might fix the problem. The documentation for rowTrackBy states:

Function to optimize the dom operations by delegating to ngForTrackBy, default algorithm checks for object identity.

However, while the rowTrackBy function is getting called, it had no effect on the outcome of the select.

I also tried adding an explicit key attribute to the node. It made no difference.

Possible Workaround: Adding a unique key to the data. This is ugly,but should work.

This may be related to #10228, which was closed in spite of the fact that the op indicated that the described solution had nothing to do with editing. But I couldn't run that reproducer to confirm.

Expected behavior

When you select a node in the treetable, other nodes that are not children of that node should not be selected.

@mconner mconner added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 11, 2024
@mertsincan
Copy link
Member

Hi @mconner,

I think you can use dataKey prop for it and set a unique key of nodes.

<TreeTable dataKey="id" ..../>

// each node need to contain unique `id` key. 

For now, closed. If the problem persists, please reopen this ticket.
Best Regards,

@mconner
Copy link
Author

mconner commented Jan 15, 2024

@mertsincan,
While adding dataKey="id" and then adding unique keys works (and is presumably more performant than just adding the key, as I had suggested in the workaround), it is kludgy, as the data now requires the key, and the API uses TreeNode, which already has a key attribute:
/**
* Mandatory unique key of the node.
*/
key?: string;

Why is that not used instead?

In any case, this should be better documented. The example in the doc only works because it didn't happen to have two nodes with the same data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible
Projects
None yet
Development

No branches or pull requests

2 participants