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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...