Dashboards & Visualizations

Javascript - Web framework - how to pass a parameter to the on method ("click:row")

OL
Communicator

Hello, would anyone know if there is a way to pass a parameter to the on method in javascript?
I have tried:

var submittedTokenModel = mvc.Components.get("submitted");
console.log(submittedTokenModel);

table_view_obj.on("click:row", function(e, submittedTokenModel){
   console.log(submittedTokenModel);
});

But the return submittedTokenModel objects in the console are different (and the second one is not what I want)

0 Karma

jeffland
Champion

You don't need to pass submittedTokenModel, you can just do

var submittedTokenModel = mvc.Components.get("submitted");
table_view_obj.on("click:row", function(e){
    console.log(submittedTokenModel);
});

Your browsers js engine will try to find submittedTokenModel inside the local scope of the anonymous function. Since this function doesn't declare an object by that name, the engine will try to find it in the outer scope of the anonymous function, which in this case is the scope in which you declared both the anonymous function you assign to the click event of table_view_obj and submittedTokenModel.

If the values of submittedTokenModel are different between the initial console.log at the beginning of your code and when the event handler calls your anonymous function, then the token model has probably been changed by something. If you want to get notified whenever that happens, have something like this in your code:

submittedTokenModel.on("change", function(e) {
    console.log("submittedTokenModel changed: " + JSON.stringify(submittedTokenModel));
});
0 Karma

OL
Communicator

Hi @jeffland,
Thank you very much for your help on this. This is what I originally tried by I had a undef object when I was in the function.

0 Karma

niketnilay
Legend

@OL, First off, have you verified whether you are working with default token model or submitted? Do you have searchWhenChanged turned to false and submitButton set to true? If you indeed have these then ideally you should not get undefined. If you are coding click event handler for table view, Submitted token model will give you previously submitted value even though the value has changed.

You can also try the following search if you are seeing undefined error in JavaScript:

     var submittedTokenModel = mvc.Components.get("submitted");
     submittedTokenModel.on("change", function(e) {
         if(submittedTokenModel!==undefined){
             console.log("submittedTokenModel defined and changed: " + JSON.stringify(submittedTokenModel));
         }else{
             console.log("submittedTokenModel undefined");
         }
     });

If it is for specific token change for defaultTokenModel, for example <yourTokenName> you can add the following condition if(<yourTokenName>!==undefined){ ...}. Replace <yourTokenName> with the token name which you want to capture when it changes.

  var defaultTokenModel = mvc.Components.get("default");
 defaultTokenModel.on("change:<yourTokenName>", function(newTokenName, <yourTokenName>, options) 
 {
     if(<yourTokenName>!==undefined){
         console.log("<yourTokenName>: ",<yourTokenName>);
     }
 });
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

OL
Communicator

Hi @niketnilay,

I don't think the problem is with submittedTokenModel itself as when you use the console.log outside of the function, everything is working as expected. It is just when you call it from inside the function.

0 Karma

niketnilay
Legend

Can you share your current code? Or create a mock dashboard to recreate your issue?

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @QL,
Can you please share more information regarding what you want to pass on click?? Have you tried by removing submittedTokenModel a a parameter ? Can you please try below?

var submittedTokenModel = mvc.Components.get("submitted");
 console.log(submittedTokenModel);

 table_view_obj.on("click:row", function(e){
    console.log(submittedTokenModel);
 });
0 Karma

OL
Communicator

Hi Kamlesh_vaghela,
Thank you for your reply. I wasn't to keep the question generic as other people might have different need, but to be more specific about my case, here is the workflow:

  1. A user click on a row, which correspond to the line he/she wants to delete
  2. a modal pop-up window appear asking if the user is sure he/she wants to delete the line
  3. if confirmed, it runs the search that delete the corresponding line.

The dashboard is a but more complex and that is why I wanted to pass various parameters via the submittedTokenModel.

And I tried what you mentioned, and I get a undef object in the function. It seems to work well with the defaultTokenModel.

0 Karma
Get Updates on the Splunk Community!

Improve Your Security Posture

Watch NowImprove Your Security PostureCustomers are at the center of everything we do at Splunk and security ...

Maximize the Value from Microsoft Defender with Splunk

 Watch NowJoin Splunk and Sens Consulting for this Security Edition Tech TalkWho should attend:  Security ...

This Week's Community Digest - Splunk Community Happenings [6.27.22]

Get the latest news and updates from the Splunk Community here! News From Splunk Answers ✍️ Splunk Answers is ...