Created
June 27, 2014 21:03
-
-
Save developerdizzle/66519b46ffcfd7fa9660 to your computer and use it in GitHub Desktop.
Bootstrap Filter plugin
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 ($) { | |
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