-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
232 lines (197 loc) · 10.9 KB
/
index.html
File metadata and controls
232 lines (197 loc) · 10.9 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<title>Autocomplete 2</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="materialize.min.css">
<script type="text/javascript" src="jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="materialize.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
.top-row{padding-top: 3rem;}
.card .card-title{padding: 20px}
.material-icons {vertical-align: middle; line-height: inherit}
</style>
</head>
<body class="grey lighten-4">
<script type="text/javascript">
$(document).ready(function () {
ajaxAutoComplete({inputId: 'searchField', ajaxURL: 'https://www.gsmarena.com/search-json.php3?sSearch='});
});
function ajaxAutoComplete(options) {
var $input = $("#" + options.inputId);
var $autocomplete = $('<ul id="ac" class="autocomplete-content dropdown-content" style="position:absolute"></ul>'),
$inputDiv = $input.closest('.input-field'),
request,
runningRequest = false,
timeout,
liSelected;
if ($inputDiv.length) {
$inputDiv.append($autocomplete); // Set ul in body
} else {
$input.after($autocomplete);
}
var highlight = function (string, match) {
var matchStart = string.toLowerCase().indexOf("" + match.toLowerCase() + ""),
matchEnd = matchStart + match.length - 1,
beforeMatch = string.slice(0, matchStart),
matchText = string.slice(matchStart, matchEnd + 1),
afterMatch = string.slice(matchEnd + 1);
string = "<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>";
return string;
};
$autocomplete.on('click', 'li', function () {
$input.val($(this).text().trim());
$autocomplete.empty();
});
$input.on('keyup', function (e) {
if (timeout) { // comment to remove timeout
clearTimeout(timeout);
}
if (runningRequest) {
request.abort();
}
if (e.which === 13) { // select element
liSelected[0].getElementsByTagName('a')[0].click();
return;
}
if (e.which === 40) { // down arrow
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.next();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = $autocomplete.find('li').eq(0).addClass('selected');
}
} else {
liSelected = $autocomplete.find('li').eq(0).addClass('selected');
}
return; // stop new AJAX call
} else if (e.which === 38) { // up arrow
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.prev();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = $autocomplete.find('li').last().addClass('selected');
}
} else {
liSelected = $autocomplete.find('li').last().addClass('selected');
}
return;
}
// escape these keys
if (e.which === 9 || // tab
e.which === 16 || // shift
e.which === 17 || // ctrl
e.which === 18 || // alt
e.which === 20 || // caps lock
e.which === 35 || // end
e.which === 36 || // home
e.which === 37 || // left arrow
e.which === 39 || // right arrow
e.which === 44 ) {// print screen
return;
} else if (e.which === 27) { // Esc. Close ul
$autocomplete.empty();
return;
}
var val = $input.val().toLowerCase();
$autocomplete.empty();
if (val.length > 2) {
timeout = setTimeout(function () { // comment this line to remove timeout
runningRequest = true;
request = $.ajax({
type: 'GET',
url: options.ajaxURL + val,
success: function (data) {
if (!$.isEmptyObject(data)) {
var appendList = '';
for (var key in data) {
if (data.hasOwnProperty(key)) {
if (key === "news") { // only reading news
for (var news in data[key]) {
var li = '';
if (!!data[key]) {
li += "<li>" + "<img src='" + data[key][news].src + "' class='left circle'>";
li += "<a href='" + "http://www.gsmarena.com/" + data[key][news].href + "'>";
li += "<span>" + highlight(data[key][news].text, val) + "</span></a></li>";
} else {
li += '<li><span>' + key + '</span></li>';
}
appendList += li;
}
}
}
}
$autocomplete.append(appendList);
}
},
complete: function () {
runningRequest = false;
}
});
}, 250); // comment this line to remove timeout
}
});
$(document).click(function () {
if (!$(event.target).closest($autocomplete).length) {
$autocomplete.empty();
}
});
}
</script>
<div class="container">
<div class="row">
<h3 class="center-align" style="font-weight: 300">Materialize CSS Autocomplete with AJAX</h3>
<div class="col s12">
<div class="card amber">
<div class="card-content">
<div class="row" style="margin-bottom: 0px">
<div class="col s1 center-align"><i class="material-icons">info_outline</i></div><div class="col s11"><p>Please enable loading scripts from other sources for proper functionality.
You will be prompted to enable them once you type more than 3 letters in the search box. (If on Chrome browser, check the end of the Address Bar)</p></div>
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content">
<div class="row" style="margin-bottom: 12px">
<div class="col s12 m6 offset-m3">
<div class="input-field">
<input id="searchField" name="searchField" type="text" class="autocomplete" required placeholder="Search" autocomplete="off">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col s12 m4">
<div class="card blue-grey darken-1">
<div class="card-content white-text" style="text-align: justify">
<p>This is a demonstration on how to make an autocomplete dropdown that fetches data from a server as the user types. You can read the full article
<a href="http://blog.chanukawijayakoon.me/2016/11/materialize-css-autocomplete-with-ajax/" target="_blank">here.</a></p><br>
<p>Try typing a few queries related to mobile phones.</p>
<p class="grey-text text-lighten-1">Eg: Nokia, Apple, RAM, Android</p><br>
<p>Here, the search box queries www.gsmarena.com for news articles as the user gives input. When the user stops typing,
an AJAX request is made which fetches data from the servers and then they are presented in a drop-down.
Note that in this case the drop-down list items have been made into links to the relevant news posts.</p>
</div>
</div>
</div>
<div class="col s12 m8">
<div class="card">
<div class="card-title blue white-text">Code</div>
<div class="card-content">
<script src="https://gist.github.com/merovingienne/2b034e0a7b26d9a82ab2f92c58c83738.js"></script>
</div>
</div>
</div>
</div>
</div>
</body>
</html>