deltoss / d3-mitch-tree

D3 plugin to create stunning interactive tree visualisations.
MIT License
82 stars 42 forks source link

How to delete a node with or without a child. #88

Closed adriavieira314 closed 3 years ago

adriavieira314 commented 3 years ago

Hello. I've been trying to remove a node dynamically but the way I've been doing it it's really not working because I may remove the selected node but its child remains and, of course, I get an error when building the tree because the properties can't find it's parent.

I actually just been filtering the object. Is there a better way to do this?

document.getElementById('removeButton').addEventListener('click', function(){
   var value = document.getElementById('removeText').value;
   data = data.filter(function( obj ) {
      return obj.name !== value;
   });

   var treePlugin = new d3.mitchTree.boxedTree()
      .setIsFlatData(true)
      .setData(data)
      .setElement(document.getElementById("visualisation"))
      .setIdAccessor(function(data) {
         return data.id;
      })
      .setParentIdAccessor(function(data) {
         return data.parentId;
      })
      .setBodyDisplayTextAccessor(function(data) {
         return data.description;
      })
      .setTitleDisplayTextAccessor(function(data) {
         return data.name;
      })
      .initialize();
});

This is how I've been doing.