Splunk-具有XML的Javascript

问题描述 投票:1回答:1

我已经使用xml编码开发了仪表板。在XML中,我已经引用了javascript。

XML:我将字段中输入的单词作为Highlighttoken传递。

Javascript:我正在获取单词并在XML引用的所有表中进行搜索。我可以搜索单个单词。但我想用逗号分隔的多个单词字符串进行搜索。请找到以下代码,并帮助我使用javascript。

示例:如果我将关键字搜索为“ Splunk”,它将突出显示,但是如果我搜索为“ Splunk,web,access”,则将不显示任何内容。

XML:

<form script="highlightToken.js">
  <label>Highlight_text</label>
  <fieldset>
    <input type="time" token="field1">
      <label></label>
      <default>
        <earliest>-5m@m</earliest>
        <latest>now</latest>
      </default>
    </input>
    <input type="text" token="highlightToken" searchWhenChanged="true">
      <label>Search Word</label>
      <default>splunk*</default>
      <initialValue>searchword</initialValue>
    </input>
  </fieldset>
  <row>
    <panel>
      <title>$highlightToken$</title>
      <table id="highlightTable1">
        <search id="highlightSearch1">
          <query>index=_internal
| stats count by sourcetype</query>

          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>

        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
    <panel>
      <table id="highlightTable2">
        <search id="highlightSearch2">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
  <row>
    <panel>
      <table id="highlightTable3">
        <search id="highlightSearch3">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
    <panel>
      <table id="highlightTable4">
        <search id="highlightSearch4">
          <query>index=_internal sourcetype="splunkd"
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
  <row>
    <panel>
      <table id="highlightTable5">
        <search id="highlightSearch5">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
</form>

Javascript |:

require([
    "underscore",
    "jquery",
    "splunkjs/mvc",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/tableview",
    "splunkjs/mvc/simplexml/ready!"
], function (_, $, mvc, SearchManager, TableView) {
    var defaultTokenModel = mvc.Components.get("default");
    // var strHighlightText = ""
    var strHighlightText = defaultTokenModel.get("highlightToken");
    var customCellRenderer = TableView.BaseCellRenderer.extend({
        canRender: function (cell) {
            return cell.field !== "_time";
        },
        render: function ($td, cell) {
            var strText = cell.value;
            if (strHighlightText !== "*" && strHighlightText !== "") {
                var regEx = new RegExp(strHighlightText, "gi");
                strText = strText.replace(regEx, '<b style="background:#4dff4d;">$&</b>');
            }
            $td.addClass('string').html(_.template(strText));
        }
    });
    function highlightTable() {
        var maxCount = 6;
        for (i = 0; i < maxCount; i++) {
            var tableItem = mvc.Components.get("highlightTable" + i);
            if (typeof (strHighlightText) !== "undefined" && typeof (tableItem) !== "undefined") {
                var search = mvc.Components.get("highlightSearch" + i);
                if (search !== undefined) {
                    console.log("highlightSearch:", i)
                    search.startSearch();
                }
                console.log("highlightToken:", strHighlightText, " tableId:", i)
                tableItem.getVisualization(function (tableView) {
                    tableView.addCellRenderer(new customCellRenderer());
                });
            }
        }
    }
    defaultTokenModel.on("change:highlightToken", function (model, value, options) {
        if (typeof (value) !== "undefined" || value !== "$value$") {
            strHighlightText = value;
            highlightTable();
        }
    });
    highlightTable();
});
javascript xml splunk
1个回答
0
投票

您正在JavaScript中使用正则表达式来匹配突出显示的文本,因此可以将(Splunk)|(web)|(access)放在多个短语中。

或者,如果您真的想用逗号分隔,则需要修改以下代码部分。您需要先在逗号处分割strHighlightText,然后使用每个分割词循环以下代码]

var regEx = new RegExp(strHighlightText, "gi");
strText = strText.replace(regEx, '<b style="background:#4dff4d;">$&</b>');
© www.soinside.com 2019 - 2024. All rights reserved.