mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 13:08:16 -06:00
Add support for setting a 'null' option
This commit is contained in:
parent
3874c8de9b
commit
6926b3709e
BIN
netbox/project-static/dist/netbox.js
vendored
BIN
netbox/project-static/dist/netbox.js
vendored
Binary file not shown.
BIN
netbox/project-static/dist/netbox.js.map
vendored
BIN
netbox/project-static/dist/netbox.js.map
vendored
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
import { RecursivePartial, TomInput, TomSettings } from 'tom-select/dist/types/types';
|
||||
import { RecursivePartial, TomInput, TomOption, TomSettings } from 'tom-select/dist/types/types';
|
||||
import { addClasses } from 'tom-select/src/vanilla'
|
||||
import queryString from 'query-string';
|
||||
import TomSelect from 'tom-select';
|
||||
@ -13,9 +13,9 @@ import { getElement, replaceAll } from '../../util';
|
||||
// Extends TomSelect to provide enhanced fetching of options via the REST API
|
||||
export class DynamicTomSelect extends TomSelect {
|
||||
|
||||
/*
|
||||
* Transitional code from APISelect
|
||||
*/
|
||||
public readonly nullOption: Nullable<TomOption> = null;
|
||||
|
||||
// Transitional code from APISelect
|
||||
private readonly queryParams: QueryFilter = new Map();
|
||||
private readonly staticParams: QueryFilter = new Map();
|
||||
private readonly dynamicParams: DynamicParamsMap = new DynamicParamsMap();
|
||||
@ -31,6 +31,16 @@ export class DynamicTomSelect extends TomSelect {
|
||||
// Glean the REST API endpoint URL from the <select> element
|
||||
this.api_url = this.input.getAttribute('data-url') as string;
|
||||
|
||||
// Set the null option (if any)
|
||||
const nullOption = this.input.getAttribute('data-null-option');
|
||||
if (nullOption) {
|
||||
let valueField = user_settings.valueField || 'value';
|
||||
let labelField = user_settings.labelField || 'text';
|
||||
this.nullOption = {}
|
||||
this.nullOption[valueField] = 'null';
|
||||
this.nullOption[labelField] = nullOption;
|
||||
}
|
||||
|
||||
// Populate static query parameters.
|
||||
this.getStaticParams();
|
||||
for (const [key, value] of this.staticParams.entries()) {
|
||||
@ -64,6 +74,11 @@ export class DynamicTomSelect extends TomSelect {
|
||||
addClasses(self.wrapper, self.settings.loadingClass);
|
||||
self.loading++;
|
||||
|
||||
// Populate the null option (if any) if not searching
|
||||
if (self.nullOption && !value) {
|
||||
self.addOption(self.nullOption);
|
||||
}
|
||||
|
||||
// Make the API request
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
|
Loading…
Reference in New Issue
Block a user