Created
July 16, 2014 11:26
-
-
Save jmcd/5d4d40a133d4ca55532b to your computer and use it in GitHub Desktop.
jQuery plugin to filter tables/whatever
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 ($) { | |
$.fn.filtered = function (options) { | |
var roots = this; | |
var displayProperty = $(roots).first().css('display'); | |
var elementContentArrays = []; | |
setUpSelectorsAsFunctions(); | |
preLoadContent(); | |
setUpEventListeners(); | |
function setUpSelectorsAsFunctions() { | |
var filterLen = options.filters.length; | |
for (var index = 0; index < filterLen; index++) { | |
var findSelector = options.filters[index].findSelector; | |
var findSelectorType = typeof (findSelector); | |
if (findSelectorType !== "function") { | |
options.filters[index].findSelector = function (item) { return $(item).find(findSelector); }; | |
} | |
} | |
} | |
function setUpEventListeners() { | |
for (var index in options.filters) { | |
var inputSelector = options.filters[index].inputSelector; | |
$(inputSelector).keyup(function () { | |
performFilter(); | |
if (options.after !== undefined) { | |
options.after($(roots)); | |
} | |
}); | |
$(inputSelector).click(function () { | |
if (($(this).is(':radio') || $(this).is(':checkbox')) && $(this).is(':checked')) { | |
performFilter(); | |
if (options.after !== undefined) { | |
options.after($(roots)); | |
} | |
} | |
}); | |
} | |
} | |
function preLoadContent() { | |
$(roots).each(function () { | |
var contentArray = []; | |
var filterLen = options.filters.length; | |
for (var index = 0; index < filterLen; index++) { | |
var findSelector = options.filters[index].findSelector; | |
var contents = findSelector($(this)); | |
contents.each(function () { | |
var contentText = $(this).text().toLowerCase(); | |
contentArray.push(contentText); | |
}); | |
} | |
elementContentArrays.push(contentArray); | |
}); | |
} | |
function performFilter() { | |
var filterCount = options.filters.length; | |
var inputValuesForEachFilter = []; | |
for (var index2 = 0; index2 < filterCount; index2++) { | |
var inputSelector = options.filters[index2].inputSelector; | |
var targets2 = getInputValues(inputSelector); | |
inputValuesForEachFilter.push(targets2); | |
} | |
$(roots).each(function(elementIndex) { | |
var contents = elementContentArrays[elementIndex]; | |
var match = true; | |
for (var filterIndex = 0; filterIndex < filterCount; filterIndex++) { | |
var targets = inputValuesForEachFilter[filterIndex]; | |
var targetLen = targets.length; | |
for (var targetIndex = 0; targetIndex < targetLen; targetIndex++) { | |
var target = targets[targetIndex]; | |
var subMatch = false; | |
for (var contentIndex = 0; contentIndex < contents.length; contentIndex++) { | |
var contentText = contents[contentIndex]; | |
var contains = contains2(target, contentText); | |
if (contains) { | |
subMatch = true; | |
break; | |
} | |
} | |
if (!subMatch) { | |
match = false; | |
break; | |
} | |
} | |
if (!match) { | |
break; | |
} | |
} | |
setVisibility(this, match); | |
}); | |
} //performFilter | |
function setVisibility(item, visible) { | |
if (visible) { | |
$(item).css({ 'display': displayProperty }); | |
} else { | |
$(item).css({ 'display': 'none' }); | |
} | |
} | |
function getInputValues(inputSelector) { | |
var result = []; | |
$(inputSelector).each(function () { | |
var input = $(this); | |
if (input.is(':radio') || input.is(':checkbox')) { | |
if (input.is(':checked')) { | |
result.push(input.val().toLowerCase()); | |
} | |
} else { | |
if (input.is('[placeholder]') && input.val() === input.attr('placeholder')) { | |
return; | |
} | |
result.push(input.val().toLowerCase()); | |
} | |
}); | |
return result; | |
} | |
function contains2(target, searchSpace) { | |
return searchSpace.indexOf(target) != -1; | |
} | |
return this; | |
}; | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment