Skip to content

Commit

Permalink
Added toggle and complete callbacks
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
rmm5t committed Mar 6, 2013
1 parent 7235cea commit 511789d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
29 changes: 17 additions & 12 deletions dist/jquery.sieve.js
@@ -1,5 +1,5 @@
/*!
* jQuery Sieve v0.2.5 (2013-02-28)
* jQuery Sieve v0.3.0-pre (2013-03-05)
* http://rmm5t.github.com/jquery-sieve/
* Copyright (c) 2013 Ryan McGeary; Licensed MIT
*/
Expand Down Expand Up @@ -28,33 +28,38 @@
searchInput: null,
searchTemplate: "<div><label>Search: <input type='text'></label></div>",
itemSelector: "tbody tr",
textSelector: null
textSelector: null,
toggle: function(item, match) {
return item.toggle(match);
},
complete: function() {}
}, options);
if (!settings.searchInput) {
searchBar = $(settings.searchTemplate);
settings.searchInput = searchBar.find("input");
container.before(searchBar);
}
return settings.searchInput.on("keyup.sieve change.sieve", function() {
var query, rows;
var items, query;
query = compact($(this).val().toLowerCase().split(/\s+/));
rows = container.find(settings.itemSelector);
return rows.each(function() {
var cells, matches, q, row, text, _i, _len;
row = $(this);
items = container.find(settings.itemSelector);
items.each(function() {
var cells, item, match, q, text, _i, _len;
item = $(this);
if (settings.textSelector) {
cells = row.find(settings.textSelector);
cells = item.find(settings.textSelector);
text = cells.text().toLowerCase();
} else {
text = row.text().toLowerCase();
text = item.text().toLowerCase();
}
matches = true;
match = true;
for (_i = 0, _len = query.length; _i < _len; _i++) {
q = query[_i];
matches && (matches = text.indexOf(q) >= 0);
match && (match = text.indexOf(q) >= 0);
}
return row.toggle(matches);
return settings.toggle(item, match);
});
return settings.complete();
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.sieve.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -12,7 +12,7 @@
<div class="pull-right text-right">
<a href="https://github.com/rmm5t/jquery-sieve"><span class="label label-info"><i class="icon-github-alt"></i> View on GitHub</span></a>
<a href="http://plugins.jquery.com/sieve/"><span class="label label-info"><i class="icon-external-link"></i> View on Plugin Registry</span></a>
<span class="label label-inverse">v0.2.5</span>
<span class="label label-inverse">v0.3.0-pre</span>
</div>
<h1>
Sieve jQuery Plugin
Expand Down
2 changes: 1 addition & 1 deletion sieve.jquery.json
Expand Up @@ -2,7 +2,7 @@
"name": "sieve",
"title": "jQuery Sieve",
"description": "jQuery plugin that allows you to quickly add an interactive search filter to any block of content.",
"version": "0.2.5",
"version": "0.3.0-pre",
"author": {
"name": "Ryan McGeary",
"email": "ryan@mcgeary.org",
Expand Down
21 changes: 12 additions & 9 deletions src/jquery.sieve.coffee
Expand Up @@ -14,6 +14,8 @@ $.fn.sieve = (options) ->
searchTemplate: "<div><label>Search: <input type='text'></label></div>"
itemSelector: "tbody tr"
textSelector: null
toggle: (item, match) -> item.toggle(match)
complete: ->
}, options)

if !settings.searchInput
Expand All @@ -23,19 +25,20 @@ $.fn.sieve = (options) ->

settings.searchInput.on "keyup.sieve change.sieve", ->
query = compact($(this).val().toLowerCase().split(/\s+/))
rows = container.find(settings.itemSelector)
items = container.find(settings.itemSelector)

rows.each ->
row = $(this)
items.each ->
item = $(this)
if settings.textSelector
cells = row.find(settings.textSelector)
cells = item.find(settings.textSelector)
text = cells.text().toLowerCase()
else
text = row.text().toLowerCase()

matches = true
text = item.text().toLowerCase()

match = true
for q in query
matches &&= text.indexOf(q) >= 0
match &&= text.indexOf(q) >= 0

settings.toggle(item, match)

row.toggle(matches)
settings.complete()

0 comments on commit 511789d

Please sign in to comment.