You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.9 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
var itemsSelectTag = $('#itemsSelectTag').selectize()[0].selectize;
document.getElementById('customersSelectTag').addEventListener('change', function () {
var customerId = this.value;
// FETCH CUSTOMER ITEMS
fetch('/fetch-customer-items/' + customerId + '/')
.then(response => {
return response.json();
})
.then(data => {
itemsSelectTag.clearOptions();
// Remove the selected option "Select Customer First"
var selectCustomerOption = itemsSelectTag.options['Select Customer First'];
if (selectCustomerOption) {
delete itemsSelectTag.options['Select Customer First'];
}
// Add items related to the customer
data.items_related_to_customer.forEach(function (item) {
itemsSelectTag.addOption({value: item.id, text: item.title});
});
// Add items without a customer
data.items_without_customer.forEach(function (item) {
itemsSelectTag.addOption({value: item.id, text: item.title});
});
itemsSelectTag.refreshOptions();
})
.catch(error => console.error('Error fetching customer items:', error));
// FETCH CUSTOMER BUSINESSES
$.ajax({
url: `/fetch-customer-businesses/${customerId}/`,
type: 'GET',
success: function (data) {
$('#businessSelectTag').empty();
if (data.business) {
$('#businessSelectTag').append(`<option value="${data.business.id}">${data.business.name}</option>`);
}
},
error: function (xhr, status, error) {
console.error(error);
}
});
});
});