Created
April 28, 2014 20:48
-
-
Save joshstrange/11383566 to your computer and use it in GitHub Desktop.
A very simple full text search for hiding/showing elements on a page that requires jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var QuickAndDirtySearch = new QuickAndDirtySearchController($('#myTable tbody tr'), $('#searchBox')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function QuickAndDirtySearchController(elements, searchBox) { | |
var instance = this; | |
instance.elements = elements; | |
instance.searchBox = searchBox; | |
instance.elements.each(function(){ | |
var el = $(this); | |
var text = instance.recursiveText(el); | |
el.data('searchText', text); | |
}); | |
instance.searchBox.on('keyup', function(){ | |
var val = $(this).val(); | |
instance.search(val); | |
}); | |
}; | |
QuickAndDirtySearchController.prototype.search = function(text) { | |
var instance = this; | |
instance.elements.each(function(){ | |
var el = $(this); | |
if(el.data('searchText').indexOf(text.toLowerCase()) != -1) { | |
el.show(); | |
} else { | |
el.hide(); | |
} | |
}); | |
}; | |
QuickAndDirtySearchController.prototype.recursiveText = function(el) { | |
var instance = this; | |
var fullText =[]; | |
var children = el.children(); | |
if(children.length) { | |
children.each(function(){ | |
fullText.push(instance.recursiveText($(this))); | |
}); | |
return fullText.join(' '); | |
} else { | |
return el.text().toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment