Dashboards & Visualizations

HTML Dashboard: How to use Javascript to change text input?

benjaminlin1019
Explorer

I have trouble in using JS to change my text input from a comma list to convert search to a list of " OR ...OR" statement.
So if my input text is "1,2,3,4", then my query after converting should look like: sourcetype=mydata id=1 OR id=2 OR id=3 OR id=4 | .....;
However, when I inspect the query in search box, it is showing:
sourcetype=mydata id=1,2,3,4. Does anyone has anyidea? Following is my code
var ids = new String("$myTextValue$");
ids = ids.replace( " ","" );
ids = ids.split( "," );

    var ids_str = [];
    for( var i = 0; i < ids.length; i++ )
    {
      ids_str.push( "id=" + ids[i] );
    }

    var newComputedValue = ids_str.join( " OR " );

    var search1qry = "sourcetype=mydata " + newComputedValue ;

    var search1 = new SearchManager({
        "id": "search1",
        "earliest_time": "$earliest$",
        "cancelOnUnload": true,
        "search": search1qry,
        "status_buckets": 0,
        "latest_time": "$latest$",
        "app": utils.getCurrentApp(),
        "auto_cancel": 90,
        "preview": true
    }, {tokens: true, tokenNamespace: "submitted"});

gdiogo
Explorer

In this context,I don't see the point of using javascript...
Indeed I guess you could transform the token value simply from you Simple XML dashboard code !
You just need to add :

replace("$myTextValue$", ",", " OR ")
inside your input ...

However maybe I didn't get your context properly ...

0 Karma

piUek
Path Finder

The problem with this code is in this part:

var ids = new String("$myTextValue$");

When i do :
var ids = new String("1,2,3,4");
then i get an object which is not string. It's because I'm doing a string object out of a string object which is wrong approach - http://stackoverflow.com/questions/5750656/whats-the-point-of-new-stringx-in-javascript for reference.

So instead of doing this try just:
var ids = $myTextValue$;

0 Karma

paulstark
Path Finder

have you solved this issue? i and trying to do the same thing. thanks in advance!

0 Karma
Get Updates on the Splunk Community!

SplunkTrust Application Period is Officially OPEN!

It's that time, folks! The application/nomination period for the 2025 SplunkTrust is officially open! If you ...

Splunk Answers Content Calendar, June Edition II

Get ready to dive into Splunk Dashboard panels this week! We'll be tackling common questions around ...

Splunk Observability Cloud's AI Assistant in Action Series: Auditing Compliance and ...

This is the third post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how to ...