Dashboards & Visualizations

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

cchitten
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 "network_view1" or "network_view_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?

1 Solution

cchitten
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 "network_panel" is the name of my original html section which contains the SingleViews and "network_status" is the name of the SingleViews section.

View solution in original post

cchitten
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 "network_panel" is the name of my original html section which contains the SingleViews and "network_status" is the name of the SingleViews section.

Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...