Dashboards & Visualizations
Highlighted

How to dynamically remove components (eg. SingleView) from a dashboard created with javascript.

Path Finder

I have written a django template for a dashboard which dynamically adds singleview elements to the dashboard based on the number of events that are returned. I have also added a timerange picker at the top. However when I change the time the searches for each individual singleview element updates but I can't seem to delete the singleview elements themselves to then recreate news ones for the new data. There will be a different number of singleview elements depending on the time range picked.

How do I delete all of the singleview elements in the dashboard when the timerange changes?

I have tried:

for (index = 0, len = Object.keys(splunkjs.mvc.Components.attributes).length; index < len; ++index) {
          var id = Object.keys(splunkjs.mvc.Components.attributes)[index];
          if ( /^network_view/.test(id) ){
                    splunkjs.mvc.Components.getInstance(id).dispose();
          }
}

This essentially finds all the components that exist and dispose of the ones with a name like "networkview1" or "networkview_2" etc.
However I get a problem where some of the new singleviews created are 'undefined' in the components list and are therefore not deleted by this. Then the new ones aren't created because it find duplicates with the same instance name.

Is there another way to do this or am I missing something in my method?

Highlighted

Re: How to dynamically remove components (eg. SingleView) from a dashboard created with javascript.

Path Finder

I solved this.

Instead of using:

splunkjs.mvc.Components.getInstance(id).dispose();

I use:

splunkjs.mvc.Components.getInstance(id).remove();

However this also removed the html section that held the SingleView element.
So therefore what I needed to do was also add a line which re-adds the html section.
My new code looks like this:

for (index = 0, len = Object.keys(splunkjs.mvc.Components.attributes).length; index < len; ++index) {
                                        var id = Object.keys(splunkjs.mvc.Components.attributes)[index];
                                        if ( /^network_view/.test(id) ){
                                                splunkjs.mvc.Components.getInstance(id).remove();
                                        }
                                }
                                document.getElementById("network_panel").innerHTML+= '<div id="network_status" class="splunk-view splunk-single">'

Where "networkpanel" is the name of my original html section which contains the SingleViews and "networkstatus" is the name of the SingleViews section.

View solution in original post