-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tablesearch.js
79 lines (66 loc) · 2.18 KB
/
jquery.tablesearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// project: tableSearch
// author: Soone
// date: 190113
//
// description: Search into a table and display results taht match the query
// jquery version:1.8
//
jQuery(function(){
$.fn.tablesearch = function(args){
args = $.extend({
color: "black",
bgcolor: "yellow"
}, args);
// place your code here
var tables = this;
//on submit
$("#formTS").on("submit",function(){
//delete yellow hightlight
$(".yellowTS").each(function(){
var tx = $(this).text();
$(this).replaceWith(tx);
});
//get query
var query = $("#queryTS").val();
//for each tables
tables.each(function(){
var td = $(this).find('td');
td.parent().hide(); //hide all table line tr
//for each line
td.each(function(){
var tx = $(this).text(); //get line content
var pattern = new RegExp(query,"gi");
var matches = pattern.exec(tx); //search
if(matches){ //if some content match replace by a hightlighted span
var newtx = tx.replace(matches[0],'<span class="yellowTS" style="color:'+args.color+';background-color:'+args.bgcolor+'">'+matches[0]+'</span>');
if($(this).children().is('a')){
$(this).children('a').html(newtx);
}
else {
$(this).html(newtx);
}
//display line that match
$(this).parent().show();
}
});
});
return false;
});
$("#clearTS").on('click',function(){
//display all lines of all tables
tables.each(function(){
var tr = $(this).find('td').parent();
tr.each(function(){
$(this).show();
});
});
//remove hightlights
$('.yellowTS').each(function(){
var tx = $(this).text();
$(this).replaceWith(tx);
});
});
// eoc
return this;
};
});