Example: Events viewers using JavaScript for apps outside Splunk Web

This example uses JavaScript to display examples of each of the possible types of events viewer.

To use this code, save it as an HTML file in your own web site's directory. Be sure to update the splunkjs.config section for your own web app. The authenticate function redirects to a login_form.html example login page, which you can find here. For more, see Use SplunkJS Stack in your own web apps.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Events Viewers</title>

    <link rel="stylesheet" type="text/css" href="static/splunkjs/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="static/splunkjs/css/dashboard.css" />
</head>

<body>
    <div class="dashboard-body container-fluid main-section-body">
        <div class="row">
            <div class="dashboard-header">
                <h2>Events viewers</h2>
            </div>
        </div>

        <!-- Row for the list events viewer -->
        <div class="dashboard-row">
            <div class="dashboard-cell" style="width: 60%;">
                <div class="dashboard-panel">
                    <div class="dashboard-element">
                        <div class="panel-head">
                            <h3>List style</h3>
                        </div>
                        <div class="panel-body">
                            <div id="myeventsviewer-list"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Row for the table events viewer -->
        <div class="dashboard-row">
            <div class="dashboard-cell" style="width: 60%;">
                <div class="dashboard-panel">
                    <div class="dashboard-element">
                        <div class="panel-head">
                            <h3>Table style</h3>
                        </div>
                        <div class="panel-body">
                            <div id="myeventsviewer-table"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Row for the raw events viewer -->
        <div class="dashboard-row">
            <div class="dashboard-cell" style="width: 60%;">
                <div class="dashboard-panel">
                    <div class="dashboard-element">
                        <div class="panel-head">
                            <h3>Raw style</h3>
                        </div>
                        <div class="panel-body">
                            <div id="myeventsviewer-raw"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <script src="static/splunkjs/config.js"></script>

    <script>
        // Configure SplunkJS Stack
        splunkjs.config({
            proxyPath: "/proxy",
            scheme: "https",
            host: "localhost",
            port: 8089,
            authenticate: function(done) { 
                require([
                    "jquery",
                    "jquery.cookie"
                ], function($) {
                    // Retrieve the session key and username from cookies
                    var splunkSessionKey = $.cookie("splunk_sessionkey");
                    var splunkCurrentUser = $.cookie("splunk_username");

                    // Log in using the session key and username
                    if (splunkSessionKey) {
                        done(null, {sessionKey: splunkSessionKey, username: splunkCurrentUser}); 
                    }
                    // If there is no session key, redirect to the login form
                    else {
                        window.location.replace("login_form.html");
                    }
                });
            }
        });
        
        // Configure the web site's base URL
        require.config({
            baseUrl: "static/"
        });

        // Set up the Web Framework components
        var deps = [
            "splunkjs/ready!",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/eventsviewerview"
        ];
        require(deps, function(mvc) {
            // Load individual components
            var SearchManager = require("splunkjs/mvc/searchmanager");
            var EventsViewer = require("splunkjs/mvc/eventsviewerview");

            // Set up a search manager
            var search1 = new SearchManager({
                id: "example-search",
                search: "index=_internal | head 100",
                preview: true,
                cache: true
            });

            // Create 3 events viewers: list, table, and raw
            listviewer = new EventsViewer({
                id: "example-eventsviewer-list",
                managerid: "example-search",
                type: "list",
                "list.drilldown": "outer",
                drilldownRedirect: true,
                "list.wrap": true,
                count: 3,
                pagerPosition: "top",
                showPager: true,
                rowNumbers: false,
                el: $("#myeventsviewer-list")
            }).render();

            tableviewer = new EventsViewer({
                id: "example-eventsviewer-table",
                managerid: "example-search",
                type: "table",
                "table.drilldown": true,
                drilldownRedirect: true,
                "table.sortColumn": "sourcetype",
                "table.sortDirection": "asc",
                "table.wrap": true,
                count: 3,
                showPager: false,
                rowNumbers: false,
                el: $("#myeventsviewer-table")
            }).render();

            rawviewer = new EventsViewer({
                id: "example-eventsviewer-raw",
                managerid: "example-search",
                type: "raw",
                "raw.drilldown": "inner",
                drilldownRedirect: true,
                count: 3,
                pagerPosition: "top",
                showPager: true,
                rowNumbers: false,
                el: $("#myeventsviewer-raw")
            }).render();
        });
    </script>
</body>
</html>