function primeCreateNew() { // Clear the form $('.form-control').val(''); const form = document.getElementById('ItemModalForm'); document.getElementById('itemModifyModalLabel').innerText= "Create a new item"; form.setAttribute('method', 'POST'); return true; } function primeEdit() { const form = document.getElementById('ItemModalForm'); document.getElementById('itemModifyModalLabel').innerText = 'Edit an item'; form.setAttribute('method', 'PATCH'); return true; } function getDataForEdit(id) { $.ajax({ type: 'get', url: `/api/v1/items?id=${id}`, success: function (data) { const result = JSON.parse(data); // Get elements inside the editCategoryModal const modal_itemName = document.getElementById('itemModifyModalName'); const modal_itemComment = document.getElementById('itemModifyModalComment'); const modal_itemAmount = document.getElementById('itemModifyModalAmount'); const modal_itemSKU = document.getElementById('itemModifyModalSKU'); const modal_itemStorageLocation = document.getElementById('itemModifyModalStorageLocation'); const modal_itemManufacturer = document.getElementById('itemModifyModalManuf'); const modal_itemCategory = document.getElementById('itemModifyModalCategory'); const modal_itemStatus = document.getElementById('itemModifyModalStatus'); const modal_itemid = document.getElementById('itemModifyModalId'); modal_itemName.value = result.name; modal_itemComment.value = result.comment; modal_itemAmount.value = result.amount; modal_itemSKU.value = result.SKU; modal_itemManufacturer.value = result.manufacturer; // Select the correct option in the dropdown const modal_itemCategoryOptions = modal_itemCategory.options; for (let i = 0; i < modal_itemCategoryOptions.length; i++) { if (modal_itemCategoryOptions[i].value == result.category.id) { modal_itemCategoryOptions[i].selected = true; } } // Select the correct option in the dropdown const modal_itemStatusOptions = modal_itemStatus.options; for (let i = 0; i < modal_itemStatusOptions.length; i++) { if (modal_itemStatusOptions[i].value == result.status.id) { modal_itemStatusOptions[i].selected = true; } } // Select the correct option in the dropdown const modal_itemStorageLocationOptions = modal_itemStorageLocation.options; for (let i = 0; i < modal_itemStorageLocationOptions.length; i++) { if (modal_itemStorageLocationOptions[i].value == result.storageLocation.id) { modal_itemStorageLocationOptions[i].selected = true; } } modal_itemid.value = result.id; }, error: function (data) { console.log('!!!! ERROR !!!!', data); // Hide overlay with spinner $('.loader-overlay').removeClass('active'); // Close the modal $('.modal').modal('hide'); createNewToast(' Something went wrong. The category does no longer exist.', "text-bg-danger") } }); }