dagre-d3 icon indicating copy to clipboard operation
dagre-d3 copied to clipboard

Adding a "button" to each node

Open lynerjy opened this issue 8 years ago • 5 comments

I'd like to modify my node shape so that all of them get drawn with a 'delete' button.

For example, a small square that I append to every node, which when clicked, can drive a series of actions. I want to make something I can refer to using something to the effect of svg.selectAll("g.deletebutton").on("click", function(id)...

Any ideas?

lynerjy avatar Jul 25 '16 08:07 lynerjy

What I've done is to use the labelType: 'html', and then added a button inside. I.e..

g.setNode(n.nodeid, {
        labelType: 'html',
        label: "<input type='button' class='button' value='button'>some description",
}

Right now, in order to see which node's button has been pressed, I have one function to check the node for a click, and another function to check if the button has been clicked.

svg.selectAll("g.node").on("click", function(id) {
    var clickednode = id;
}
'click .button': function(){
    buttonclicked = 'true'
}

And then I run my event on both those outputs.

Is there any easier way to just directly get the id of the node which the button is in, when it's clicked?

lynerjy avatar Aug 10 '16 17:08 lynerjy

I think you can use jQuery or pure js to do that.

alanhe421 avatar Nov 02 '17 08:11 alanhe421

I think you can use jQuery or pure js to do that.

@alanhg @cpettitt I know this thread is old but I'm working on a similar problem and jQuery as far as I can tell does not solve this. Just to mention the differences, in my case I have a button in each node, that when clicked should re-render the node to include more details about it. I made it work when I click the entire node but that has annoying side-effects (for example if people click a link that appears in the node, it simultaneously redirects and expands the node.

Since dagreD3 creates nodes and edges as svg objects, I need to be able to get the node object when I click on a button that is included in the node. The node object meaning not the corresponding tag, but the actual dagreD3 object, same as if I add an .on("click") to the entire node.

Is this possible?

deanp70 avatar Dec 08 '18 10:12 deanp70

Did anyone here find a possible solution to this?

RupalBarman-alation avatar May 17 '19 06:05 RupalBarman-alation

@RupalBarman-alation Add a tooltip and add your actions inside the tooltip, this way you don't have to worry about your buttons overlapping with other nodes and edges. This lib is only considers nodes and edges in the graph to calculate where things should be, has no idea about your extra elements.

hatpick avatar May 23 '19 22:05 hatpick