Having trouble obtaining the accurate index of the clicked node in d3 in order to update the corresponding JSON data

When using a collapsible tree diagram like the one found at https://bl.ocks.org/mbostock/4339083, my goal is to make a server call to retrieve additional children and add them to the existing JSON data upon loading.

While I have successfully achieved this on click events, determining the correct index of the clicked node in order to update the JSON accordingly has proven to be a challenge.

The depth of the children within the JSON structure is associated with the function(d) { return d.depth }, but accurately identifying which child was clicked at that specific depth seems to be problematic.

Initially, it may seem straightforward by using the index provided by the function(i) in d3, however, this index changes as nodes are opened or closed (try opening and closing nodes in the example link).

Is there a find method or another feature in d3 that can help me leverage the correct index?

depth: 2 = children.children 

What is the best way to access the x's mentioned above?

root.children[x].children[x].children = childArray;

Although the example only shows one layer of children, this functionality needs to work recursively so that every node can be clicked to reveal more data.

See the full example here (Line 264): http://codepen.io/pjbrof/pen/kXzGpd

root.children[0].children[0].children = childArray;
update(root);

Answer №1

From my personal observation, the index provided to every D3 accessor function always corresponds to its position in the document object model (DOM) relative to its parent element: https://github.com/d3/d3-selection/issues/73

To address this issue, I have implemented a solution in the click event by either using an object lookup or an array filter based on a unique identifier.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain a JSON object in JSP by making an AJAX call

Obtaining the JSONObject from my bean is done within my helper class. Within the helper: public JSONObject init() throws Exception{ // codes for initialization are located here JSONObject json = JSONObject.fromObject(bean); return ...

How to dynamically populate a grid in ExtJs 4 with data from a JSON source

Hey there, I'm a beginner with ExtJS and I've run into a bit of an issue. I'm attempting to load data into an ExtJS grid that is serialized as JSON on the server (using Django's serialize() method), but unfortunately, I'm seeing an ...

Steps to remove a child node and retrieve the parent element

My jQuery skills are not the best and I could really use some help. Here's the situation: I have an HTML table structured like this : <table id="table"> <tr><td><img class="image" />test</td><td></td></tr& ...

Arrange an array of objects by making a nested API call in Angular

My task involves sorting an array of objects based on the response from the first API call in ascending order. The initial API call returns a list of arrays which will be used for the subsequent API call. The first API call fetches something like this: [0 ...

"Exploring the process of manipulating local storage and the potential for

I am in the process of developing a web application that heavily relies on localstorage. Currently, I have it configured to save data to the local storage using this specific format: Key: Four digit number Value: My data Now, my goal is to gather all the ...

Invoke a method within an *ngIf statement in Angular 5

Suppose I have the following code snippet: <div *ngIf="item">lorem ipsum</div> Is there a way to trigger a function if the *ngIf condition is true? Perhaps something like this... <div *ngIf="(item) : callFunction() ? ...">lorem ipsum& ...

How to make a list with a scroll bar in an HTML table cell

Having trouble creating a scrollable list in a dynamically appended HTML table cell using JavaScript? You're not alone. Even after assigning the list to the cell parent, it seems that the list is not behaving as expected - it's not scrollable and ...

Stacked divs experiencing layout issues in Firefox browser

An innovative book animation has been crafted, showcasing flipping pages with a clever use of z-index to stack the pages. While everything displays correctly in the browser UI, an interesting situation arises when inspecting elements. In Firefox, selectin ...

Managing multiple markers using the LayersControl.Overlay in React Leaflet

I am working on a project that involves displaying multiple public facilities in a city on a map, each represented by a marker. I want to implement a feature where users can filter these facilities based on their typology using checkboxes. Each typology wi ...

Endless conversion cycle encountered while employing custom JSON converter

In my current project, I am facing an issue where I encounter an infinite loop while attempting to convert an Item or any of its subclasses such as ArmorItem. To determine the type of Item for deserialization, I have implemented a custom JsonConverter na ...

What is the best way to implement states for checkbox Booleans within a todo list, allowing users to easily add or remove items as needed?

Currently, I have set up a todo list feature on my website where users can add and remove items. The goal is to allow them to also check or uncheck items as needed. While the functionality to add or delete items is working smoothly and the checkboxes upda ...

What is the term used to describe the way console.log styles the Json object?

Have you ever noticed that when a JSON object is printed, say in a script run by node using console.log, it doesn't exactly pretty print the JSON? It sort of strikes a balance between showing as few lines as possible while still maintaining readabilit ...

Ways to retrieve a specific Array[property] within an object?

I am struggling to access a specific property within an array of objects. My goal is to extract the "name" elements from the app catalog array, combine them with the names from the custom array apps.name, and assign the result to a new property in the ques ...

The event listener for "popstate" is not triggering on the .document

Struggling with implementing a popstate event handler on an Ajax page using the .document object: document.addEventListener("popstate", myPopState); Despite my efforts, this handler never seems to fire. I was hoping that after reloading the page, the po ...

One way to use AngularJs and Bootstrap is by utilizing a button to set a value in a type

Greetings to all, I am new here and this is my debut question on stack overflow! I am currently attempting to create an application that automatically inputs a value into a typeahead field when a button is clicked. Does anyone happen to know of a solution ...

What is the best way to set up a promise for an HTTP GET request?

How can promises be implemented around the provided http get request? $http({ method: 'GET', url: 'getData.php', params: {bill: 'Active'} }) .then(function (response) { bill=response.data.results; }); There i ...

Inquiries prior to projecting rays

As I delve into the world of Interaction with Three.js, several questions arise in my mind. 1) Can you shed some light on what exactly Viewport Coordinates are? 2) In what ways do they differ from client Coordinates? 3) How did we come up with this form ...

Receiving JSX from deeply nested dynamic imports in the latest version of NextJS 12 triggers an error message stating that [object Promise] is

There have been numerous proposed solutions for this particular issue on Stackoverflow, but none of them seem to work for my specific code. It appears that my situation is somewhat unique due to the nested dynamic imports involved: In a NextJS component, ...

On a button click, the div's height will increase and then decrease when the same button is clicked again

I need help with a div that has its height set to 0px initially, and I want it to increase to 300px when a button is clicked. When the button is clicked again, I want the height to go back to 0px. Here's the CSS code: nav{ height:0px; overfl ...

Leveraging asynchronous data in a synchronous manner

I am dealing with tax rate data stored in the database to ensure easy updates when necessary. However, JavaScript's asynchronous nature complicates accessing this data as it requires promises or callbacks to retrieve query results. Is there a solution ...