mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-06-25 11:37:22 +02:00
Custom auto-complete fields, remove DAL requirement
This commit is contained in:
34
static/js/autocomplete_model.js
Normal file
34
static/js/autocomplete_model.js
Normal file
@ -0,0 +1,34 @@
|
||||
$(document).ready(function () {
|
||||
$(".autocomplete").keyup(function(e) {
|
||||
let target = $("#" + e.target.id);
|
||||
let prefix = target.attr("id");
|
||||
let api_url = target.attr("api_url");
|
||||
let api_url_suffix = target.attr("api_url_suffix");
|
||||
if (!api_url_suffix)
|
||||
api_url_suffix = "";
|
||||
let name_field = target.attr("name_field");
|
||||
if (!name_field)
|
||||
name_field = "name";
|
||||
let input = target.val();
|
||||
|
||||
$.getJSON(api_url + "?format=json&search=^" + input + api_url_suffix, function(objects) {
|
||||
let html = "";
|
||||
|
||||
objects.results.forEach(function (obj) {
|
||||
html += li(prefix + "_" + obj.id, obj[name_field]);
|
||||
});
|
||||
|
||||
$("#" + prefix + "_list").html(html);
|
||||
|
||||
objects.results.forEach(function (obj) {
|
||||
$("#" + prefix + "_" + obj.id).click(function() {
|
||||
target.val(obj[name_field]);
|
||||
$("#" + prefix + "_pk").val(obj.id);
|
||||
});
|
||||
|
||||
if (input === obj[name_field])
|
||||
$("#" + prefix + "_pk").val(obj.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user