Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hideOnInit and hideIfEmpty options; Formatted Readme #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,53 @@ Usage
-----

Include jQuery, the plugin, then initialize the plugin:

<script src="jquery-1.6.4.min.js"></script>
<script src="jquery.fastLiveFilter.js"></script>
<script>
$(function() {
$('#search_input').fastLiveFilter('#search_list');
});
</script>
```html
<script src="jquery-1.6.4.min.js"></script>
<script src="jquery.fastLiveFilter.js"></script>
<script>
$(function() {
$('#search_input').fastLiveFilter('#search_list');
});
</script>
```

The above would work with this HTML:

<input id="search_input" placeholder="Type to filter">
<ul id="search_list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
```html
<input id="search_input" placeholder="Type to filter">
<ul id="search_list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
```

Options
-------

Options are given as the second argument. Synopsis:

$(INPUT_SELECTOR).fastLiveFilter(LIST_SELECTOR, options);
```javascript
$(INPUT_SELECTOR).fastLiveFilter(LIST_SELECTOR, options);
```

Available options:

- timeout: How many milliseconds to wait after keydown before filtering the list. Default is 0.
- callback: A callback method which will be given the number of items left in the list.
- selector: By default, the plugin will match the filter against the text of the `li`. If specifed, the selector will be applied to the `li` and the resulting text will be used instead. **WARNING:** Use of complex selectors may reduce performance significantly, especially in large lists!
Option | Type | Default | Description
------ | ---- | ------- | -----------
timeout | integer | 0 | How many milliseconds to wait after keydown before filtering the list
hideOnInit | boolean | false | Hides all the list items after initialization
hideIfEmpty | boolean | false | Hides all list items if the search field is empty
callback | function | none | A callback method which will be given the number of items left in the list
selector | jQuery selector | text of `li` | By default, the plugin will match the filter against the text of the `li`. If specifed, the selector will be applied to the `li` and the resulting text will be used instead. **WARNING:** Use of complex selectors may reduce performance significantly, especially in large lists!

Example:

$('#search_input').fastLiveFilter('#search_list', {
timeout: 200,
callback: function(total) { $('#num_results').html(total); }
});

```javascript
$('#search_input').fastLiveFilter('#search_list', {
timeout: 200,
hideOnInit: true,
hideIfEmpty: true,
callback: function(total) { $('#num_results').html(total); }
});
```
Problems? Want to contribute?
-----------------------------

Expand Down
52 changes: 34 additions & 18 deletions jquery.fastLiveFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jQuery.fn.fastLiveFilter = function(list, options) {
var lastFilter = '';
var timeout = options.timeout || 0;
var callback = options.callback || function() {};

var hideOnInit = options.hideOnInit || false;
var hideIfEmpty = options.hideIfEmpty || false;
var keyTimeout;

// NOTE: because we cache lis & len here, users would need to re-init the plugin
Expand All @@ -23,27 +24,42 @@ jQuery.fn.fastLiveFilter = function(list, options) {
var lis = list.children();
var len = lis.length;
var oldDisplay = len > 0 ? lis[0].style.display : "block";
callback(len); // do a one-time callback on initialization to make sure everything's in sync
if (hideOnInit) {
lis.each(function() {
this.style.display = "none";
});
}
if (hideOnInit || (hideIfEmpty && !$.trim(this.value).length)) {
callback(0); // return the correct value of 0 if the search field is empty upon init or hideOnInit is true
} else {
callback(len); // do a one-time callback on initialization to make sure everything's in sync
}

input.change(function() {
// var startTime = new Date().getTime();
var numShown = 0;
var filter = input.val().toLowerCase();
var li, innerText;
var numShown = 0;
for (var i = 0; i < len; i++) {
li = lis[i];
innerText = !options.selector ?
(li.textContent || li.innerText || "") :
$(li).find(options.selector).text();

if (innerText.toLowerCase().indexOf(filter) >= 0) {
if (li.style.display == "none") {
li.style.display = oldDisplay;
}
numShown++;
} else {
if (li.style.display != "none") {
li.style.display = "none";
if (hideIfEmpty && !$.trim(this.value).length) {
lis.each(function() {
this.style.display = "none";
});
} else {
for (var i = 0; i < len; i++) {
li = lis[i];
innerText = !options.selector ?
(li.textContent || li.innerText || "") :
$(li).find(options.selector).text();

if (innerText.toLowerCase().indexOf(filter) >= 0) {
if (li.style.display == "none") {
li.style.display = oldDisplay;
}
numShown++;
} else {
if (li.style.display != "none") {
li.style.display = "none";
}
}
}
}
Expand All @@ -54,7 +70,7 @@ jQuery.fn.fastLiveFilter = function(list, options) {
}).keydown(function() {
clearTimeout(keyTimeout);
keyTimeout = setTimeout(function() {
if( input.val() === lastFilter ) return;
if (input.val() === lastFilter) return;
lastFilter = input.val();
input.change();
}, timeout);
Expand Down