Get selected node in TreeSelect

UI Components for React
Post Reply
ravinder.tawni
Posts: 20
Joined: 31 May 2016, 13:13

03 Feb 2023, 09:23

I am using the TreeSelect component in my project. I am using a complex object with children in it. I want to get the whole selected object on the selection of any value.

Code: Select all

{
    key: '0',
    label: 'Documents',
    data: 'Documents Folder',
    icon: 'pi pi-fw pi-inbox',
    children: [
        {
            key: '0-0',
            label: 'Work',
            data: 'Work Folder',
            icon: 'pi pi-fw pi-cog',
            children: [
                { key: '0-0-0', label: 'Expenses.doc', icon: 'pi pi-fw pi-file', data: 'Expenses Document' },
                { key: '0-0-1', label: 'Resume.doc', icon: 'pi pi-fw pi-file', data: 'Resume Document' }
            ]
        },
        {
            key: '0-1',
            label: 'Home',
            data: 'Home Folder',
            icon: 'pi pi-fw pi-home',
            children: [{ key: '0-1-0', label: 'Invoices.txt', icon: 'pi pi-fw pi-file', data: 'Invoices for this month' }]
        }
    ]
Let's say in above object if I select the first child('work'), I want whole object

Code: Select all

{
            key: '0-0',
            label: 'Work',
            data: 'Work Folder',
            icon: 'pi pi-fw pi-cog',
            children: [
                { key: '0-0-0', label: 'Expenses.doc', icon: 'pi pi-fw pi-file', data: 'Expenses Document' },
                { key: '0-0-1', label: 'Resume.doc', icon: 'pi pi-fw pi-file', data: 'Resume Document' }
            ]
        

ravinder.tawni
Posts: 20
Joined: 31 May 2016, 13:13

03 Feb 2023, 10:12

I found a solution. Although it was tough to find it because of documentation style.

Code: Select all

onNodeSelect={(e)=>{ }}
can be used. This event return the whole node and can be accessed with

Code: Select all

e.node

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests