Splunk Dev

How to use SplunkJS to automatically "select all" checkboxes in a group?

xuehui
New Member

I want to add a button for a checkbox group, so that when I click the button all the checkboxes will be selected.
How can I do this by using SplunkJS?

Labels (2)
Tags (2)
0 Karma
1 Solution

vnravikumar
Champion

Hi

Try this

<form script="checkbox.js">
  <label>checkbox</label>
  <fieldset submitButton="false">
    <input type="checkbox" token="sports" id="rk">
      <label>Sports</label>
      <choice value="football">Football</choice>
      <choice value="baseball">Baseball</choice>
      <choice value="cricket">Cricket</choice>
      <choice value="boxing">Boxing</choice>
    </input>
  </fieldset>
  <row>
    <html>
      <div>
        <button id="all">Select All</button>
      </div>
    </html>
  </row>
</form>

js:

require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc) {
    $(document).ready(function(){
        $("#all").click(function(){
           var checkbox= mvc.Components.get("rk");
           var temp=[];
          var dictionary = checkbox.settings.get("choices");
           Object.keys(dictionary).forEach(function(key) {
               var checkboxvalues =  Object.values(dictionary[key].value).join("");
               temp.push(checkboxvalues);
        });
        checkbox.val(temp);
        });
    });
});

View solution in original post

0 Karma

vnravikumar
Champion

Hi

Try this

<form script="checkbox.js">
  <label>checkbox</label>
  <fieldset submitButton="false">
    <input type="checkbox" token="sports" id="rk">
      <label>Sports</label>
      <choice value="football">Football</choice>
      <choice value="baseball">Baseball</choice>
      <choice value="cricket">Cricket</choice>
      <choice value="boxing">Boxing</choice>
    </input>
  </fieldset>
  <row>
    <html>
      <div>
        <button id="all">Select All</button>
      </div>
    </html>
  </row>
</form>

js:

require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc) {
    $(document).ready(function(){
        $("#all").click(function(){
           var checkbox= mvc.Components.get("rk");
           var temp=[];
          var dictionary = checkbox.settings.get("choices");
           Object.keys(dictionary).forEach(function(key) {
               var checkboxvalues =  Object.values(dictionary[key].value).join("");
               temp.push(checkboxvalues);
        });
        checkbox.val(temp);
        });
    });
});
0 Karma

prashanthberam
Explorer

I Have similar problem but when we have multiple checkboxes in dashboard with different id's for each checkbox, How can we select/Deselect All checkboxes at time. below my sample xml code

<input type="checkbox" id="rk1" token="field1">
<label>Select/Unselect ALL</label>
<choice value="1">2</choice>
<delimiter> </delimiter>
</input>
<input type="checkbox" id="rk2" token="field2">
<label>Check3</label>
<choice value="chicago">chicago1</choice>
</input>
<input type="checkbox" id="rk3" token="field3">
<label>check3</label>
<choice value="Detriot">detroit1</choice>
</input>

0 Karma

xuehui
New Member

Thank you very much for your answer. I tried. It's okey.
but now,I hava a new question.
Q:when the checkboxgroup's options was creted by SPL, how can i get all options.
The following command has no effect.
[var dictionary = checkbox.settings.get("choices");]

0 Karma

xuehui
New Member

i implemented my function with the following JS:

var clsWfResults = search1.data("preview");
clsWfResults.on("data", function() {
var i = 0;
while (clsWfResults.data().rows[i][0] != "") {
array[i] = clsWfResults.data().rows[i][0];
i++;
}
});

$("#all_check_button").on("click",function(){
splunkjs.mvc.Components.get("input1").val(array);
});

$("#all_clear_button").on("click",function(){

splunkjs.mvc.Components.get("input1").val(null);
});

0 Karma
Get Updates on the Splunk Community!

What the End of Support for Splunk Add-on Builder Means for You

Hello Splunk Community! We want to share an important update regarding the future of the Splunk Add-on Builder ...

Solve, Learn, Repeat: New Puzzle Channel Now Live

Welcome to the Splunk Puzzle PlaygroundIf you are anything like me, you love to solve problems, and what ...

Building Reliable Asset and Identity Frameworks in Splunk ES

 Accurate asset and identity resolution is the backbone of security operations. Without it, alerts are ...