Skip to content

Instantly share code, notes, and snippets.

@erhangundogan
Created October 15, 2013 11:26
Show Gist options
  • Select an option

  • Save erhangundogan/6990150 to your computer and use it in GitHub Desktop.

Select an option

Save erhangundogan/6990150 to your computer and use it in GitHub Desktop.
ul.list-city {
margin:1em;
padding:0;
}
ul.list-city li {
list-style-type: none;
}
.content {
clear:left;
margin:1em;
}
h3 {
margin:1em;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://documentcloud.github.io/underscore/underscore-min.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<h3>Popular Destinations</h3>
<div class="container"></div>
<div class="content"></div>
</body>
</html>
var cities = [
{name: "Moscow", count: 12, content: "<p>description</p>"},
{name: "Amsterdam", count: 25, content: "html"},
{name: "Lisbon", count: 15, content: "html"},
{name: "Berlin", count: 19, content: "html"},
{name: "Madrid", count: 25, content: "html"}
];
$(function(){
var columnCount = 3,
sortedCities = _.sortBy(cities, "name").reverse(),
rowCount = Math.ceil(cities.length / columnCount);
for (var c = 0; c < columnCount; c++) {
var list = $("<ul class='list-city'/>");
for (var r = 0; r < rowCount; r++) {
var city = sortedCities.pop();
if (city) {
var cell = $("<li/>")
.append(
$("<a/>")
.attr("href","#")
.attr("id", city.name)
.append(city.name))
.append(" (" + city.count + ")");
list.append(cell);
}
}
$(".container").append(
$("<div/>")
.css("float", "left")
.append(list)
);
}
$("ul.list-city li > a").on("click", function(ev) {
ev.preventDefault();
var searchCriteria = $(this).attr("id");
var foundCity = _.find(cities, function(city) {
return city.name == searchCriteria;
});
if (foundCity && foundCity.content) {
$(".content").html(foundCity.content);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment