Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion DynamicSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ class DynamicSelect {
}
}

// New refresh method with selection of the new element
refresh(data, selectedValue = null) {
this.data = data; // Update the data

if (selectedValue) {
// Find the newly added element and mark it as selected
this.data.forEach(option => {
if (option.value === selectedValue) {
option.selected = true; // Mark this option as selected
} else {
option.selected = false; // Unselect others
}
});
}

const newElement = this._template(); // Create new HTML for the component
this.element.replaceWith(newElement); // Replace the current element with the new one
this.element = newElement; // Update the reference to the new element

this._updateSelected();
this._eventHandlers(); // Reattach event handlers
}

get selectedValue() {
let selected = this.data.filter(option => option.selected);
selected = selected.length ? selected[0].value : '';
Expand Down Expand Up @@ -180,4 +203,4 @@ class DynamicSelect {
}

}
document.querySelectorAll('[data-dynamic-select]').forEach(select => new DynamicSelect(select));
document.querySelectorAll('[data-dynamic-select]').forEach(select => new DynamicSelect(select));