Search code examples
javascriptxmlvisualizationsplunksplunk-query

How to pass or set token when click on image in html panel in splunk?


I want to set or pass value to the token , after clicking on image in HTML panel

Here is my panel code:

<form script="my.js">
<row>
<panel>
<html>
<a id="mydivId">
<img src="/static/app/My_app/bck_city.png"/>
</a>
</html>
</panel>
</row>
</form>

And after clicking on the image of above panel the below token should set with value (I want to set token="mytoken", with some value after clicking on the image above)

<row>
<panel>
<title>mypanel</title>
<event>
<search>
<query>|makeresults
|eval result=$mytoken$
|table result</query>
<earliest>-15m</earliest>
<latest>now</latest>
</search>
<option name="list.drilldown">none</option>
<option name="refresh.display">progressbar</option>
</event>
</panel>
</row>

I also tried setting token value by .js

 require(['underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/utils',
'splunkjs/mvc/tokenutils',
'splunkjs/mvc/simplexml/ready!'], function ($) {
    var utils = require("splunkjs/mvc/utils");
    $(document).ready(function () {
    $("#mydivId").on("click", function (){
    var tokens = mvc.Components.get("default");
    var tokenValue = tokens.get("mytoken");
    tokens.set("mytoken", "cheese");
        });
    });
});

enter image description here


Solution

  • I have published an example for you here:

    https://github.com/bshuler/splunk_app_for_stackoverflow_questions

    but the basics are:

    view:

    <dashboard script="set_token.js">
      <row>
        <panel>
          <title>mytoken = $mytoken$</title>
          <html>
            <a id="mydivId">
            <img src="/static/app/stackoverflow/set_token.jpg"/>
            </a>
          </html>
        </panel>
      </row>
      <row>
        <panel>
          <table>
            <search id="search1">
              <query>| makeresults | eval my_value="$mytoken$"</query>
              <earliest>-15m</earliest>
              <latest>now</latest>
            </search>
            <option name="drilldown">none</option>
          </table>
        </panel>
      </row>
    </dashboard>
    

    and javascript set_token.js

    require([
        "splunkjs/mvc",
        "splunkjs/mvc/simplexml/ready!"
    ], function(mvc) {
    
        // Get your div
        var my_div = $("#mydivId");
    
        // Respond to clicks
        my_div.on("click", function(e) {
            var tokens = mvc.Components.get("submitted");
            tokens.set("mytoken", "cheese");
        });
    
    });