holiber / sl-vue-tree

Customizable draggable tree component for Vue.js
MIT License
344 stars 79 forks source link

Remove node using contextmenu not working if node is not selected #78

Open zealan-spender opened 4 years ago

zealan-spender commented 4 years ago

I've discovered that right-clicking does not select the node, therefore clicking 'Remove' from the contextmenu will not remove the node unless node is previously selected.

So the example with contextmenu should be updated in order to work: showContextMenu(node, event) { event.preventDefault(); const $slVueTree = this.$refs.slVueTree; $slVueTree.select( node.path ); ...

rmirabelle commented 4 years ago

I have a workaround for this. Right clicking a node should definitely select the node first, but the default behavior is not to. Luckily the workaround is straightforward. Drill into the sl-vue-tree API via its ref and manually select the target node on right click:

<template>
   <sl-vue-tree @nodecontextmenu="onNodeRightClicked" ref="tree"/>
</template>
<script>
export default {
   methods: {
      onNodeRightClicked(node, e) {
         e.preventDefault()
         //get the tree reference
         const tree = this.$refs.tree
         //use the API to select the node
         tree.select(node.path)
         //do the rest of your work
      }
   }
}
</script>

I'd really like to see this fabulous library updated to do this automatically, since there's no need for a context menu with no context!