Compare commits

...

4 Commits

Author SHA1 Message Date
Arthur
5359ae4fc2 fix selected item sorting 2026-01-14 14:43:42 -08:00
Arthur
05619a9745 fix selected item sorting 2026-01-14 14:35:45 -08:00
Arthur
6317bcc657 update to simpler sorting 2026-01-14 14:29:08 -08:00
Arthur
20f52153a4 update to simpler sorting 2026-01-14 14:21:43 -08:00
4 changed files with 15 additions and 9 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -75,6 +75,7 @@ export class DynamicTomSelect extends TomSelect {
load(value: string) { load(value: string) {
const self = this; const self = this;
// Save current selection before clearing
const currentValue = self.getValue(); const currentValue = self.getValue();
// Automatically clear any cached options. (Only options included // Automatically clear any cached options. (Only options included
@@ -84,6 +85,7 @@ export class DynamicTomSelect extends TomSelect {
// Clear user_options to prevent the pre-selected option from being treated specially // Clear user_options to prevent the pre-selected option from being treated specially
(self as any).user_options = {}; (self as any).user_options = {};
// Populate the null option (if any) if not searching
if (self.nullOption && !value) { if (self.nullOption && !value) {
self.addOption(self.nullOption); self.addOption(self.nullOption);
} }
@@ -97,28 +99,29 @@ export class DynamicTomSelect extends TomSelect {
addClasses(self.wrapper, self.settings.loadingClass); addClasses(self.wrapper, self.settings.loadingClass);
self.loading++; self.loading++;
// Make the API request
fetch(url) fetch(url)
.then(response => response.json()) .then(response => response.json())
.then(apiData => { .then(apiData => {
const results: Dict[] = apiData.results; const results: Dict[] = apiData.results;
// Add options and set $order to preserve API response order // Add options and manually set $order to ensure correct sorting
results.forEach((result, index) => { results.forEach((result, index) => {
const option = self.getOptionFromData(result); const option = self.getOptionFromData(result);
self.addOption(option); self.addOption(option);
// Set $order after addOption() to override any special handling of pre-selected items
const key = option[self.settings.valueField as string] as string; const key = option[self.settings.valueField as string] as string;
if (self.options[key]) { if (self.options[key]) {
(self.options[key] as any).$order = index; (self.options[key] as any).$order = index;
} }
}); });
if (self.loading > 0) { self.loading--;
self.loading--; if (self.loading === 0) {
if (self.loading === 0) { self.wrapper.classList.remove(self.settings.loadingClass as string);
self.wrapper.classList.remove(self.settings.loadingClass as string);
}
} }
// Restore the current selection
if (currentValue && !self.items.includes(currentValue as string)) { if (currentValue && !self.items.includes(currentValue as string)) {
self.items.push(currentValue as string); self.items.push(currentValue as string);
} }

View File

@@ -49,6 +49,9 @@ export function initDynamicSelects(): void {
labelField: LABEL_FIELD, labelField: LABEL_FIELD,
maxOptions: MAX_OPTIONS, maxOptions: MAX_OPTIONS,
// Preserve API response order
sortField: '$order',
// Disable local search (search is performed on the backend) // Disable local search (search is performed on the backend)
searchField: [], searchField: [],