Dashboards & Visualizations

Why does updating a token with JavaScript not cause panels dependent on those tokens to refresh?

andrewtrobec
Motivator

Hello,
I am having trouble understanding why a token update via JavaScript does not cause a panel that is dependent on that token to refresh. I have a date input called status_date that is configured to set a token tok_status_date during initialization (with today's date), and when it changes. JS file is as follows:

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

        // initialization
        var tokens = mvc.Components.get("default");
        document.getElementById("status_date").valueAsDate = new Date();
        tokens.set("tok_status_date",document.getElementById("status_date").value);

        $('#status_date').on("change", function (e){
                tokens.set("tok_status_date",document.getElementById("status_date").value);
        });
});

An HTML panel that I have configured will always show the value of tok_status_date, even when I update the value of the input.

<row>
    <panel>
        <html>
            <center><h1>$tok_status_date$</h1></center>
        </html>
    </panel>
</row>

I also have an XML panel whose search is dependent on tok_status_date, but it does not update whenever I change the value of the input:

<query>| makeresults | eval Day = strftime(strptime("$tok_status_date$","%Y-%m-%d"),"%d/%m/%Y") | table Day</query>

Why is this? Am I missing something in the JavaScript file?

Thank you and best regards,

Andrew

1 Solution

andrewtrobec
Motivator

I've managed to solve the problem. I was only working with the "default" tokens when I should have also been using the "submitted" tokens. Here is the script that works:

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

         // initialization
         var def_tok = mvc.Components.get("default");
         var sub_tok = mvc.Components.get("submitted");
         document.getElementById("status_date").valueAsDate = new Date();

         def_tok.set("tok_status_date",document.getElementById("status_date").value);
         sub_tok.set("tok_status_date",document.getElementById("status_date").value);

         $('#status_date').on("change", function (e){
                 def_tok.set("tok_status_date",document.getElementById("status_date").value);
                 sub_tok.set("tok_status_date",document.getElementById("status_date").value);
         });
 });

View solution in original post

andrewtrobec
Motivator

I've managed to solve the problem. I was only working with the "default" tokens when I should have also been using the "submitted" tokens. Here is the script that works:

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

         // initialization
         var def_tok = mvc.Components.get("default");
         var sub_tok = mvc.Components.get("submitted");
         document.getElementById("status_date").valueAsDate = new Date();

         def_tok.set("tok_status_date",document.getElementById("status_date").value);
         sub_tok.set("tok_status_date",document.getElementById("status_date").value);

         $('#status_date').on("change", function (e){
                 def_tok.set("tok_status_date",document.getElementById("status_date").value);
                 sub_tok.set("tok_status_date",document.getElementById("status_date").value);
         });
 });

jmartin_pro
Explorer

Thank you so much! You saved me. I could not find this answer anywhere else.

0 Karma

lbrtntq
Engager

thanks man!

Get Updates on the Splunk Community!

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...