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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...