TreeSelect: how to only make the leaf nodes selectable? #1603
Replies: 4 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
i may be late but: https://primevue.org/treeselect/#api.options.TreeNode.selectable |
Beta Was this translation helpful? Give feedback.
-
Same question here, spent 3 hours on this today. Setting the parent node's selectable property to false does not work as then the parents are not only not selectable, but not expandable either, so there is no way to view their children. This seems like a very common use case, disappointed that there is not way to do this. Nothing in the documentation on creating a custom dropdown panel (slots are shown but no examples) |
Beta Was this translation helpful? Give feedback.
-
To make only the leaf nodes selectable in a tree structure, you can use JavaScript to add event listeners or apply CSS classes based on node types. Here’s a sample approach using JavaScript and HTML: HTML<ul id="tree">
<li class="node">Node 1
<ul>
<li class="node leaf">Leaf 1</li>
<li class="node leaf">Leaf 2</li>
</ul>
</li>
<li class="node">Node 2
<ul>
<li class="node leaf">Leaf 3</li>
<li class="node leaf">Leaf 4</li>
</ul>
</li>
</ul> CSS.node {
cursor: pointer;
}
.leaf {
cursor: pointer;
}
.leaf.selected {
background-color: yellow;
} JavaScriptdocument.addEventListener('DOMContentLoaded', () => {
const tree = document.getElementById('tree');
tree.addEventListener('click', (event) => {
const target = event.target;
if (target.classList.contains('leaf')) {
target.classList.toggle('selected');
// Handle leaf node selection logic here
console.log('Leaf node selected:', target.textContent);
}
});
}); This approach ensures that only leaf nodes are selectable and can be styled or manipulated as required. Read more: https://history-spot.com/ |
Beta Was this translation helpful? Give feedback.
-
I am new to PrimeVue and I am using TreeSelect component. The TreeSelect works fine but I wanted to make changes to it so that only the leaf nodes are selectable not the parent nodes.
<TreeSelect v-model="selectedValue" :options="NodeData" placeholder="Select Template" class="md:w-[20rem] w-full" />
I want only the children to be selectable.
Can someone help me?
Beta Was this translation helpful? Give feedback.
All reactions