Primeng: p-tree selection not working as expected when using filtering

Created on 2 Jan 2020  路  4Comments  路  Source: primefaces/primeng

I'm submitting a

[x] bug report

Current behavior
The selected node is incorrect when using filtering option.
It seems that the selectedNode is set on the first parent node found in tree, when using filtering

Expected behavior
Selected node should reflect the real selection

Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/github-xz82dx

  1. Type "cpu" in filter input field
  2. Select the cpuStats leaf node
  3. The selected node is wrong : myFolder instead of cpuStats

If filter input is left blank everything is fine.

What is the motivation / use case for changing the behavior?
Behavior is not consistent

Please tell us about your environment:

  • Angular version: 9.0.0-rc.4
  • PrimeNG version: 9.0.0-rc.1
  • Browser: Tested on Chrome 79

Most helpful comment

Hey, @tinysquare I am having the same problem and found out that you need to have "key" property on your nodes. Just set key and everything is gonna work.

All 4 comments

I guess the problem comes from this code https://github.com/primefaces/primeng/blob/master/src/app/components/tree/tree.ts#L638 :

if (this.hasFilteredNodes()) {
  node = this.getNodeWithKey(node.key, this.value);

If think it should be this.filteredNodes instead of this.value in order to use the filteredTree and not the complete tree.

Hey, @tinysquare I am having the same problem and found out that you need to have "key" property on your nodes. Just set key and everything is gonna work.

Thanks, it works with key property set.

Anyway I think it's counterintuitive and at least documentation should mention that key is mandatory when using filtering. Morevover, the demo data doesnt have this key property so I dont know how they get it to work on showcase.
I noticed a link to https://github.com/primefaces/primeng/issues/7237#issue-409791407 (which is confusing because it says "If users have filter and checkbox selection features on Tree, they have to use 'key'" which is incorrect because I'm using single selection and it's still required) on showcase page, but it should be more obvious and explained in "Filtering" section, not on unnoticeable "_more_" link imo.

Unable to replicate with PrimeNG 9.0.5, if the issue persists please create a new ticket with a test case reproducing the issue e.g. stackblitz or a github repo and it will be reviewed by our team once again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Helayxa picture Helayxa  路  3Comments

mitosandov picture mitosandov  路  3Comments

cyberrranger picture cyberrranger  路  3Comments

miresk picture miresk  路  3Comments

papiroca-tm picture papiroca-tm  路  3Comments