-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.listFilter.js
More file actions
112 lines (95 loc) · 2.81 KB
/
jquery.listFilter.js
File metadata and controls
112 lines (95 loc) · 2.81 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* JQuery ListFilter plugin
*
* Filters a HTML list (defined as <element data-listfilter-list="ul#some-list">)
*
* Usage:
* $('form').listFilter();
*
* The list items can be filtered by an attribute:
* <element data-listfilter-by-attribute="true"> ("data-listfilter-index" as default)
* <element data-listfilter-by-attribute="data-title">
*
* Note: index value in data attribute should be lowecased.
*
* or by children element:
* <element data-listfilter-by-element="h3:contains({{value}})">
*
* Note: by-attribute method is much faster than byElement, because of CSS3 native selector usage
*
*
* @version 0.2
* @since 2014-01
* @author Mateusz Janik
*
*/
(function(global){
var ns = 'listfilter';
/**
* JQuery plugin
*/
(function($){
var pluginName = 'listFilter';
$.fn[pluginName] = function(o) {
var options = $.extend({}, $.fn[pluginName].defaults, o);
this.each(function () {
var $el = $(this),
$input = $el.find('input[type=search]'),
$submit = $el.find('button'),
$list = $($el.data(ns + 'List')),
$items = $list.children().addClass('listfilter-item'),
listItem = $el.data(ns + 'ListItem') || '',
byElement = $el.data(ns + 'ByElement'),
byAttribute = $el.data(ns + 'ByAttribute')
;
if (byAttribute) {
var listID = $list[0].id;
// List id must be specified
if (!listID) {
return false
}
var $searchStyle = $('<style type="text/css" id="listfilter-style-' + (Math.round(new Date().getTime() + (Math.random() * 100))) + '"></style>')
.appendTo('head');
// Use default (data-listfilter-list) attribute if not specified
byAttribute = typeof byAttribute === 'string' ? byAttribute : ('data-' + ns + '-index');
}
if ($submit.length) {
$submit.on('click', function(){
_search($input)
});
} else {
$input.on(options.eventType, function(){
_search($input)
}).on('search', function () {
if ($input.val() === '') {
_search($input);
}
});
}
function _search($inp){
if (byElement) {
if ($inp.val() === '') {
$items.removeClass(ns + '-item--hidden');
} else {
var selector = byElement.replace('{{value}}', $inp.val());
var $found = $items.removeClass(ns + '-item--hidden').filter(function(){
return $(this).find(selector).length > 0
});
$items.not($found).addClass(ns + '-item--hidden');
}
} else if (byAttribute) {
if ($inp.val() === '') {
$searchStyle.html('');
} else {
$searchStyle.html('#' + listID + " > " + listItem + ":not([" + byAttribute + "*=\"" + $inp.val().toLowerCase() + "\"]) { display: none; }");
}
}
}
});
return this;
}
$.fn[pluginName].defaults = {
eventType: 'keyup'
}
})(jQuery);
})(window);