| 1 |
<!doctype html> |
| 2 |
|
| 3 |
<html lang="en"> |
| 4 |
<head> |
| 5 |
<meta charset="utf-8"> |
| 6 |
|
| 7 |
<title></title> |
| 8 |
<meta name="description" content=""> |
| 9 |
<meta name="author" content=""> |
| 10 |
|
| 11 |
<link rel="stylesheet" href="../src/jquery.typeahead.css"> |
| 12 |
|
| 13 |
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> |
| 14 |
<script src="../src/jquery.typeahead.js"></script> |
| 15 |
|
| 16 |
</head> |
| 17 |
<body> |
| 18 |
|
| 19 |
<div style="width: 100%; max-width: 800px; margin: 0 auto;"> |
| 20 |
|
| 21 |
<h1>Hockey_v2 Demo</h1> |
| 22 |
|
| 23 |
<ul> |
| 24 |
<li> |
| 25 |
<a href="http://www.runningcoder.org/jquerytypeahead/documentation/">Documentation</a> |
| 26 |
</li> |
| 27 |
<li> |
| 28 |
<a href="http://www.runningcoder.org/jquerytypeahead/demo/">Demos</a> |
| 29 |
</li> |
| 30 |
</ul> |
| 31 |
|
| 32 |
<div style="margin-bottom: 20px;"> |
| 33 |
<button onclick="addRandomTeam()">Add a random Team</button> |
| 34 |
<button onclick="removeRandomTeam()">Remove a random Team</button> |
| 35 |
</div> |
| 36 |
|
| 37 |
<form> |
| 38 |
<div class="typeahead__container"> |
| 39 |
<div class="typeahead__field"> |
| 40 |
<div class="typeahead__query"> |
| 41 |
<input class="js-typeahead" |
| 42 |
name="q" |
| 43 |
autofocus |
| 44 |
autocomplete="off"> |
| 45 |
</div> |
| 46 |
<div class="typeahead__button"> |
| 47 |
<button type="submit"> |
| 48 |
<span class="typeahead__search-icon"></span> |
| 49 |
</button> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
</div> |
| 53 |
</form> |
| 54 |
|
| 55 |
<script> |
| 56 |
|
| 57 |
var data = [{ |
| 58 |
"name": "Ducks", |
| 59 |
"img": "ducks", |
| 60 |
"city": "Anaheim", |
| 61 |
"id": "ANA", |
| 62 |
"conference": "Western", |
| 63 |
"division": "Pacific" |
| 64 |
}, { |
| 65 |
"name": "Thrashers", |
| 66 |
"img": "thrashers", |
| 67 |
"city": "Atlanta", |
| 68 |
"id": "ATL", |
| 69 |
"conference": "Eastern", |
| 70 |
"division": "Southeast" |
| 71 |
}, { |
| 72 |
"name": "Bruins", |
| 73 |
"img": "bruins", |
| 74 |
"city": "Boston", |
| 75 |
"id": "BOS", |
| 76 |
"conference": "Eastern", |
| 77 |
"division": "Northeast" |
| 78 |
}, { |
| 79 |
"name": "Sabres", |
| 80 |
"img": "sabres", |
| 81 |
"city": "Buffalo", |
| 82 |
"id": "BUF", |
| 83 |
"conference": "Eastern", |
| 84 |
"division": "Northeast" |
| 85 |
}, { |
| 86 |
"name": "Flames", |
| 87 |
"img": "flames", |
| 88 |
"city": "Calgary", |
| 89 |
"id": "CGY", |
| 90 |
"conference": "Western", |
| 91 |
"division": "Northwest" |
| 92 |
}, { |
| 93 |
"name": "Hurricanes", |
| 94 |
"img": "hurricanes", |
| 95 |
"city": "Carolina", |
| 96 |
"id": "CAR", |
| 97 |
"conference": "Eastern", |
| 98 |
"division": "Southeast" |
| 99 |
}, { |
| 100 |
"name": "Blackhawks", |
| 101 |
"img": "blackhawks", |
| 102 |
"city": "Chicago", |
| 103 |
"id": "CHI", |
| 104 |
"conference": "Western", |
| 105 |
"division": "Central" |
| 106 |
}, { |
| 107 |
"name": "Avalanche", |
| 108 |
"img": "avalanche", |
| 109 |
"city": "Colorado", |
| 110 |
"id": "COL", |
| 111 |
"conference": "Western", |
| 112 |
"division": "Northwest" |
| 113 |
}, { |
| 114 |
"name": "Bluejackets", |
| 115 |
"img": "bluejackets", |
| 116 |
"city": "Columbus", |
| 117 |
"id": "CBJ", |
| 118 |
"conference": "Western", |
| 119 |
"division": "Central" |
| 120 |
}, { |
| 121 |
"name": "Stars", |
| 122 |
"img": "stars", |
| 123 |
"city": "Dallas", |
| 124 |
"id": "DAL", |
| 125 |
"conference": "Western", |
| 126 |
"division": "Pacific" |
| 127 |
}, { |
| 128 |
"name": "Red Wings", |
| 129 |
"img": "redwings", |
| 130 |
"city": "Detroit", |
| 131 |
"id": "DET", |
| 132 |
"conference": "Western", |
| 133 |
"division": "Central" |
| 134 |
}, { |
| 135 |
"name": "Oilers", |
| 136 |
"img": "oilers", |
| 137 |
"city": "Edmonton", |
| 138 |
"id": "EDM", |
| 139 |
"conference": "Western", |
| 140 |
"division": "Northwest" |
| 141 |
}, { |
| 142 |
"name": "Panthers", |
| 143 |
"img": "panthers", |
| 144 |
"city": "Florida", |
| 145 |
"id": "FLA", |
| 146 |
"conference": "Eastern", |
| 147 |
"division": "Southeast" |
| 148 |
}, { |
| 149 |
"name": "Kings", |
| 150 |
"img": "kings", |
| 151 |
"city": "Los Angeles", |
| 152 |
"id": "LAK", |
| 153 |
"conference": "Western", |
| 154 |
"division": "Pacific" |
| 155 |
}, { |
| 156 |
"name": "Wild", |
| 157 |
"img": "wild", |
| 158 |
"city": "Minnesota", |
| 159 |
"id": "MIN", |
| 160 |
"conference": "Western", |
| 161 |
"division": "Northwest" |
| 162 |
}, { |
| 163 |
"name": "Canadiens", |
| 164 |
"img": "canadiens", |
| 165 |
"city": "Montreal", |
| 166 |
"id": "MTL", |
| 167 |
"conference": "Eastern", |
| 168 |
"division": "Northeast" |
| 169 |
}, { |
| 170 |
"name": "Predators", |
| 171 |
"img": "predators", |
| 172 |
"city": "Nashville", |
| 173 |
"id": "NSH", |
| 174 |
"conference": "Western", |
| 175 |
"division": "Central" |
| 176 |
}, { |
| 177 |
"name": "Devils", |
| 178 |
"img": "devils", |
| 179 |
"city": "New Jersey", |
| 180 |
"id": "NJD", |
| 181 |
"conference": "Eastern", |
| 182 |
"division": "Atlantic" |
| 183 |
}, { |
| 184 |
"name": "Islanders", |
| 185 |
"img": "islanders", |
| 186 |
"city": "New York", |
| 187 |
"id": "NYI", |
| 188 |
"conference": "Eastern", |
| 189 |
"division": "Atlantic" |
| 190 |
}, { |
| 191 |
"name": "Rangers", |
| 192 |
"img": "rangers", |
| 193 |
"city": "New York", |
| 194 |
"id": "NYR", |
| 195 |
"conference": "Eastern", |
| 196 |
"division": "Atlantic" |
| 197 |
}, { |
| 198 |
"name": "Senators", |
| 199 |
"img": "senators", |
| 200 |
"city": "Ottawa", |
| 201 |
"id": "OTT", |
| 202 |
"conference": "Eastern", |
| 203 |
"division": "Northeast" |
| 204 |
}, { |
| 205 |
"name": "Flyers", |
| 206 |
"img": "flyers", |
| 207 |
"city": "Philadelphia", |
| 208 |
"id": "PHI", |
| 209 |
"conference": "Eastern", |
| 210 |
"division": "Atlantic" |
| 211 |
}, { |
| 212 |
"name": "Coyotes", |
| 213 |
"img": "coyotes", |
| 214 |
"city": "Phoenix", |
| 215 |
"id": "PHX", |
| 216 |
"conference": "Western", |
| 217 |
"division": "Pacific" |
| 218 |
}, { |
| 219 |
"name": "Penguins", |
| 220 |
"img": "penguins", |
| 221 |
"city": "Pittsburgh", |
| 222 |
"id": "PIT", |
| 223 |
"conference": "Eastern", |
| 224 |
"division": "Atlantic" |
| 225 |
}, { |
| 226 |
"name": "Sharks", |
| 227 |
"img": "sharks", |
| 228 |
"city": "San Jose", |
| 229 |
"id": "SJS", |
| 230 |
"conference": "Western", |
| 231 |
"division": "Pacific" |
| 232 |
}, { |
| 233 |
"name": "Blues", |
| 234 |
"img": "blues", |
| 235 |
"city": "St. Louis", |
| 236 |
"id": "STL", |
| 237 |
"conference": "Western", |
| 238 |
"division": "Central" |
| 239 |
}, { |
| 240 |
"name": "Lightning", |
| 241 |
"img": "lightning", |
| 242 |
"city": "Tampa Bay", |
| 243 |
"id": "TBL", |
| 244 |
"conference": "Eastern", |
| 245 |
"division": "Southeast" |
| 246 |
}, { |
| 247 |
"name": "Maple Leafs", |
| 248 |
"img": "mapleleafs", |
| 249 |
"city": "Toronto", |
| 250 |
"id": "TOR", |
| 251 |
"conference": "Eastern", |
| 252 |
"division": "Northeast" |
| 253 |
}, { |
| 254 |
"name": "Canucks", |
| 255 |
"img": "canucks", |
| 256 |
"city": "Vancouver", |
| 257 |
"id": "VAN", |
| 258 |
"conference": "Western", |
| 259 |
"division": "Northwest" |
| 260 |
}, { |
| 261 |
"name": "Capitals", |
| 262 |
"img": "capitals", |
| 263 |
"city": "Washington", |
| 264 |
"id": "WSH", |
| 265 |
"conference": "Eastern", |
| 266 |
"division": "Southeast" |
| 267 |
}, { |
| 268 |
"name": "Jets", |
| 269 |
"img": "jets", |
| 270 |
"city": "Winnipeg", |
| 271 |
"id": "WPG", |
| 272 |
"conference": "Eastern", |
| 273 |
"division": "Southeast" |
| 274 |
}]; |
| 275 |
|
| 276 |
typeof $.typeahead === 'function' && $.typeahead({ |
| 277 |
input: ".js-typeahead", |
| 278 |
minLength: 1, |
| 279 |
maxItem: 8, |
| 280 |
maxItemPerGroup: 6, |
| 281 |
order: "asc", |
| 282 |
hint: true, |
| 283 |
searchOnFocus: true, |
| 284 |
multiselect: { |
| 285 |
// limit: 2, |
| 286 |
// limitTemplate: 'You can\'t select more than 2 teams', |
| 287 |
matchOn: ["id", "name"], |
| 288 |
cancelOnBackspace: true, |
| 289 |
// href: '/{{name}}.html', |
| 290 |
// data: [{ |
| 291 |
// "matchedKey": "name", |
| 292 |
// "name": "Canadiens", |
| 293 |
// "img": "canadiens", |
| 294 |
// "city": "Montreal", |
| 295 |
// "id": "MTL", |
| 296 |
// "conference": "Eastern", |
| 297 |
// "division": "Northeast", |
| 298 |
// "group": "teams" |
| 299 |
// }], |
| 300 |
data: function () { |
| 301 |
var deferred = $.Deferred(); |
| 302 |
|
| 303 |
setTimeout(function () { |
| 304 |
deferred.resolve([{ |
| 305 |
"matchedKey": "name", |
| 306 |
"name": "Canadiens", |
| 307 |
"img": "canadiens", |
| 308 |
"city": "Montreal", |
| 309 |
"id": "MTL", |
| 310 |
"conference": "Eastern", |
| 311 |
"division": "Northeast", |
| 312 |
"group": "teams" |
| 313 |
}]); |
| 314 |
}, 2000); |
| 315 |
|
| 316 |
deferred.always(function () { |
| 317 |
console.log('data loaded from promise'); |
| 318 |
}); |
| 319 |
|
| 320 |
return deferred; |
| 321 |
}, |
| 322 |
callback: { |
| 323 |
onClick: function (node, item, event) { |
| 324 |
event.preventDefault(); |
| 325 |
console.log(item); |
| 326 |
alert(item.name + ' Clicked!'); |
| 327 |
}, |
| 328 |
onCancel: function (node, item, event) { |
| 329 |
console.log(item) |
| 330 |
} |
| 331 |
} |
| 332 |
}, |
| 333 |
templateValue: "{{name}}", |
| 334 |
display: ["name", "city"], |
| 335 |
emptyTemplate: 'no result for {{query}}', |
| 336 |
source: { |
| 337 |
teams: { |
| 338 |
data: data |
| 339 |
} |
| 340 |
}, |
| 341 |
callback: { |
| 342 |
onSubmit: function (node, form, items, event) { |
| 343 |
event.preventDefault(); |
| 344 |
|
| 345 |
alert(JSON.stringify(items)) |
| 346 |
} |
| 347 |
}, |
| 348 |
debug: true |
| 349 |
}); |
| 350 |
|
| 351 |
function addRandomTeam() { |
| 352 |
var randomTeamInt = getRandomInt(0, data.length - 1); |
| 353 |
var randomTeam = data[randomTeamInt]; |
| 354 |
|
| 355 |
if (!Typeahead[".js-typeahead"].addMultiselectItemLayout(randomTeam)) { |
| 356 |
alert(randomTeam.name + ' is aleady added!') |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
function removeRandomTeam() { |
| 361 |
if (!Typeahead[".js-typeahead"].items.length) { |
| 362 |
alert('Teams are Empty'); |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
var randomTeamIndex = getRandomInt(0, Typeahead[".js-typeahead"].items.length - 1); |
| 367 |
|
| 368 |
Typeahead[".js-typeahead"].cancelMultiselectItem(randomTeamIndex); |
| 369 |
} |
| 370 |
|
| 371 |
function getRandomInt(min, max) { |
| 372 |
min = Math.ceil(min); |
| 373 |
max = Math.floor(max); |
| 374 |
|
| 375 |
return Math.floor(Math.random() * (max - min)) + min; |
| 376 |
} |
| 377 |
|
| 378 |
</script> |
| 379 |
</div> |
| 380 |
|
| 381 |
</body> |
| 382 |
</html> |