I am trying to filter some results based on a group of check box's, using list.js plugin.
I have managed to sort by one criteria at the moment, which only sorts by one item at a time i.e. only easy, only moderate, but when I try and select multiple checkbox's at the moment it doesn't work ie. selecting both easy and moderate at the same time.
Does anyone have any advice how I can do this. The html and javascript are below.
Thanks in advance.
<div id="search-results">
Sort by:
<button class="sort btn" data-sort="name">
Name <i class="fa fa-fw"></i>
</button>
<button class="sort btn" data-sort="date">
Departures <i class="fa fa-fw"></i>
</button>
<button class="sort btn" data-sort="difficulty">
Difficulty <i class="fa fa-fw"></i>
</button>
Filter:
<label for="easy">Easy</label>
<input type="checkbox" name="easy" class="filter" data-value="Easy" />
<label for="moderate">Moderate</label>
<input type="checkbox" name="moderate" class="filter" data-value="Moderate" />
<label for="challenging">Challenging</label>
<input type="checkbox" name="challenging" class="filter" data-value="Challenging" />
<div class="list">
@foreach (var page in umbracoPages.OrderBy(x => x.Difficulty))
{
<div class="package">
<span class="name">@page.Name</span><br />
<span class="date"><strong>@page.Date @(page.Date == 1 ? "departure" : "departures") available</strong></span><br />
<span class="difficulty">@page.Difficulty</span>
</div>
}
</div>
</div>
Javascript:
$(function () {
$("#dateFrom, #dateTo").datepicker({
dateFormat: "dd.mm.yy",
constrainInput: true,
changeMonth: true,
changeYear: true,
minDate: 0
});
var options = {
valueNames: ['name', 'date', 'difficulty' ]
};
var userList = new List('search-results', options);
//filter
$('.filter').change(function() {
var bool = this.checked;
var value = $(this).data("value");
userList.filter(function (item) {
if (item.values().difficulty == value && bool == true) {
return true;
} else if (userList.filtered && bool == false) {
return true;
} else {
return false;
}
});
return false;
}); });
You need to store active filters in an array and check against all of them.
$(function() {
var options = {
valueNames: ['name', 'date', 'difficulty' ]
};
var userList = new List('search-results', options);
var activeFilters = [];
//sort
userList.on("updated", function () {
$('.sort').each(function () {
if ($(this).hasClass("asc")) {
$(this).find(".fa").addClass("fa-sort-asc").removeClass("fa-sort-desc").show();
} else if ($(this).hasClass("desc")) {
$(this).find(".fa").addClass("fa-sort-desc").removeClass("fa-sort-asc").show();
} else {
$(this).find(".fa").hide();
}
})
})
//filter
$('.filter').change(function() {
var isChecked = this.checked;
var value = $(this).data("value");
if(isChecked){
// add to list of active filters
activeFilters.push(value);
}
else
{
// remove from active filters
activeFilters.splice(activeFilters.indexOf(value), 1);
}
userList.filter(function (item) {
if(activeFilters.length > 0)
{
return(activeFilters.indexOf(item.values().difficulty)) > -1;
}
return true;
});
});
});
http://codepen.io/anon/pen/QbOYaG
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With