mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 15:11:58 -04:00
Added QBO Item support with autocomplete
This commit is contained in:
67
assets/javascripts/autocomplete.js
Normal file
67
assets/javascripts/autocomplete.js
Normal file
@@ -0,0 +1,67 @@
|
||||
(function () {
|
||||
|
||||
window.initLineItemAutocomplete = function(context) {
|
||||
let scope = context || document;
|
||||
$(scope).find(".line-item-description").each(function() {
|
||||
if ($(this).data("autocomplete-initialized")) return;
|
||||
$(this).data("autocomplete-initialized", true);
|
||||
|
||||
$(this).autocomplete({
|
||||
appendTo: "body",
|
||||
minLength: 2,
|
||||
source: function(request, response) {
|
||||
$.getJSON("/items/autocomplete", { q: request.term })
|
||||
.done(function(data) {
|
||||
response(data.map(function(item) {
|
||||
return {
|
||||
label: item.text,
|
||||
value: item.text,
|
||||
id: item.id,
|
||||
price: item.price || 0
|
||||
};
|
||||
}));
|
||||
})
|
||||
.fail(function(err){
|
||||
console.error("Autocomplete error:", err);
|
||||
response([]);
|
||||
});
|
||||
},
|
||||
select: function(event, ui) {
|
||||
let $input = $(this);
|
||||
let row = $input.closest(".line-item");
|
||||
|
||||
$input.val(ui.item.value); // <-- set description
|
||||
row.find(".item-id-field").val(ui.item.id);
|
||||
|
||||
if (ui.item.price !== undefined && row.find(".price-field").length) {
|
||||
row.find(".price-field").val(ui.item.price);
|
||||
}
|
||||
|
||||
updateLineItemTotals();
|
||||
|
||||
return false; // still prevent default to avoid double entry
|
||||
},
|
||||
change: function(event, ui) {
|
||||
if (!ui.item) {
|
||||
let row = $(this).closest(".line-item");
|
||||
row.find(".item-id-field").val("");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Clear item_id when user types manually
|
||||
$(document).on("input", ".line-item-description", function(){
|
||||
let row = $(this).closest(".line-item");
|
||||
row.find(".item-id-field").val("");
|
||||
});
|
||||
|
||||
function initializeAutocomplete() {
|
||||
window.initLineItemAutocomplete(document);
|
||||
}
|
||||
|
||||
$(document).ready(initializeAutocomplete);
|
||||
document.addEventListener("turbo:load", initializeAutocomplete);
|
||||
|
||||
})();
|
||||
@@ -22,7 +22,11 @@
|
||||
Date.now().toString()
|
||||
);
|
||||
|
||||
//container.insertAdjacentHTML("beforeend", content);
|
||||
container.insertAdjacentHTML("beforeend", content);
|
||||
|
||||
// initialize autocomplete on the new row
|
||||
initLineItemAutocomplete(container.lastElementChild);
|
||||
}
|
||||
|
||||
// REMOVE
|
||||
@@ -50,4 +54,11 @@
|
||||
|
||||
// Works for Turbo navigation
|
||||
document.addEventListener("turbo:load", initNestedForms);
|
||||
})();
|
||||
})();
|
||||
|
||||
$(document).on("input", ".line-item-description", function(){
|
||||
|
||||
let row = $(this).closest(".line-item");
|
||||
row.find(".item-id-field").val("");
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user