Click here to Skip to main content
15,664,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using javascript and knockoutjs to implement a search filter using the viewmodel .I am not able to get the search filter to work.below is my js file

//Object Constructor Class for Locations
var Locations = function (title, lat, lng, id, marker){

  this.title = title; = lat;
  this.lng  = lng; = id;
  this.marker = marker;

var viewModel = {
  locations : [
    new Locations('The Travel Cafe',  51.499542,  -0.114134,  
    new Locations('Scooter Caffè',  51.50050110129609,  -0.11393191896812872,  
    new Locations('Four Corners',  51.50042544028603,  -0.11365986202011345,  
    new Locations('The Library Lounge at Marriott County Hall',  51.50193765161251,  
                  -0.12006535004714862, '543148b9498ec4159f277ce6'),
    new Locations('Coleman Coffee Roasters',  51.500636,  -0.112947,  

  searchBox: ko.observable('')

}; = ko.computed(function() {
  var self = this;
  var searchResult = this.searchBox().toLowerCase();

  return ko.utils.arrayFilter(self.locations, function(markerLocation) {
            var title = markerLocation.title.toLowerCase();
            var matched = title.indexOf(searchResult) > -1;
            var marker = markerLocation.marker;
            if (marker) {
            //when I console.log(markerLocation.marker)---> i get undefined
            return matched;
    }, viewModel);


here is the html file

<pre><!DOCTYPE html>
<html lang="en">
	<meta charset= "utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=Edge">
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<link rel="stylesheet" href="css/style.css">
	<title>Neighbourhood Project</title>

	<div class=".container">
		<div id="map"></div>
	  <input type="text" placeholder="Search for locations" data-bind:"value: searchBox">
	  <ul data-bind="foreach: search">
	    <li data-bind="text: title "></li>
  <script type="text/javascript" src="scripts/knockout-3.4.2.js"></script>
	<script type="text/javascript" src="js/app.js"></script>
	<script type="text/javascript" src="js/map.js"></script>
	<script async defer type="text/javascript" src=""></script>

What I have tried:

i have tried to declare the locations[] array in the viewmodel as an ko.observableArray(). but it didn't work.
can some pls help me with this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900