mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial push to public repo
This commit is contained in:
40
netbox/project-static/js/livesearch.js
Normal file
40
netbox/project-static/js/livesearch.js
Normal file
@@ -0,0 +1,40 @@
|
||||
$(document).ready(function() {
|
||||
var search_field = $('#id_livesearch');
|
||||
var search_key = search_field.attr('data-key');
|
||||
var label = search_field.attr('data-label');
|
||||
if (!label) {
|
||||
label = 'name';
|
||||
}
|
||||
|
||||
search_field.autocomplete({
|
||||
source: function(request, response) {
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: search_field.attr('data-source'),
|
||||
data: search_key + '=' + request.term,
|
||||
success: function(data) {
|
||||
var choices = [];
|
||||
$.each(data, function (index, choice) {
|
||||
choices.push({
|
||||
value: choice.id,
|
||||
label: choice[label]
|
||||
});
|
||||
});
|
||||
response(choices);
|
||||
}
|
||||
});
|
||||
},
|
||||
select: function(event, ui) {
|
||||
event.preventDefault();
|
||||
search_field.val(ui.item.label);
|
||||
var real_field = $('#id_' + search_field.attr('data-field'));
|
||||
real_field.empty();
|
||||
real_field.append($("<option></option>").attr('value', ui.item.value).text(ui.item.label));
|
||||
real_field.change();
|
||||
// If the field has a parent helper, reset the parent to no selection
|
||||
$('select[filter-for="' + real_field.attr('name') + '"]').val('');
|
||||
},
|
||||
minLength: 4,
|
||||
delay: 500
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user