2(I Can't Get No) SatisfactionThe Rolling Stones1965
39 |
3ImagineJohn Lennon1971
40 |
4What's Going OnMarvin Gaye1971
41 |
5RespectAretha Franklin1967
42 |
43 |
44 |
45 |
46 | 1
47 | Like a Rolling Stone
48 |
Bob Dylan
49 |
1965
50 |
51 |
52 | 2
53 | (I Can't Get No) Satisfaction
54 |
The Rolling Stones
55 |
1965
56 |
57 |
58 | 3
59 | Imagine
60 |
John Lennon
61 |
1971
62 |
63 |
64 | 4
65 | What's Going On
66 |
Marvin Gaye
67 |
1971
68 |
69 |
70 | 5
71 | Respect
72 |
Aretha Franklin
73 |
1967
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # jQuery Searcher Plugin
2 |
3 | The jQuery Searcher Plugin connects any list-like data with an input for searching.
4 |
5 | ## Installation
6 |
7 | Download the latest [release](https://github.com/lloiser/jquery-searcher/releases/) of this plugin on GitHub.
8 |
9 | Include the jquery.searcher.js script after the jQuery library (unless you are packaging scripts somehow else):
10 | ```html
11 |
12 | ```
13 |
14 | ## Usage
15 |
16 | ```js
17 | $("...").searcher({
18 | itemSelector: "...", // jQuery selector for the data item element
19 | textSelector: "...", // jQuery selector for the element which contains the text
20 | inputSelector: "..." // jQuery selector for the input element
21 | });
22 | ```
23 |
24 | ## Example
25 |
26 | See the live version of the following example on the [GitHub page](http://lloiser.github.io/jquery-searcher/).
27 |
28 | Given the following HTML markup:
29 | ```html
30 |
31 |
32 |
33 |
34 |
#1
35 |
Like a Rolling Stone
36 |
Bob Dylan
37 |
1965
38 |
39 |
40 |
#2
41 |
(I Can't Get No) Satisfaction
42 |
The Rolling Stones
43 |
1965
44 |
45 | ...
46 |
47 |
48 | ```
49 | And executing the following script connects the input with the table:
50 | ```js
51 | $("#tabledata").searcher({
52 | inputSelector: "#tablesearchinput"
53 | // itemSelector (tbody > tr) and textSelector (td) already have proper default values
54 | });
55 | ```
56 |
57 | ## Documentation
58 |
59 | The following table contains all possible options which can be passed to the plugin.
60 |
61 |
62 |
63 |
64 |
Name
65 |
Type
66 |
Description
67 |
68 |
69 |
70 |
71 |
itemSelector
72 |
string
73 |
74 | jQuery selector for the data item element (e.g. tr, li).
75 | Default: "tbody > tr"
76 |
77 |
78 |
79 |
textSelector
80 |
string
81 |
82 | jQuery selector for the element which contains the text within the item element.
83 | If not specified the data item element is used instead.
84 | Default: "td"
85 |
86 |
87 |
88 |
inputSelector
89 |
string
90 |
91 | jQuery selector for the input element which is used to filter the data.
92 | Default: ""
93 |
94 |
95 |
96 |
caseSensitive
97 |
bool
98 |
99 | Determines whether the search should be case sensitive or not.
100 | Default: false
101 |
102 |
103 |
104 |
toggle
105 |
function
106 |
107 | this function is called for each data item element when the text in the input changes.
108 | it is called with the data item element and a boolean value indicating whether the item contains the text or not.
109 | Default: function(item, containsText) { $(item).toggle(containsText); }
110 |
111 |
112 |
113 |
114 |
115 | ## License
116 |
117 | Copyright (c) 2014 Lukas Beranek Licensed under the MIT license.
--------------------------------------------------------------------------------
/src/jquery.searcher.js:
--------------------------------------------------------------------------------
1 | (function IIFE() {
2 |
3 | "use strict";
4 |
5 | function factory($)
6 | {
7 | var pluginName = "searcher",
8 | dataKey = "plugin_" + pluginName,
9 | defaults = {
10 | // selector for the item element
11 | itemSelector: "tbody > tr",
12 | // selector for the text elements
13 | textSelector: "td",
14 | // selector for the input
15 | inputSelector: "",
16 | // determines whether the search is case sensitive or not
17 | caseSensitive: false,
18 | // function to toggle the visibility of the item
19 | toggle: function(item, containsText)
20 | {
21 | $(item).toggle(containsText);
22 | }
23 | };
24 |
25 | function Searcher(element, options)
26 | {
27 | this.element = element;
28 |
29 | this.options = $.extend({ }, defaults, options);
30 |
31 | this._create();
32 | }
33 |
34 | Searcher.prototype = {
35 | dispose: function()
36 | {
37 | // unbind all events
38 | this._$input.unbind("." + pluginName);
39 | // toggle all elements with true
40 | var options = this.options,
41 | toggle = options.toggle || defaults.toggle;
42 | this._$element.find(options.itemSelector).each(function() { toggle(this, true); });
43 | },
44 | filter: function(value)
45 | {
46 | this._lastValue = value;
47 |
48 | var options = this.options,
49 | textSelector = options.textSelector,
50 | toggle = options.toggle || defaults.toggle;
51 |
52 | // build the regular expression for searching
53 | var flags = "gm" + (!options.caseSensitive ? "i" : "");
54 | var regex = new RegExp("(" + escapeRegExp(value) + ")", flags);
55 |
56 | this._$element
57 | .find(options.itemSelector)
58 | .each(function eachItem() {
59 | var $item = $(this),
60 | $textElements = textSelector ? $item.find(textSelector) : $item,
61 | itemContainsText = false;
62 |
63 | $textElements = $textElements.each(function eachTextElement() {
64 | itemContainsText = itemContainsText || !!$(this).text().match(regex);
65 | return !itemContainsText; // stop if at least one text element contains the text
66 | });
67 |
68 | toggle(this, itemContainsText);
69 | });
70 | },
71 | _create: function()
72 | {
73 | var options = this.options;
74 |
75 | this._$element = $(this.element);
76 |
77 | // find the input and bind to various events
78 | this._fn = $.proxy(this._onValueChange, this);
79 | var eventNames = "input." + pluginName + " change." + pluginName + " keyup." + pluginName;
80 | this._$input = $(options.inputSelector).bind(eventNames, this._fn);
81 |
82 | // remember the last entered value
83 | this._lastValue = null;
84 |
85 | // call the toggle with true for all items on startup
86 | var toggle = options.toggle || defaults.toggle;
87 | this._$element.find(options.itemSelector).each(function() { toggle(this, true); });
88 | },
89 | _onValueChange: function()
90 | {
91 | var value = this._$input.val();
92 | if (value === this._lastValue)
93 | return; // nothing has changed
94 |
95 | this.filter(value);
96 | }
97 | };
98 |
99 | function escapeRegExp(text)
100 | {
101 | // see https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
102 | return text.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
103 | }
104 |
105 | $.fn[pluginName] = function pluginHandler(options) {
106 | var args = Array.prototype.slice.call(arguments, 1);
107 | return this.each(function() {
108 | var searcher = $.data(this, dataKey);
109 | var t = typeof(options);
110 | if (t === "string" && searcher)
111 | {
112 | searcher[options].apply(searcher, args);
113 | if (options === "dispose")
114 | $.removeData(this, dataKey);
115 | }
116 | else if (t === "object")
117 | {
118 | if (!searcher)
119 | // create a new searcher
120 | $.data(this, dataKey, new Searcher(this, options));
121 | else
122 | // update the options of the existing
123 | $.extend(searcher.options, options);
124 | }
125 | });
126 | };
127 |
128 | }
129 |
130 | // AMD style (register as an anonymous module)
131 | if (typeof(define) === "function" && define.amd)
132 | define(["jquery"], factory);
133 | // node/CommonJS style (for Browserify)
134 | else if (typeof(exports) === "object")
135 | module.exports = factory;
136 | // browser
137 | else
138 | factory(jQuery);
139 |
140 | }).call(this);
141 |
--------------------------------------------------------------------------------
/dist/jquery.searcher.js:
--------------------------------------------------------------------------------
1 | /*! jQuery Searcher Plugin - v0.3.0 - 2016-01-29
2 | * https://github.com/lloiser/jquery-searcher/
3 | * Copyright (c) 2016 Lukas Beranek; Licensed MIT
4 | */
5 | (function IIFE() {
6 |
7 | "use strict";
8 |
9 | function factory($)
10 | {
11 | var pluginName = "searcher",
12 | dataKey = "plugin_" + pluginName,
13 | defaults = {
14 | // selector for the item element
15 | itemSelector: "tbody > tr",
16 | // selector for the text elements
17 | textSelector: "td",
18 | // selector for the input
19 | inputSelector: "",
20 | // determines whether the search is case sensitive or not
21 | caseSensitive: false,
22 | // function to toggle the visibility of the item
23 | toggle: function(item, containsText)
24 | {
25 | $(item).toggle(containsText);
26 | }
27 | };
28 |
29 | function Searcher(element, options)
30 | {
31 | this.element = element;
32 |
33 | this.options = $.extend({ }, defaults, options);
34 |
35 | this._create();
36 | }
37 |
38 | Searcher.prototype = {
39 | dispose: function()
40 | {
41 | // unbind all events
42 | this._$input.unbind("." + pluginName);
43 | // toggle all elements with true
44 | var options = this.options,
45 | toggle = options.toggle || defaults.toggle;
46 | this._$element.find(options.itemSelector).each(function() { toggle(this, true); });
47 | },
48 | filter: function(value)
49 | {
50 | this._lastValue = value;
51 |
52 | var options = this.options,
53 | textSelector = options.textSelector,
54 | toggle = options.toggle || defaults.toggle;
55 |
56 | // build the regular expression for searching
57 | var flags = "gm" + (!options.caseSensitive ? "i" : "");
58 | var regex = new RegExp("(" + escapeRegExp(value) + ")", flags);
59 |
60 | this._$element
61 | .find(options.itemSelector)
62 | .each(function eachItem() {
63 | var $item = $(this),
64 | $textElements = textSelector ? $item.find(textSelector) : $item,
65 | itemContainsText = false;
66 |
67 | $textElements = $textElements.each(function eachTextElement() {
68 | itemContainsText = itemContainsText || !!$(this).text().match(regex);
69 | return !itemContainsText; // stop if at least one text element contains the text
70 | });
71 |
72 | toggle(this, itemContainsText);
73 | });
74 | },
75 | _create: function()
76 | {
77 | var options = this.options;
78 |
79 | this._$element = $(this.element);
80 |
81 | // find the input and bind to various events
82 | this._fn = $.proxy(this._onValueChange, this);
83 | var eventNames = "input." + pluginName + " change." + pluginName + " keyup." + pluginName;
84 | this._$input = $(options.inputSelector).bind(eventNames, this._fn);
85 |
86 | // remember the last entered value
87 | this._lastValue = null;
88 |
89 | // call the toggle with true for all items on startup
90 | var toggle = options.toggle || defaults.toggle;
91 | this._$element.find(options.itemSelector).each(function() { toggle(this, true); });
92 | },
93 | _onValueChange: function()
94 | {
95 | var value = this._$input.val();
96 | if (value === this._lastValue)
97 | return; // nothing has changed
98 |
99 | this.filter(value);
100 | }
101 | };
102 |
103 | function escapeRegExp(text)
104 | {
105 | // see https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
106 | return text.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
107 | }
108 |
109 | $.fn[pluginName] = function pluginHandler(options) {
110 | var args = Array.prototype.slice.call(arguments, 1);
111 | return this.each(function() {
112 | var searcher = $.data(this, dataKey);
113 | var t = typeof(options);
114 | if (t === "string" && searcher)
115 | {
116 | searcher[options].apply(searcher, args);
117 | if (options === "dispose")
118 | $.removeData(this, dataKey);
119 | }
120 | else if (t === "object")
121 | {
122 | if (!searcher)
123 | // create a new searcher
124 | $.data(this, dataKey, new Searcher(this, options));
125 | else
126 | // update the options of the existing
127 | $.extend(searcher.options, options);
128 | }
129 | });
130 | };
131 |
132 | }
133 |
134 | // AMD style (register as an anonymous module)
135 | if (typeof(define) === "function" && define.amd)
136 | define(["jquery"], factory);
137 | // node/CommonJS style (for Browserify)
138 | else if (typeof(exports) === "object")
139 | module.exports = factory;
140 | // browser
141 | else
142 | factory(jQuery);
143 |
144 | }).call(this);
145 |
--------------------------------------------------------------------------------
/examples/list.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | List example
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | #. Title - Artist (Year)
18 |
19 |
Like a Rolling Stone - Bob Dylan (1965)
20 |
(I Can't Get No) Satisfaction - The Rolling Stones (1965)
21 |
Imagine - John Lennon (1971)
22 |
What's Going On - Marvin Gaye (1971)
23 |
Respect - Aretha Franklin (1967)
24 |
Good Vibrations - The Beach Boys (1966)
25 |
Johnny B. Goode - Chuck Berry (1958)
26 |
Hey Jude - The Beatles (1968)
27 |
Smells Like Teen Spirit - Nirvana (1991)
28 |
What'd I Say - Ray Charles (1959)
29 |
My Generation - The Who (1965)
30 |
A Change Is Gonna Come - Sam Cooke (1964)
31 |
Yesterday - The Beatles (1965)
32 |
Blowin' in the Wind - Bob Dylan (1963)
33 |
London Calling - The Clash (1980)
34 |
I Want to Hold Your Hand - The Beatles (1963)
35 |
Purple Haze - Jimi Hendrix (1967)
36 |
Maybellene - Chuck Berry (1955)
37 |
Hound Dog - Elvis Presley (1956)
38 |
Let It Be - The Beatles (1970)
39 |
Born to Run - Bruce Springsteen (1975)
40 |
Be My Baby - The Ronettes (1963)
41 |
In My Life - The Beatles (1965)
42 |
People Get Ready - The Impressions (1965)
43 |
God Only Knows - The Beach Boys (1966)
44 |
A Day in the Life - The Beatles (1967)
45 |
Layla - Derek and the Dominos (1970)
46 |
(Sittin' on) the Dock of the Bay - Otis Redding (1968)
47 |
Help! - The Beatles (1965)
48 |
I Walk the Line - Johnny Cash (1956)
49 |
Stairway to Heaven - Led Zeppelin (1971)
50 |
Sympathy for the Devil - The Rolling Stones (1968)
51 |
River Deep, Mountain High - Tina Turner (1966)
52 |
You've Lost That Lovin' Feeling - Righteous Brothers (1964)
53 |
Light My Fire - The Doors (1967)
54 |
One - U2 (1991)
55 |
No Woman, No Cry - Bob Marley (1975)
56 |
Gimme Shelter - The Rolling Stones (1969)
57 |
That'll Be the Day - Buddy Holly (1957)
58 |
Dancin' in the Streets - Martha and the Vandellas (1964)
59 |
The Weight - The Band (1968)
60 |
Waterloo Sunset - The Kinks (1968)
61 |
Tutti Frutti - Little Richard (1956)
62 |
Georgia on My Mind - Ray Charles (1960)
63 |
Heartbreak Hotel - Elvis Presley (1956)
64 |
Heroes - David Bowie (1977)
65 |
Bridge Over Troubled Water - Simon & Garfunkel (1970)
66 |
All Along the Watchtower - Jimi Hendrix (1968)
67 |
Hotel California - The Eagles (1976)
68 |
The Tracks of My Tears - Smokey Robinson (1965)
69 |
The Message - Grandmaster Flash (1982)
70 |
When Doves Cry - Prince (1984)
71 |
Anarchy in the U.K. - The Sex Pistols (1977)
72 |
When a Man Loves a Woman - Percy Sledge (1966)
73 |
Louie Louie - The Kingsmen (1963)
74 |
Long Tall Sally - Little Richard (1956)
75 |
A Whiter Shade of Pale - Procol Harum (1967)
76 |
Billie Jean - Michael Jackson (1983)
77 |
The Times They Are A-Changin' - Bob Dylan (1964)
78 |
Let's Stay Together - Al Green (1971)
79 |
Whole Lotta Shakin' Going On - Jerry Lee Lewis (1957)
80 |
Bo Diddley - Bo Diddley (1955)
81 |
For What It's Worth - Buffalo Springfield (1967)
82 |
She Loves You - The Beatles (1963)
83 |
Sunshine of Your Love - Cream (1968)
84 |
Redemption Song - Bob Marley (1980)
85 |
Jailhouse Rock - Elvis Presley (1957)
86 |
Tangled Up in Blue - Bob Dylan (1975)
87 |
Crying - Roy Orbison (1961)
88 |
Walk On By - Dionne Warwick (1964)
89 |
California Girls - The Beach Boys (1965)
90 |
Papa's Got a Brand New Bag - James Brown (1966)
91 |
Summertime Blues - Eddie Cochran (1958)
92 |
Superstition - Stevie Wonder (1972)
93 |
Whole Lotta Love - Led Zeppelin (1969)
94 |
Strawberry Fields Forever - The Beatles (1967)
95 |
Mystery Train - Elvis Presley (1955)
96 |
I Got You (I Feel Good) - James Brown (1965)
97 |
Mr. Tambourine Man - The Byrds (1965)
98 |
I Heard It Through the Grapevine - Marvin Gaye (1968)
99 |
Blueberry Hill - Fats Domino (1956)
100 |
You Really Got Me - The Kinks (1964)
101 |
Norwegian Wood (This Bird Has Flown) - The Beatles (1965)
102 |
Every Breath You Take - The Police (1983)
103 |
Crazy - Patsy Cline (1961)
104 |
Thunder Road - Bruce Springsteen (1975)
105 |
Ring of Fire - Johnny Cash (1963)
106 |
My Girl - The Temptations (1965)
107 |
California Dreamin' - The Mamas & The Papas (1965)
108 |
In the Still of the Night - The Five Satins (1956)
109 |
Suspicious Minds - Elvis Presley (1969)
110 |
Blitzkrieg Bop - The Ramones (1976)
111 |
I Still Haven't Found What I'm Looking For - U2 (1987)
112 |
Good Golly, Miss Molly - Little Richard (1958)
113 |
Blue Suede Shoes - Carl Perkins (1956)
114 |
Great Balls of Fire - Jerry Lee Lewis (1957)
115 |
Roll Over Beethoven - Chuck Berry (1956)
116 |
Love and Happiness - Al Green (1972)
117 |
Fortunate Son - Creedence Clearwater Revival (1969)
118 |
You Can't Always Get What You Want - Rolling Stones (1969)