Skip to content

Commit 3cfd8ce

Browse files
committed
add plugin and demo from CodePen
1 parent 2c87844 commit 3cfd8ce

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

demo/demo.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@import url(http://fonts.googleapis.com/css?family=Roboto:400,700);
2+
3+
.wrapper {
4+
font-family: Roboto;
5+
width: 900px;
6+
max-width: 100%;
7+
margin: auto;
8+
}
9+
10+
table {
11+
width: 100%;
12+
border-spacing: 0;
13+
}
14+
15+
table th {
16+
text-align: left;
17+
border-bottom: 1px solid black;
18+
}
19+
20+
table tbody tr:nth-child(even) {
21+
background-color: #DDD;
22+
}
23+
24+
table tbody td {
25+
padding: 2px 5px;
26+
}
27+
28+
table tbody td:first-of-type {
29+
font-weight: bold;
30+
}
31+
32+
@media only screen and (max-width: 40em) {
33+
table th {
34+
display: none;
35+
}
36+
table tbody td {
37+
display: block;
38+
}
39+
table tbody td:not(:first-of-type) {
40+
margin-left: 20px;
41+
}
42+
}

demo/demo.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$(document).ready(function () {
2+
3+
$.ajax({
4+
url: 'http://api.randomuser.me/?results=100&seed=foobar',
5+
dataType: 'json',
6+
success: function (data) {
7+
for ( var i = 0; i < data.results.length; i++ ) {
8+
//console.log(data.results[i].user.name.first);
9+
$('#example tbody').append("<tr></tr>");
10+
$('#example tbody tr:last-of-type')
11+
.append("<td>" + data.results[i].user.name.first + " " + data.results[i].user.name.last + "</td>")
12+
.append("<td>" + data.results[i].user.email + "</td>")
13+
.append("<td>" + data.results[i].user.cell + "</td>");
14+
}
15+
}
16+
});
17+
18+
$( "#example" ).searchable({
19+
placeholder: "Search Table",
20+
});
21+
});

demo/index.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
6+
<meta charset="UTF-8">
7+
8+
<title>Searchable Table | jquery.searchable.js</title>
9+
10+
<link rel="stylesheet" type="text/css" href="demo.css">
11+
12+
</head>
13+
14+
<body>
15+
16+
<div class="wrapper">
17+
<table id="example">
18+
<thead>
19+
<tr>
20+
<th>Name</th>
21+
<th>Email</th>
22+
<th>Cell</th>
23+
</tr>
24+
</thead>
25+
26+
<tbody>
27+
28+
</tbody>
29+
</table>
30+
</div>
31+
32+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
33+
<script src="../dist/jquery.searchable.js"></script>
34+
<script src="demo.js"></script>
35+
36+
</body>
37+
38+
</html>

dist/jquery.searchable.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* jQuery Searchable - v0.1.1
3+
* Search rows in a table.
4+
* http://kylebjohnson.me
5+
*
6+
* Made by Kyle B. Johnson
7+
* Under MIT License
8+
*/
9+
(function( $ ) {
10+
$.fn.searchable = function(options) {
11+
12+
var settings = $.extend({}, {
13+
search_class: '',
14+
search_placeholder: "search",
15+
}, options);
16+
17+
return this.each(function() {
18+
19+
var search_id = this.id + '_search';
20+
21+
jQuery('<input/>', {
22+
id: search_id,
23+
class: settings.search_class,
24+
placeholder: settings.placeholder,
25+
}).insertBefore( $(this) );
26+
27+
$( '#' + search_id ).keypress(function() {
28+
var search = $( this ).val();
29+
$('tbody tr').show();
30+
if(search) {
31+
$("tbody tr:not(:contains('" + search + "'))").hide();
32+
}
33+
});
34+
35+
});
36+
37+
};
38+
}( jQuery ));

dist/jquery.searchable.min.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* jQuery Searchable - v0.1.1
3+
* Search rows in a table.
4+
* http://kylebjohnson.me
5+
*
6+
* Made by Kyle B. Johnson
7+
* Under MIT License
8+
*/
9+
(function(e){e.fn.searchable=function(t){var n=e.extend({},{search_class:"",search_placeholder:"search"},t);return this.each(function(){var t=this.id+"_search";jQuery("<input/>",{id:t,"class":n.search_class,placeholder:n.placeholder}).insertBefore(e(this));e("#"+t).keypress(function(){var t=e(this).val();e("tbody tr").show();if(t){e("tbody tr:not(:contains('"+t+"'))").hide()}})})}})(jQuery)

0 commit comments

Comments
 (0)