2015-08-03 38 views
0

我想使用Jquery突出显示正常html表中特定列中的特定数据字段。可以为此场景提供一段代码。我已经实现的是突出显示Solr的记录,但不知道如何将其应用于连续的确切列。使用Jquery突出显示表中特定的数据列

var newresult = {"_header": { 
     "nvPairs": [ 
        "status", 
        0, 
        "QTime", 
        3, 
        "params", 
        { 
         "nvPairs": [ 
          "hl.fragsize", 
          "100000", 
          "hl.simple.pre", 
          "<em>", 
          "hl.fl", 
          "name", 
          "wt", 
          "javabin", 
          "hl.maxAnalyzedChars", 
          "100000", 
          "hl", 
          "true", 
          "version", 
          "2", 
          "hl.highlightMultiTerm", 
          "true", 
          "hl.snippets", 
          "100", 
          "hl.useFastVectorHighlighter", 
          "true", 
          "q", 
          "harsha", 
          "hl.regex.slop", 
          "0.5", 
          "hl.regex.pattern", 
          "[-\\w ,/\n\\\"']*", 
          "hl.simple.post", 
          "</em>" 
         ] 
        } 
       ] 
      }, 
      "_results": [ 
       { 
        "id": "21", 
        "name": [ 
         "asd asda fsdfsdfds harsha sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds dsfsdf d f ds fdf df df dsfdsf dfdsf d  fdsdf dfs harsha" 
        ], 
        "_version_": 1508203924863058000 
       } 
      ], 
      "_highlightingInfo": { 
       "nvPairs": [ 
        "21", 
        { 
         "nvPairs": [ 
          "name", 
          [ 
           "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds dsfsdf d f ds fdf df df dsfdsf dfdsf d  fdsdf dfs <em>harsha</em>" 
          ] 
         ] 
        } 
       ] 
      }, 
      "_highlighting": { 
       "21": { 
        "name": [ 
         "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds dsfsdf d f ds fdf df df dsfdsf dfdsf d  fdsdf dfs <em>harsha</em>" 
        ] 
       } 
      }, 
      "elapsedTime": 19, 
      "response": { 
       "nvPairs": [ 
        "responseHeader", 
        { 
         "nvPairs": [ 
          "status", 
          0, 
          "QTime", 
          3, 
          "params", 
          { 
           "nvPairs": [ 
            "hl.fragsize", 
            "100000", 
            "hl.simple.pre", 
            "<em>", 
            "hl.fl", 
            "name", 
            "wt", 
            "javabin", 
            "hl.maxAnalyzedChars", 
            "100000", 
            "hl", 
            "true", 
            "version", 
            "2", 
            "hl.highlightMultiTerm", 
            "true", 
            "hl.snippets", 
            "100", 
            "hl.useFastVectorHighlighter", 
            "true", 
            "q", 
            "harsha", 
            "hl.regex.slop", 
            "0.5", 
            "hl.regex.pattern", 
            "[-\\w ,/\n\\\"']*", 
            "hl.simple.post", 
            "</em>" 
           ] 
          } 
         ] 
        }, 
        "response", 
        [ 
         { 
          "id": "21", 
          "name": [ 
           "asd asda fsdfsdfds harsha sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds dsfsdf d f ds fdf df df dsfdsf dfdsf d  fdsdf dfs harsha" 
          ], 
          "_version_": 1508203924863058000 
         } 
        ], 
        "highlighting", 
        { 
         "nvPairs": [ 
          "21", 
          { 
           "nvPairs": [ 
            "name", 
            [ 
             "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds dsfsdf d f ds fdf df df dsfdsf dfdsf d  fdsdf dfs <em>harsha</em>" 
            ] 
           ] 
          } 
         ] 
        } 
       ] 
      } 
     }; 

var highlight = {}; 

$.each(newresult._highlighting, function(i, hitem){ 
    var match = hitem.name[0].match(/<em>(.*?)<\/em>/); 
    // var match = hitem.description[0].match(/elegant/g); 
    console.log("match "+match); 
    highlight[i] = match[1]; 


}); 
var res1 = newresult._highlighting; 
var results; 
var result; 
for(var res2 in res1) 
{ 
    var hlight = res1[res2].name; 
    results= highlight[res2]; 

} 

$.each(hlight, function(ix, items){ 
    var word = results; 
    result = items.replace(new RegExp(word, 'g'),word); 

}); 

//var res = newresult.response.nvPairs[5].nvPairs[1].nvPairs[1]; 
//var word ="harsha"; 
//var result = res[0].replace(new RegExp(word, 'g'), '<em>harsha</em>'); 






    $("body").append('<div>' + result + '</div>'); 



}); 
+1

如果没有示例HTML,问题太模糊“特定列中的特定数据字段”没有描述您如何选择该数据或列。请显示你的尝试。 –

回答

1

你可以看看下面的链接。

Fiddle

$("#coa_history_data tbody tr.data-in-table").each(function() { 
    var $firstCell = $('td:eq(2)', this); 
    var $secondCell = $('td:eq(3)', this); 
    var $thirdCell = $('td:eq(4)', this); 
    var $fourthCell = $('td:eq(5)', this); 
if($firstCell.text() != $secondCell.text()){ 
    $firstCell.css('backgroundColor','yellow'); 
    $secondCell.css('backgroundColor','yellow'); 
} 
if($thirdCell.text() != $fourthCell.text()){ 
    $thirdCell.css('backgroundColor','orange'); 
    $fourthCell.css('backgroundColor','orange'); 
} 

});