<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to set class for different fields using JavaScript for changing font color and background color in Splunk table? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-set-class-for-different-fields-using-JavaScript-for/m-p/486428#M193689</link>
    <description>&lt;P&gt;@812456 while posting code on Splunk Answers use the Code button ( &lt;CODE&gt;101010&lt;/CODE&gt; ), or shortcut &lt;CODE&gt;Ctrl+K&lt;/CODE&gt; to ensure that special characters do not escape.&lt;/P&gt;

&lt;P&gt;I think the better approach for you would be to use the Table Search Manager to get the list of fields you want to iterate and pass to the Cell Render extension as a string array. Refer to one of my older answers, see if it fits your needs: &lt;A href="https://answers.splunk.com/answers/618930/how-can-i-get-the-table-cell-colorization-renderin-1.html"&gt;https://answers.splunk.com/answers/618930/how-can-i-get-the-table-cell-colorization-renderin-1.html&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 05 May 2020 12:42:55 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2020-05-05T12:42:55Z</dc:date>
    <item>
      <title>How to set class for different fields using JavaScript for changing font color and background color in Splunk table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-set-class-for-different-fields-using-JavaScript-for/m-p/486427#M193688</link>
      <description>&lt;P&gt;Hi i am new to Splunk/JavaScript, Need your help for reducing my code, i have created two class for 2 fields, likewise we  have 50 fields in splunk table. class is created for changing  font and background color based on condition. &lt;BR /&gt;
With this current approach i need to add/repeat 50 times (i.e.) for all the fields. Each time i need to add/call &lt;STRONG&gt;setTimeout(function()&lt;/STRONG&gt; how can i customize the redundancy of the code for creating class and calling the setTimeout function for single time in javascript. &lt;BR /&gt;
kindly find my code&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;enter code here&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;table.js&lt;/P&gt;

&lt;P&gt;require([&lt;BR /&gt;
     'underscore',&lt;BR /&gt;
     'jquery',&lt;BR /&gt;
     'splunkjs/mvc',&lt;BR /&gt;
     'splunkjs/mvc/tableview',&lt;BR /&gt;
     'splunkjs/mvc/simplexml/ready!'&lt;BR /&gt;
 ], &lt;BR /&gt;
function(_, $, mvc, TableView) {&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  // Row Coloring Example with custom, client-side range interpretation

 var CustomRangeRenderer = TableView.BaseCellRenderer.extend({
     canRender: function(cell) {
         // Enable this custom cell renderer for both the active_hist_searches and the active_realtime_searches field
         //return _(["LOgType"]).contains(cell.field);
          return true;
     },
     render: function($td, cell) {
         // Add a class to the cell based on the returned value

         var value = cell.value;

         // Apply interpretation for string of autorización
         if (cell.field === "LOgType") {
             if (value == "error") {
                 $td.addClass('range-cell').addClass('range-severe');
             }
         }
         if (cell.field === "ID") {
             if(value!==null) {
                 $td.addClass('range-con').addClass('range-low');
             }
 if (cell.field === "Desc") {
             if(value!==null) {
                 $td.addClass('range-cos').addClass('range-high');
             }
         }

         // Update the cell content
         $td.text(value).addClass("string");
     }
 });

 mvc.Components.get('Table1').getVisualization(function(tableView) {
     tableView.on('rendered', function() {

         setTimeout(function(){
         // Apply class of the cells to the parent row in order to color the whole row
         tableView.$el.find('td.range-cell').each(function() {
             $(this).parents('tr').addClass(this.className);
         });
      },100);
   setTimeout(function(){
         tableView.$el.find('td.range-con').each(function() {
             $(this).parents('tr').addClass(this.className);
         });
      },100);
     });
    setTimeout(function(){
         tableView.$el.find('td.range-cos').each(function() {
             $(this).parents('tr').addClass(this.className);
         });
      },100);
     });
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;`tableView.addCellRenderer(new CustomRangeRenderer()); // Add custom cell renderer, the table will re-render automatically.&lt;BR /&gt;
      }); &lt;BR /&gt;
 });&lt;/P&gt;

&lt;P&gt;table.css&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;         #Table1 tr.range-severe td{
            color: red;             
            }
            #Table1 tr td.range-low{
            background-color: #FFC597 !important;            
            }
           #Table1 tr td.range-high{
            background-color: #FFC597 !important;            
            }
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 May 2020 15:55:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-set-class-for-different-fields-using-JavaScript-for/m-p/486427#M193688</guid>
      <dc:creator>812456</dc:creator>
      <dc:date>2020-05-04T15:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to set class for different fields using JavaScript for changing font color and background color in Splunk table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-set-class-for-different-fields-using-JavaScript-for/m-p/486428#M193689</link>
      <description>&lt;P&gt;@812456 while posting code on Splunk Answers use the Code button ( &lt;CODE&gt;101010&lt;/CODE&gt; ), or shortcut &lt;CODE&gt;Ctrl+K&lt;/CODE&gt; to ensure that special characters do not escape.&lt;/P&gt;

&lt;P&gt;I think the better approach for you would be to use the Table Search Manager to get the list of fields you want to iterate and pass to the Cell Render extension as a string array. Refer to one of my older answers, see if it fits your needs: &lt;A href="https://answers.splunk.com/answers/618930/how-can-i-get-the-table-cell-colorization-renderin-1.html"&gt;https://answers.splunk.com/answers/618930/how-can-i-get-the-table-cell-colorization-renderin-1.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 12:42:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-set-class-for-different-fields-using-JavaScript-for/m-p/486428#M193689</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2020-05-05T12:42:55Z</dc:date>
    </item>
  </channel>
</rss>

