Skip to content

Instantly share code, notes, and snippets.

@developerdizzle
Created June 27, 2014 21:03
Show Gist options
  • Save developerdizzle/66519b46ffcfd7fa9660 to your computer and use it in GitHub Desktop.
Save developerdizzle/66519b46ffcfd7fa9660 to your computer and use it in GitHub Desktop.
Bootstrap Filter plugin
(function ($) {
var lists = {};
function filterList($list) {
var shouldShow = function ($item) {
var filters = lists[$item.id];
for (var filter in filters) {
var className = filters[filter];
if (className) {
if (!$item.is(className)) {
return false;
}
}
}
return true;
}
$list.find('.filter-item').each(function () {
var $li = $(this);
var show = shouldShow($li);
$li.toggleClass('active', show);
if ($li.hasClass('fade')) {
$li.toggleClass('in', show);
}
});
}
$(function () {
$(document).on('click', '[data-toggle="filter"]', function (e) {
e.preventDefault();
var $this = $(this);
var $li = $this.closest('li');
var $parent = $this.closest('ul');
var parent = $this.data('parent').substr(1);
var $list = $($this.data('target'));
$parent.find('li').removeClass('active');
$li.addClass('active');
var filter = $this.data('filter');
lists[$list.id] = lists[$list.id] || {};
lists[$list.id][parent] = filter;
filterList($list);
$this.trigger({
type: 'filtered.bs.filter',
relatedTarget: $list
});
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment