最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

recursion - How to create selectedKeys structure for Tree component of Primevue v3 - Stack Overflow

matteradmin9PV0评论

I have an layersTree object that contains data for items' rendering and selectedKeys for setting the checked state of a tree's checkboxes. By deafult selectedKeys is empty object.

const layersTree = {
    "data": [
        {
            "visible_default": true,
            "key": "20",
            "label": "Group layer 1",
            "children": [
                {
                    "visible_default": true,
                    "key": "57",
                    "label": "Layer 1"
                },
            ]
        },
        {
            "visible_default": true,
            "key": "21",
            "label": "Group layer 2",
            "children": [
                {
                    "visible_default": true,
                    "key": "22",
                    "label": "Group layer 3",
                    "children": [
                        {
                            "visible_default": false,
                            "key": "22",
                            "label": "Layer 4"
                        },
                        {
                            "visible_default": false,
                            "key": "35",
                            "label": "Layer 5"
                        }
                    ]
                },
                {
                    "visible_default": true,
                    "key": "35",
                    "label": "Layer 5"
                }
            ]
        }
    ],
    "selectedKeys": {},
}

I need to set selectedKeys to value, which includes checked and partialChecked boolean values like this:

{
    "20": {
        "checked": true,
        "partialChecked": false
    },
    "21": {
        "checked": true,
        "partialChecked": false
    },
    "22": {
        "checked": true,
        "partialChecked": false
    },
    "23": {
        "checked": true,
        "partialChecked": false
    }
}

So, how can I correctly apply recursive tree traversal to generate selectedKeys object?

Post a comment

comment list (0)

  1. No comments so far