Splunk Dev

mvc.Components.getInstance("input2"); for multiple inputs?

dt201482
New Member

I have the following code that I am using from this link (modified slightly) to remove or add my All select as needed. Works perfectly for my input 2....but I have 5 more inputs I want it to do the exact same functionality on. I can't really find much documentation on the methods available for mvc.Components, so I can't tell if there is a getClass, or some similar functionality. Anyone have a clue on how I can do below, but for all inputs matching input2 - input8? Relevant code is from line 11 through 34

Edit: Can't add links to answers from this site yet.....

require(['jquery','underscore','splunkjs/mvc', 'website_status_cell_renderer', 'splunkjs/mvc/simplexml/ready!'],
    function($, _, mvc, WebsiteStatusCellRenderer){

        var statusTable = mvc.Components.get('element2');

        statusTable.getVisualization(function(tableView){
            tableView.table.addCellRenderer(new WebsiteStatusCellRenderer());
            tableView.table.render();
        });

      var selection = [];
      var multi = mvc.Components.getInstance("input2");

      multi.on("change", function(){
          // get the current selection
          selection = multi.val();

          // check if there is more than one entry and one of them is "*"
          if (selection.length > 1 && ~(selection.indexOf("*"))) {
              if (selection.indexOf("*") == 0) {
                  // "*" was first, remove it and leave rest
                  selection.splice(selection.indexOf("*"), 1);
                  multi.val(selection);
                  multi.render();
              } else {
                  // "*" was added later, remove rest and leave "*"
                  multi.val("*");
                  multi.render();
              }
          } else if ( selection.length < 1){
            multi.val("*");
            multi.render();
          }
      });


    }
);
Tags (3)
0 Karma
1 Solution

LukeMurphey
Champion

If you know that inputs 2 through 8 need to be modified, you could just use a function to iterate through the inputs accordingly:

       function setupMultiInput(instance_id){
         var multi = mvc.Components.getInstance(instance_id);

         multi.on("change", function(){
             // get the current selection
             selection = multi.val();

             // check if there is more than one entry and one of them is "*"
             if (selection.length > 1 && ~(selection.indexOf("*"))) {
                 if (selection.indexOf("*") == 0) {
                     // "*" was first, remove it and leave rest
                     selection.splice(selection.indexOf("*"), 1);
                     multi.val(selection);
                     multi.render();
                 } else {
                     // "*" was added later, remove rest and leave "*"
                     multi.val("*");
                     multi.render();
                 }
             } else if ( selection.length < 1){
               multi.val("*");
               multi.render();
             }
         });
       }

       for(var c = 2; c <= 8; c++){
        setupMultiInput("input" + c);
       }

View solution in original post

0 Karma

LukeMurphey
Champion

If you know that inputs 2 through 8 need to be modified, you could just use a function to iterate through the inputs accordingly:

       function setupMultiInput(instance_id){
         var multi = mvc.Components.getInstance(instance_id);

         multi.on("change", function(){
             // get the current selection
             selection = multi.val();

             // check if there is more than one entry and one of them is "*"
             if (selection.length > 1 && ~(selection.indexOf("*"))) {
                 if (selection.indexOf("*") == 0) {
                     // "*" was first, remove it and leave rest
                     selection.splice(selection.indexOf("*"), 1);
                     multi.val(selection);
                     multi.render();
                 } else {
                     // "*" was added later, remove rest and leave "*"
                     multi.val("*");
                     multi.render();
                 }
             } else if ( selection.length < 1){
               multi.val("*");
               multi.render();
             }
         });
       }

       for(var c = 2; c <= 8; c++){
        setupMultiInput("input" + c);
       }
0 Karma

dt201482
New Member

Thanks. Exactly what I needed. Not sure why I didn't think of it yesterday, brain must have been fried from learning how to do dashboards.

0 Karma

LukeMurphey
Champion

haha, we have all been there :).

BTW: I noticed you are using the WebsiteStatusCellRenderer. May I presume thats one from the Website Monitoring app? I'm just curious if you were making changes to that app or if you were using for something new. Let me know if you need any help with that too.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

 (view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...