forked from algolia/algoliasearch-client-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.html
More file actions
85 lines (75 loc) · 2.92 KB
/
jquery.html
File metadata and controls
85 lines (75 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script src="/dist/algoliasearch.jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/fontawesome/4.3.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/3.3.4/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<section class="panel">
<header class="panel-heading">
<div class="search_box">
<form action="#" method="get">
<input autocomplete="off" class="autocomplete" id="q" placeholder="Start typing" type="text" spellcheck="false" />
<div class="searchbutton">
<i class="icon-search icon-large"></i>
</div>
</form>
</div>
</header>
</section>
<h1>Results</h1>
<div id="hits"></div>
<script type="text/javascript">
function searchCallback(content) {
if (content.query !== $('#q').val()) {
// do not take out-dated answers into account
return;
}
if (content.hits.length === 0) {
// no results
$('#hits').empty();
return;
}
// Scan all hits and display them
var html = '';
for (var i = 0; i < content.hits.length; ++i) {
var hit = content.hits[i];
html += '<div class="hit">';
for (var attribute in hit._highlightResult) {
html += '<div class="attribute">' +
'<span>' + attribute + ': </span>' +
hit._highlightResult[attribute].value +
'</div>';
}
html += '</div>';
}
$('#hits').html(html);
}
$(document).ready(function() {
var $inputfield = $('#q');
// Replace the following values by your ApplicationID and ApiKey.
var client = $.algolia.Client('latency', '6be0576ff61c053d5f9a3225e2a90f76');
// Replace the following value by the name of the index you want to query.
var index = client.initIndex('contacts');
$inputfield.keyup(function() {
index.search($inputfield.val(), { hitsPerPage: 5 })
.done(searchCallback)
.fail(function(content) { console.log('Error', content); });
}).focus().closest('form').on('submit', function() {
// on form submit, store the query string in the anchor
location.replace('#q=' + encodeURIComponent($inputfield.val()));
return false;
});
// check if there is a query in the anchor: http://example.org/#q=my+query
if (location.hash && location.hash.indexOf('#q=') === 0) {
var q = decodeURIComponent(location.hash.substring(3));
$inputfield.val(q).trigger('keyup');
}
});
</script>
</body>
</html>