Fixed users and locations not filling in in Assets modals

This commit is contained in:
2025-10-26 11:45:01 +01:00
parent 2c0ae9f5e9
commit dc5e117c93
2 changed files with 50 additions and 25 deletions

View File

@@ -489,13 +489,16 @@ document.addEventListener('DOMContentLoaded', () => {
updateAttributesContainer.innerHTML = ''; updateAttributesContainer.innerHTML = '';
updateForm.reset(); updateForm.reset();
const locationSelect = updateForm.querySelector('#updateLocationSelect');
await loadLocationsIntoSelect(locationSelect);
try { try {
const response = await fetch(`/Assets/Get?cn=${assetId}`); const response = await fetch(`/Assets/Get?cn=${assetId}`);
const responseJson = await response.json(); const responseJson = await response.json();
const asset = responseJson.assetsModel; const asset = responseJson.assetsModel;
const locationSelect = updateForm.querySelector('#updateLocationSelect');
await loadLocationsIntoSelect(locationSelect, asset.Location);
const usersSelect = updateForm.querySelector('#updateUsersSelect');
await loadUsersIntoSelect(usersSelect, asset.Owner);
for (const [key, value] of Object.entries(asset)) { for (const [key, value] of Object.entries(asset)) {
const input = updateForm.querySelector(`[name="${key}"]`); const input = updateForm.querySelector(`[name="${key}"]`);
if (input) input.value = value; if (input) input.value = value;

View File

@@ -123,18 +123,35 @@ async function loadLocationsIntoSelect(selectElement, selectedValue = null) {
const response = await fetch('/Locations/Index'); const response = await fetch('/Locations/Index');
const locations = await response.json(); const locations = await response.json();
selectElement.innerHTML = `<option value="">${appTranslations.selectLocation}</option>`; const ts = selectElement.tomselect;
if (!ts) {
selectElement.innerHTML = `<option value="">${appTranslations.selectLocation}</option>`;
locations.forEach(loc => { locations.forEach(loc => {
const text = `${loc.location}`;
const option = document.createElement('option'); const option = document.createElement('option');
option.value = loc.location; option.value = loc.location;
option.textContent = text; option.textContent = loc.location;
if (selectedValue && selectedValue === loc.location) { if (selectedValue && selectedValue === loc.location) {
option.selected = true; option.selected = true;
} }
selectElement.appendChild(option); selectElement.appendChild(option);
}); });
return;
}
ts.clearOptions();
ts.addOption(locations.map(loc => ({
value: loc.location,
text: loc.location
})));
ts.refreshOptions(false);
if (selectedValue) {
ts.setValue(selectedValue);
} else {
ts.clear(true);
}
} catch (err) { } catch (err) {
console.error('Error loading locations:', err); console.error('Error loading locations:', err);
showToast(appTranslations.errorLoadingLocations, 'danger'); showToast(appTranslations.errorLoadingLocations, 'danger');
@@ -146,18 +163,23 @@ async function loadUsersIntoSelect(selectElement, selectedValue = null) {
const response = await fetch('/Users/Index?Cn=false&Sn=false&Title=false&Description=false&JpegPhoto=false&UserPassword=false'); const response = await fetch('/Users/Index?Cn=false&Sn=false&Title=false&Description=false&JpegPhoto=false&UserPassword=false');
const users = await response.json(); const users = await response.json();
const ts = selectElement.tomselect;
if (!ts) {
selectElement.innerHTML = `<option value="">${appTranslations.selectUser}</option>`; selectElement.innerHTML = `<option value="">${appTranslations.selectUser}</option>`;
users.forEach(u => {
users.forEach(usr => { const opt = document.createElement('option');
const text = usr.uid; opt.value = u.uid;
const option = document.createElement('option'); opt.textContent = u.uid;
option.value = usr.uid; selectElement.appendChild(opt);
option.textContent = text;
if (selectedValue && selectedValue === usr.uid) {
option.selected = true;
}
selectElement.appendChild(option);
}); });
return;
}
ts.clearOptions();
ts.addOption(users.map(u => ({ value: u.uid, text: u.uid })));
ts.refreshOptions(false);
if (selectedValue) ts.setValue(selectedValue);
} catch (err) { } catch (err) {
console.error('Error loading users:', err); console.error('Error loading users:', err);
showToast(appTranslations.errorLoadingUsers, 'danger'); showToast(appTranslations.errorLoadingUsers, 'danger');