Dashboards & Visualizations

How to use token in javascript file

viandyg
Engager

I have a multiselect panel on my dashboard and js file attached to the dashboard as well. How do i pass the token value from the multiselect into the javascript file?

e.g.:

<dashboard script="xyz.js">
  <label>xyz</label>

<fieldset submitButton="false">
<input type="multiselect" token="filter.abc" searchWhenChanged="true">
  <label>abc</label>
  <fieldForLabel>abc</fieldForLabel>
  <fieldForValue>abc</fieldForValue>
  <search>
    <query>index="index" | stats count by abc</query>
    <earliest>0</earliest>
    <latest></latest>
  </search>
  <choice value="*">All</choice>
  <default>*</default>
</input>


<row>
    <panel>
      <html>
            <div class="bk-root" id="dbdf03be-ce3a-4730-a185-e613c25394f9" data-root-id="1190"/>
        </html>
    </panel>
  </row>
</dashboard>

What do I need to change in the xyz.js so that it would be able to read the $abc$ token?

0 Karma

sandyIscream
Communicator

First you need to set a ID for your multiselect.

for example -

then you need to define the js like the following.

require([
'underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/utils',
'splunkjs/mvc/tokenutils',
'splunkjs/mvc/simplexml/ready!'], function(_, $, mvc, utils, TokenUtils){
var tokens = mvc.Components.get('default');

var abcvalue = splunkjs.mvc.Components.getInstance("abcselect"); // default value is getting captured here
if (abcvalue != undefined) {
    abcvalue.on("change", function(e){
        console.log("abc changed to "+tokens.get("abc")); //on token change the value gets captured into through tokens.get("abc")
        define your function here
    });
}

});

Also please remember to put the JS file in $SPLUNK_APP$/appserver/static folder

0 Karma

sandyIscream
Communicator

First you need to set a ID for your multiselect.

for example -

then you need to define the js like the following.

require([
'underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/utils',
'splunkjs/mvc/tokenutils',
'splunkjs/mvc/simplexml/ready!'], function(_, $, mvc, utils, TokenUtils){
var tokens = mvc.Components.get('default');

var abcvalue = splunkjs.mvc.Components.getInstance("abcselect"); // default value is getting captured here
if (abcvalue != undefined) {
    abcvalue.on("change", function(e){
        console.log("abc changed to "+tokens.get("abc")); //on token change the value gets captured into through tokens.get("abc")
        define your function here
    });
}

});

Also please remember to put the JS file in $SPLUNK_APP$/appserver/static folder

0 Karma
Get Updates on the Splunk Community!

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

A Guide To Cloud Migration Success

As enterprises’ rapid expansion to the cloud continues, IT leaders are continuously looking for ways to focus ...