Dashboards & Visualizations

Please fix my script

SN1
Path Finder

require([
    'splunkjs/mvc',
    'splunkjs/mvc/searchmanager',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!',
    'jquery'
], function(mvc, SearchManager, TableView, ignored, $) {

 

    // Define a simple cell renderer with a button
    var ActionButtonRenderer = TableView.BaseCellRenderer.extend({
        canRender: function(cell) {
            return cell.field === 'rowKey'; 
        },
        render: function($td, cell) {
            $td.addClass('button-cell');

 

      
      var rowKey = cell.value
            var $btn = $('<button class="btn btn-success">Mark Solved</button>');

 

            $btn.on('click', function(e) {
                e.preventDefault();
                e.stopPropagation();

 

                var searchQuery = `| inputlookup sbc_warning.csv
                    | eval rowKey=tostring(rowKey)
                    | eval solved=if(rowKey="${rowKey}", "1", solved)
                    | outputlookup sbc_warning.csv`;

 

                var writeSearch = new SearchManager({
                    id: "writeSearch_" + Math.floor(Math.random() * 100000),
                    search: searchQuery,
                    autostart: true
                });

 

                writeSearch.on('search:done', function() {
                    console.log("Search completed and lookup updated");
                    var panelSearch = mvc.Components.get('panel_search_id');
                    if (panelSearch) {
                        panelSearch.startSearch();
                        console.log("Panel search restarted");
                    }
                });
            });

 

            $td.append($btn);
        }
    });

 

    // Apply the renderer to the specified table
    var tableComponent = mvc.Components.get('sbc_warning_table');
    if (tableComponent) {
        tableComponent.getVisualization(function(tableView) {
            tableView.table.addCellRenderer(new ActionButtonRenderer());
            tableView.table.render();
        });
    }
});


in this i want name of the button to be "unsolved" initially and when somebody clicks it the name should change to solved

Labels (2)
0 Karma

livehybrid
SplunkTrust
SplunkTrust

Hi @SN1 

Try the following:

require([
    'splunkjs/mvc',
    'splunkjs/mvc/searchmanager',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!',
    'jquery'
], function(mvc, SearchManager, TableView, ignored, $) {
    // Define a simple cell renderer with a button
    var ActionButtonRenderer = TableView.BaseCellRenderer.extend({
        canRender: function(cell) {
            return cell.field === 'rowKey';
        },
        render: function($td, cell) {
    $td.addClass('button-cell');
    var rowKey = cell.value;
    var $btn = $('<button>').text('Unsolved');
    $btn.on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var searchQuery = `| inputlookup sbc_warning.csv | eval rowKey=tostring(rowKey) | eval solved=if(rowKey="${rowKey}", "1", solved) | outputlookup sbc_warning.csv`;
        var writeSearch = new SearchManager({
            id: "writeSearch_" + Math.floor(Math.random() * 100000),
            search: searchQuery,
            autostart: true
        });
        writeSearch.on('search:done', function() {
            $btn.text('Solved');
            var panelSearch = mvc.Components.get('panel_search_id');
            if (panelSearch) {
                panelSearch.startSearch();
            }
        });
    });
    $td.append($btn);
    }
    });

    // Apply the renderer to the specified table
    var tableComponent = mvc.Components.get('sbc_warning_table');
    if (tableComponent) {
        tableComponent.getVisualization(function(tableView) {
            tableView.table.addCellRenderer(new ActionButtonRenderer());
            tableView.table.render();
        });
    }
});

The button is created with $('<button>').text('Unsolved').

When clicked, after lookup update (search:done), the button label is changed using $btn.text('Solved').

This only changes text for that row's button; repeat clicks won't revert.

Note: If you want the initial state ("Unsolved"/"Solved") to reflect actual data, you must pass the current "solved" value for each row and set the initial button text accordingly.
Do you have a field for if its already Solved or not that you could use to set the initial button text?
 

🌟 Did this answer help you? If so, please consider:

  • Adding karma to show it was useful
  • Marking it as the solution if it resolved your issue
  • Commenting if you need any clarification

Your feedback encourages the volunteers in this community to continue contributing

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

ATTENTION: We’re Moving! (AGAIN!)

The Splunk Community Slack is undergoing a system migration to keep our workspace secure and ...

Deep Dive: Optimizing Telemetry Pipelines in Splunk Observability Cloud

In this session, we will peel back the layers of Splunk Observability Cloud’s cost-optimization features. ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...