2016-01-14 30 views
0

我正在与DataTables plugin一起工作,我在表格的每一列内搜索了多个搜索框。Javascript,Datatables,jmhighlight和多个搜索

然后我应用jmHighlight plugin来获得搜索结果的突出显示。

现在我会从你为了获得如何修改jmHighlight代码的建议:

  • 不同的跨度和颜色的亮点;
  • 并获得突出显示的单词不会在没有突出显示的情况下恢复正常。

当我打电话jmhighlight()功能我使用:

function hlCountry (e) { //"e" is the number of col activated for search 
    var $keyword = $('#col'+e+'_filter').val(); //this recover word inserted 
    console.log($keyword); //this is just for test 

    $("#example tbody #Col"+e+"_tab").jmRemoveHighlight(); //here I call jmRemoveHighlight function restricting search in specify col of table 
    $("#example tbody #Col"+e+"_tab").jmHighlight($keyword); //here I call jmHighlight function restricting search in specify col of table 
} 

我试图寻找jmRemoveHighlight()jmHighlight()内(包含页面文件)jquery.jmHighlight.min.js。 遵循代码:

/*!*************************************************** 
    * jmHighlight 
    * Version 3.1.0 
    * Copyright (c) 2014–2016, Julian Motz 
    *****************************************************/ 
    ! function(a, b) { 
    "use strict"; 
    "function" == typeof define && define.amd ? define(["jquery"], function(a, c) { 
    return b(a, c) 
    }) : "object" == typeof exports ? b(require("jquery"), a) : b(a.jQuery, a) 
    }(this, function(a, b) { 
    "use strict"; 

function c(a, c, e) { 
    return this.options = d.extend({}, { 
     debug: !1, 
     log: b.console, 
     element: "span", 
     // className: "highlight2", 
     className: "highlight", 
     filter: [], 
     separateWordSearch: !1, 
     diacritics: !0, 
     synonyms: {} 
    }, e), "object" != typeof this.options.log && (this.options.log = { 
     debug: function() {} 
    }), this.keyword = "string" == typeof c ? c : "", this.$elements = d(), a instanceof d && a.length > 0 && (this.$elements = a.add(a.find("*")), this.$elements = this.getFilteredElements()), this 
} 
var d = a; 
c.prototype.diacritics = ["aÀÁÂÃÄÅàáâãäåĀāąĄ", "cÇçćĆčČ", "dđĐďĎ", "eÈÉÊËèéêëěĚĒēęĘ", "iÌÍÎÏìíîïĪī", "lłŁ", "nÑñňŇńŃ", "oÒÓÔÕÕÖØòóôõöøŌō", "rřŘ", "sŠšśŚ", "tťŤ", "uÙÚÛÜùúûüůŮŪū", "yŸÿýÝ", "zŽžżŻźŹ"], c.prototype.getFilteredElements = function() { 
    var a = this.$elements, 
     b = this.options.filter; 
    return "object" != typeof b || a instanceof d == !1 || "[object Array]" !== Object.prototype.toString.call(b) ? a : a = a.filter(function() { 
     for (var a = d(this), c = !1, e = 0, f = b.length; f > e; e++) { 
      var g = b[e]; 
      a.is(g) && (c = !0) 
     } 
     return c ? !1 : !0 
    }) 
}, c.prototype.getTextNodes = function(a) { 
    var b = []; 
    return a instanceof d == !1 || 0 === a.length ? b : (a.each(function() { 
     var a = d(this), 
      c = a.contents().filter(function() { 
       return 3 === this.nodeType ? !0 : !1 
      }); 
     c.each(function() { 
      b.push(this) 
     }) 
    }), b) 
}, c.prototype.getKeywordRegexp = function(a) { 
    var b = "string" != typeof a ? this.keyword : a; 
    if ("string" != typeof b || "" === b) return b; 
    var c = b; 
    return d.isEmptyObject(this.options.synonyms) || (c = this.getSynonymsRegex(c)), this.options.diacritics && (c = this.getDiacriticRegex(c)), c 
}, c.prototype.getSynonymsRegex = function(a) { 
    var b = a; 
    return "string" != typeof b ? b : (d.each(this.options.synonyms, function(a, c) { 
     var d = a, 
      e = c; 
     b = b.replace(new RegExp("(" + d + "|" + e + ")", "gmi"), "(" + d + "|" + e + ")") 
    }), b) 
}, c.prototype.getDiacriticRegex = function(a) { 
    var b = a; 
    if ("string" != typeof b) return b; 
    for (var c = b.split(""), d = [], e = 0, f = c.length; f > e; e++) 
     for (var g = c[e], h = 0, i = this.diacritics.length; i > h; h++) { 
      var j = this.diacritics[h]; 
      if (-1 !== j.indexOf(g)) { 
       if (d.indexOf(j) > -1) continue; 
       b = b.replace(new RegExp("[" + j + "]", "gmi"), "[" + j + "]"), d.push(j) 
      } 
     } 
    return b 
}, c.prototype.highlight = function(a) { 
    var b = "string" != typeof a ? this.keyword : a; 
    if ("" === b) return !0; 
    if (0 === this.$elements.length) return this.options.debug && this.options.log.debug("No search context provided"), !1; 
    this.$elements = this.$elements.filter("*:not([data-jmHighlight])"); 
    var c = this.options.separateWordSearch; 
    if ("boolean" == typeof c && c) { 
     var d = b.split(" "); 
     if (d.length > 1) { 
      this.options.debug && this.options.log.debug("Highlighting keywords separately"); 
      for (var e = 0, f = d.length; f > e; e++) 
       if (!this.highlight(d[e])) return !1; 
      return !0 
     } 
    } 
    this.options.debug && (this.options.log.debug("Highlighting keyword '" + b + "' in elements:"), this.options.log.debug(this.$elements)); 
    for (var g = this.getKeywordRegexp(b), h = new RegExp(g, "mi"), i = this.getTextNodes(this.$elements), j = 0, k = i.length; k > j; j++) { 
     var l = i[j]; 
     if ("object" == typeof l && "string" == typeof l.nodeValue && "" !== l.nodeValue.trim() && h.test(l.nodeValue) !== !1 && (this.options.debug && this.options.log.debug("Regex: '" + g + "'. Node value: '" + l.nodeValue + "'"), null != l.parentNode)) { 
      var m = new RegExp("((?![^<]*>)" + g + ")", "gim"); 
      l.parentNode.innerHTML = l.parentNode.innerHTML.replace(m, this.getHighlightTag("$1")) 
     } 
    } 
    return !0 
}, c.prototype.getHighlightTag = function(a) { 
    var b = "<" + this.options.element + " class='" + this.options.className + "' data-jmHighlight='true'>", 
     c = "</" + this.options.element + ">"; 
    return "string" != typeof a && (a = ""), b + a + c 
}, c.prototype.removeHighlight = function() { 
    if (0 === this.$elements.length) return this.options.debug && this.options.log.debug("No search context provided"), !1; 
    this.options.debug && ("string" == typeof this.keyword && "" !== this.keyword ? this.options.log.debug("Removing highlighting with keyword: '" + this.keyword + "'") : this.options.log.debug("Removing highlighting")); 
    var a = new RegExp(this.getKeywordRegexp(), "mi"), 
     b = this.options.element + "[data-jmHighlight]." + this.options.className, 
     c = this, 
     e = this.$elements.filter(b); 
    return this.options.debug && (this.options.log.debug(b), this.options.log.debug(e)), e.each(function() { 
     var b = d(this); 
     return a.test(b.text()) ? void c.appendTextNodes(b, e) : !0 
    }), !0 
}, c.prototype.appendTextNodes = function(a, b) { 
    if (a instanceof d == !1 || 0 === a.length) return !1; 
    var c = a.first()[0], 
     e = c.previousSibling, 
     f = c.nextSibling, 
     g = d(e), 
     h = d(f), 
     i = !1; 
    return g.length > 0 && (3 === e.nodeType ? (e.nodeValue = e.nodeValue + a.text(), a.remove()) : b instanceof d && b.length > 0 && b.is(g) ? (g.replaceWith(a.text() + g.text()), a.remove()) : a.replaceWith(a.text()), i = !0), h.length > 0 && (3 === f.nodeType ? 0 === g.length ? (f.nodeValue = a.text() + f.nodeValue, a.remove()) : (f.nodeValue = g.text() + f.nodeValue, g.remove()) : b instanceof d && b.length > 0 && b.is(h) ? 0 === g.length ? (h.replaceWith(h.text() + a.text()), a.remove()) : (h.replaceWith(g.text() + h.text()), g.remove()) : 0 === g.length && a.replaceWith(a.text()), i = !0), i === !1 && a.replaceWith(a.text()), !0 
}, d.fn.jmHighlight = function(a, b) { 
    var e = new c(d(this), a, b); 
    return e.highlight() 
}, d.fn.jmRemoveHighlight = function(a, b) { 
    var e = new c(d(this), b, a); 
    return e.removeHighlight() 
} 
}); 

此代码是非常复杂的给我。我想我会发现jmRemoveHighlight()jmHighlight()内部代码,但没有。

当然,我应该在<span><class name>上工作,但我不知道具体如何。

编写的插件开发:

您还可以突出显示与上下文中的不同类别的多个关键字/短语。只需为每个关键字克隆下面的代码即可。在选项对象中传递一个数组filter。过滤器数组应该包含所有应该被忽略的选择器。

但我无法做到这一点。请给我看看为了解决这个问题而采取的方向?

回答