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!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...