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!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...