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!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...