I'm trying and failing to filter a list from another array with checkboxes. Essentially I have an array of jobs which inlcude an array of locations in each job. I have checkboxes of the options and have created an array of chosenLocations.
All of the above is working but I can't achieve the filtered list I require. The filtered list is displaying and is exactly the same as hiringManagerJobs, but the filtering isn't working.
I've posted my code below. Issue is in the viewModel.filteredJobs function.
KnockoutJs
var Search = function(){
var self = this;
self.hiringManagerJobs = ko.observableArray();
self.hiringManagerFilterSearchTerm = ko.observable();
self.hiringManagerFilterId = document.getElementById('hiringManagerId').value
self.LocationsFacets = ko.observableArray();
self.chosenLocations = ko.observableArray();
self.filteredJobs = ko.observableArray();
//self.filteredJobs = ko.observableArray();
searchByHiringManager = function () {
$.get('/...
}).done(function (data) {
self.hiringManagerJobs(data.Results);
self.LocationsFacets(data.Facets.Locations);
})
}
searchByHiringManager();
}
var viewModel = new Search();
viewModel.filteredJobs = ko.computed(function () {
var chosenLocations = ko.utils.arrayFilter(viewModel.LocationFacets,
function (p) {
return p.selected();
});
var jobs = viewModel.hiringManagerJobs();
if (chosenLocations.length == 0) //if none selected return all
return jobs;
else { //other wise only return selected jobs
return ko.utils.arrayFilter(jobs, function(job){
return ko.utils.arrayFilter(job.Locations, function(location){
return ko.utils.arrayFilter(chosenLocations,
function(chosenLocation) {
return location == chosenLocation.Value;
}).length > 0;
})
})
}
})
ko.applyBindings(viewModel);
HTML
<!-- ko foreach: LocationsFacets -->
<input type="checkbox" data-bind="checkedValue: $data, checked: $root.chosenLocations" />
<span data-bind="text: Value"></span>
<!-- /ko -->
<ul class="list-group" data-bind="foreach: filteredJobs">
<li class="list-group-item" data-bind="with: Document">
<p class="job-title media-heading" data-bind="text: JobTitle"></p>
</li>
</ul>
The JSON Arrays in the knockout View Model are like below
"chosenLocations": [
{
"Type": 0,
"From": null,
"To": null,
"Value": "London",
"Count": 1
},
{
"Type": 0,
"From": null,
"To": null,
"Value": "Glasgow",
"Count": 1
}
],
"hiringManagerJobs": [
{
"Score": 1,
"Highlights": null,
"Document": {
"id": "1b41ce24-280d-4fe7-8488-d4babd522bc9",
"JobTitle": "Test HiringManagerFilterId",
"CompanyName": "Test Company",
"ExtUrl": "https://.....",
"Locations": [
"London",
"Manchester",
"New York"
],
"JobSummary": "blah blah blah",
"OgLogo": null,
"HiringManagerFilterId": "xjifu9fdasjkf985ed4"
}
},
{
"Score": 1,
"Highlights": null,
"Document": {
"id": "853880b3-fbae-4271-8034-7868c4de63a8",
"JobTitle": "Senior Manager - Software Development",
"CompanyName": "Test Company",
"ExtUrl": "https:......",
"Locations": [
"London",
"Glasgow",
"Edinburgh"
],
"JobSummary": "blah blah blah ",
"OgLogo": null,
"HiringManagerFilterId": "xjifu9fdasjkf985ed4"
}
}
],
"LocationsFacets": [
{
"Type": 0,
"From": null,
"To": null,
"Value": "Edinburgh",
"Count": 1
},
{
"Type": 0,
"From": null,
"To": null,
"Value": "Glasgow",
"Count": 1
},
{
"Type": 0,
"From": null,
"To": null,
"Value": "London",
"Count": 1
}
],
p.selected();, but I don't seep.selectedbeing set anywhere so this would always return an empty array, followed by the function returning all jobs "//if none selected return all"