Area Finder Control
A search/browse control for province / city / suburb with additional data
by Swarts
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Area Finder Control</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </a>
<a class="brand" href="#">Area Finder Control</a>
<div class="nav-collapse">
<ul class="nav">
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<div class="areaFinderControl" data-bind="template: 'myAF'"></div>
</div>
<script type="text/html" id="myAF">
<div class="span6...
CSS
.label-info {
background-color: yellow;
}
.label > .label-info {
color: gray;
text-shadow: none;
}
JavaScript
var myAF = myAF || {};
myAF.provinces = [{
province : 'Gauteng',
cities : [{
city : 'Johannesburg',
suburbs : [{
suburb : 'Bryanston',
total : 2
},{
suburb : 'Claremont',
total : 6
}, {
suburb : 'Northcliff',
total : 4
}, {
suburb : 'Sandton',
total : 7
}]
}, {
city : 'Pretoria',
suburbs : [{
suburb : 'Brooklyn',
total : 3
}, {
suburb : 'Hatfield',
total : 2
}, {
suburb : 'Wapadrand',
total : 9
}]
}]
}, {
province : 'KwaZulu-Natal',
cities : [{
city : 'Durban',
suburbs : [{
suburb : 'Umhlanga',
total : 1
}]
}]
}, {
province : 'Western Cape',
cities : [{
city : 'Cape Town',
suburbs : [{
suburb : 'Claremont',
total : 18
}, {
suburb : 'Durbanville',
total : 6
}, {
suburb : 'Woodstock',
total : 1
}]
}, {
city : 'George',
suburbs : [{
suburb : 'Glenbarrie',
total : 3
}, {
suburb : 'Loerie Park',
total : 1
}, {
suburb : 'Parkdene',
total : 1
}]
}]
}];
//Add namespace
var myAF = myAF || {};
//Define Viewmodel
myAF.ViewModel = function() {
var self = this;
self.search = ko.observable('');
self.found = ko.observableArray();
self.resultsShown = ko.observable(false);
self.toggleResultsShown = function() {
self.resultsShown(!self.resultsShown());
}
self.provinces = ko.mapping.fromJS(myAF.provinces);
self.selectedProvince = ko.observable(self.provinces()[0]);
self.selectedCity = ko.observable(self.selectedProvince().cities()[0]);
self.searchThrottled =...