Skip to content

Commit

Permalink
Implement remote data on select form
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Nov 26, 2024
1 parent ccc5ff2 commit 7eab94f
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/Front/Library/Utility/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Page from '../Loader/Page';
import { MaskInput } from "maska"
import Root from '../Dom/Root';
import Arrays from './Arrays';
import { json } from 'stream/consumers';

/**
* Form
Expand Down Expand Up @@ -1957,9 +1958,100 @@ export default class Form {

}

// Declare potential add option
let addOption:(()=>void)|null = null;

// Check if
if(inputEl.dataset && "selectRemote" in inputEl.dataset && inputEl.dataset.selectRemote && UtilityStrings.isJson(inputEl.dataset.selectRemote)){

// Decode selectRemote
let remoteData = JSON.parse(inputEl.dataset.selectRemote);

// Set value
option.valueField = remoteData.value;

// Set label
option.labelField = remoteData.label;

// Set search
option.searchField = remoteData.search;

// option.allowEmptyOption = true;

// Set load
option.load = (selectQuery, callback) => {

// New query
let query = new Crazyrequest(
remoteData.url,
{
method: "get",
cache: false,
responseType: "json",
from: "internal"
}
);

// Rerurn result
query.fetch(

// Fetch all

).then(
value => {

// Callback with value retrieve
callback(value.results);

}
);

};

// Prepare add option
addOption = () => {

// New query
let query = new Crazyrequest(
remoteData.url,
{
method: "get",
cache: false,
responseType: "json",
from: "internal"
}
);

// Rerurn result
query.fetch(

// Fetch all

).then(
value => {

// Add options to tom
selectInstance.addOptions(value.results);

}
);

}

}

// Init maska
let selectInstance = new TomSelect(inputEl, option);

// Check addOption is callable
if(addOption !== null && typeof addOption === "function"){

// Run function
// @ts-ignore
addOption();

}

}

}
Expand Down

0 comments on commit 7eab94f

Please sign in to comment.