Splunk Search

A Quick question about Application.js

ma_anand1984
Contributor

I have two SimpleResultsTable in my dashboard.
I want to apply some custom js for only one Table

How can i select the table i want. Is it possible to get Table id here ?

below is my preview of application.js


if (Splunk.util.getCurrentView() == "view1"  && Splunk.Module.SimpleResultsTable) 
{
  Splunk.Module.SimpleResultsTable = $.klass(Splunk.Module.SimpleResultsTable, 
  {
      onResultsRendered: function($super)
      {
        var retVal = $super();          
        this.myCustomHeatMapDecorator();
        return retVal;
      },        
      
      myCustomHeatMapDecorator: function () 
      {

1 Solution

sideview
SplunkTrust
SplunkTrust

If you need to pick out just one particular instance of a module in a particular view, you have to reference it by the module Id, and you have to do it in a completely different way.

When you use the klass function, you're in a weird way subclassing the module from itself, and then assigning the reference to the new class back to the variable that held the old class reference. It's marginally evil but very clean and it happens before any of the modules are initialized.

When you need to pick out just one particular module by id, it looks similar but it's actually very different. You have to do it after the modules are initialized, which makes it a trickier thing to do without doing harm (note there is no $super so we have to do it manually). Also because the module id's will often all change when the XML is edited later, it's more than a little brittle.

$(document).ready(function() {
    if (Splunk.util.getCurrentView() == "view1") {
        var module = Splunk.Globals["ModuleLoader"].getModuleInstanceById("SimpleResultsTable_1_2_0");

        var myCustomHeatMapDecorator= function () {
            alert('oh hai');
        }.bind(module)

        var methodReference = module.onResultsRendered.bind(module);

        module.onResultsRendered = function() {
            var retVal = methodReference();
            myCustomHeatMapDecorator();
            return retVal;
        }
    }
});

As a side note - If you're using Sideview Utils this brittleness is largely why the Sideview CustomBehavior concept was created. If you were to use the Sideview Table module instead of SimpleResultsTable, then you could define a customBehavior on one Table and not the Other and not have any brittle linkages by module id. Although quite possibly the custom rendering/embedding features of the Table module could implement your heatmap more easily without using custom code at all!

UPDATE: I fixed a couple mistakes in the sample code posted.

View solution in original post

sideview
SplunkTrust
SplunkTrust

If you need to pick out just one particular instance of a module in a particular view, you have to reference it by the module Id, and you have to do it in a completely different way.

When you use the klass function, you're in a weird way subclassing the module from itself, and then assigning the reference to the new class back to the variable that held the old class reference. It's marginally evil but very clean and it happens before any of the modules are initialized.

When you need to pick out just one particular module by id, it looks similar but it's actually very different. You have to do it after the modules are initialized, which makes it a trickier thing to do without doing harm (note there is no $super so we have to do it manually). Also because the module id's will often all change when the XML is edited later, it's more than a little brittle.

$(document).ready(function() {
    if (Splunk.util.getCurrentView() == "view1") {
        var module = Splunk.Globals["ModuleLoader"].getModuleInstanceById("SimpleResultsTable_1_2_0");

        var myCustomHeatMapDecorator= function () {
            alert('oh hai');
        }.bind(module)

        var methodReference = module.onResultsRendered.bind(module);

        module.onResultsRendered = function() {
            var retVal = methodReference();
            myCustomHeatMapDecorator();
            return retVal;
        }
    }
});

As a side note - If you're using Sideview Utils this brittleness is largely why the Sideview CustomBehavior concept was created. If you were to use the Sideview Table module instead of SimpleResultsTable, then you could define a customBehavior on one Table and not the Other and not have any brittle linkages by module id. Although quite possibly the custom rendering/embedding features of the Table module could implement your heatmap more easily without using custom code at all!

UPDATE: I fixed a couple mistakes in the sample code posted.

sideview
SplunkTrust
SplunkTrust

No, it should be fine and I just tested. The code does have some very very subtle differences as to when it executes relative to application.js, but it wont make a difference here. I suspect that you've just dropped the document.ready() part? indeed before document.ready fires, there is no Splunk.Globals["ModuleLoader"].

Also, this question and answer is from a long time ago! Nowadays you should update to the latest Sideview Utils, use the Table module, attach a customBehavior to your one Table. Table module then has an empty template method called onResultsRendered. Way easier.

0 Karma

shuaiscott
New Member

Does this code have to be placed in application.js?

I'm trying to run some custom js on a table too. I'm running it on the SideviewUtils module as customJavascript. When it runs the js, I get an error on the console saying: "Uncaught TypeError: Cannot call method 'getModuleInstanceById' of undefined".

Is that due to it not running on application.js?

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

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