-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathautocompleteajax.js
143 lines (120 loc) · 5.38 KB
/
autocompleteajax.js
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
/**************************
* Auto complete plugin *
*************************/
$.fn.autocompleteajax = function (options) {
// Defaults
var defaults = {
data: {},
ajax: {},
callback: null,
delay: null,
minLength: 2,
autocomplete_id: null,
input_name: null
};
options = $.extend(defaults, options);
return this.each(function() {
var $input = $(this);
var data = options.data,
$inputDiv = $input.closest('.input-field'); // Div to append on
// Create autocomplete element
var $autocomplete = $('<ul class="autocomplete-content dropdown-content"></ul>');
// Append autocomplete element
if ($inputDiv.length) {
$inputDiv.append($autocomplete); // Set ul in body
} else {
$input.after($autocomplete);
}
var highlight = function(string, $el) {
var img = $el.find('img');
var matchStart = $el.text().toLowerCase().indexOf("" + string.toLowerCase() + ""),
matchEnd = matchStart + string.length - 1,
beforeMatch = $el.text().slice(0, matchStart),
matchText = $el.text().slice(matchStart, matchEnd + 1),
afterMatch = $el.text().slice(matchEnd + 1);
$el.html("<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>");
if (img.length) {
$el.prepend(img);
}
};
var timer;
// Perform search
$input.on('keyup', function (e) {
if($(this).val().length > options.minLength) {
// Send ajax request
if (timer){
clearTimeout(timer);
}
timer = setTimeout(function() {
if(!$.isEmptyObject(options.ajax)) {
// Set search string
var dataCopy = {'string' : $input.val()};
$.ajax({
url: options.ajax.url,
method: options.ajax.method,
data: dataCopy,
dataType: options.ajax.dataType,
error: options.ajax.error,
beforeSend: function(jqXHR, settings) {
options.ajax.beforeSend ? options.ajax.beforeSend(jqXHR, settings) : null;
},
success: function(res) {
data = res;
// If wanna work with response result and get the updated version
if(options.callback != null) {
data = options.callback(res) || data;
}
}
});
}
// Capture Enter
if (e.which === 13) {
$autocomplete.find('li').first().click();
return;
}
}, options.delay);
}
var val = $input.val().toLowerCase();
$autocomplete.empty();
// Check if the input isn't empty
if (val !== '') {
var results = data.data;
(results) && $.each(results, function(i, value) {
if (value.value.toLowerCase().indexOf(val) !== -1 &&
value.value.toLowerCase() !== val) {
var autocompleteOption = $('<li data-id="'+ value.id +'"></li>');
if(!!value.image) {
autocompleteOption.append('<img src="'+ value.image +'" class="right circle"><span>'+ value.value +'</span>');
} else {
autocompleteOption.append('<span>'+ value.value +'</span>');
}
$autocomplete.append(autocompleteOption);
highlight(val, autocompleteOption);
}
});
}
});
// Set input value
$autocomplete.on('click', 'li', function () {
$input.val($(this).text().trim());
var autocomplete_id = options.autocomplete_id;
if (autocomplete_id) {
var input_name = options.input_name ? options.input_name : 'autocomplete-id[' + autocomplete_id + ']';
if($('#' + autocomplete_id).length == 0) {
$input.after("<input type='hidden' id='" + autocomplete_id +
"' name='" + input_name + "' class='multi_autocomplete_id' value='" + $(this).attr('data-id') +
"' data-initial='" + $(this).attr('data-id') + "'>");
} else {
$('#' + autocomplete_id).val($(this).attr('data-id')).attr('data-initial', $(this).attr('data-id'));
}
} else {
if($('.autocomplete-id').length == 0) {
$input.after("<input type='hidden' class='autocomplete-id' name='autocomplete-id' value='" + $(this).attr('data-id') + "'>");
} else {
$('.autocomplete-id').val($(this).attr('data-id'));
}
}
$autocomplete.empty();
});
});
};