Do I need to return other data inside javascript treemap json?

I have an asp.net mvc website and I use javascript treemap control to display a heatmap . This control works fine, but I wanted to see if I can insert some other json fields into the callback that is used to update another div.

Does anyone know if this is possible. Right now I have to bounce back and forth and make 2 separate ajax calls, but I wanted to see if I could pass this information along with the treemap json response.

+6
source share
2 answers

What you need to do is one call in which you make the callback yourself. Here json will be a structure like

var data = { heatmap_data: {} other_data: {} } 

and the next call

 tm.loadJSON(data.heatmap_data); 

load heat map data and use all other data as you wish. Should this do the trick? At least that is, if loadJSON accepts the object as it seems. However, this answer seems too simple, so I may not fully understand the meaning.

+5
source

You have a specific structure for your TreeMap, basically you have a node with the keys id , name , data and children . As far as I read in the docs, there are no restrictions on other keys. This way you can add additional keys inside the data attribute.

For example, your json answer might look like this:

 { "data": { "myCustomData": { /* your data here */ } }, "id": "root", "name": "Top Albums", "children": [ { "data": { "playcount": 547, "$area": 547, "myCustomData": { /* your data here */ } }, "id": "artist_A Perfect Circle", "name": "A Perfect Circle" } ] } 

If you want to use your extra data, you do this:

 ... onClick: function(node) { ... if( node.data.myCustomData ){ /*** you have data, do something here ***/ } } 

Here you have a LIVE EXAMPLE with a warning when you click on nodes with myCustomData . hover over the do-it-yourself field in the upper left corner to see user data in a tooltip and a warning with user data when clicked.

Find the code for "mycustomdata" to find out how.

0
source

Source: https://habr.com/ru/post/910134/


All Articles