/** * Builders to use Select2 in our templates. * * This comes with two flavours : local data or remote data. * * # Local data source * * To use local data source, you must define an array * in your JS code, having the fields `id` and `text`. * * ```js * const data = [ * {id: 1, text: "foo"}, * {id: 2, text: "bar"}, * ]; * document.addEventListener("DOMContentLoaded", () => sithSelect2({ * element: document.getElementById("select2-input"), * dataSource: localDataSource(data) * })); * ``` * * You can also define a callback that return ids to exclude : * * ```js * const data = [ * {id: 1, text: "foo"}, * {id: 2, text: "bar"}, * {id: 3, text: "to exclude"}, * ]; * document.addEventListener("DOMContentLoaded", () => sithSelect2({ * element: document.getElementById("select2-input"), * dataSource: localDataSource(data, { * excluded: () => data.filter((i) => i.text === "to exclude").map((i) => parseInt(i)) * }) * })); * ``` * * # Remote data source * * Select2 with remote data sources are similar to those with local * data, but with some more parameters, like `resultConverter`, * which takes a callback that must return a `Select2Object`. * * ```js * import { makeUrl } from "#core:utils/api"; * import {userSearchUsers } from "#openapi" * document.addEventListener("DOMContentLoaded", () => sithSelect2({ * element: document.getElementById("select2-input"), * dataSource: remoteDataSource(await makeUrl(userSearchUsers), { * excluded: () => [1, 2], // exclude users 1 and 2 from the search * resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)} * }) * })); * ``` * * # Overrides * * Dealing with a select2 may be complex. * That's why, when defining a select, * you may add an override parameter, * in which you can declare any parameter defined in the * Select2 documentation. * * ```js * import { makeUrl } from "#core:utils/api"; * import {userSearchUsers } from "#openapi" * document.addEventListener("DOMContentLoaded", () => sithSelect2({ * element: document.getElementById("select2-input"), * dataSource: remoteDataSource(await makeUrl(userSearchUsers), { * resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)} * overrides: { * delay: 500 * } * }) * })); * ``` * * # Caveats with exclude * * With local data source, select2 evaluates the data only once. * Thus, modify the exclude after the initialisation is a no-op. * * With remote data source, the exclude list will be evaluated * after each api response. * It makes it possible to bind the data returned by the callback * to some reactive data, thus making the exclude list dynamic. * * # Images * * Sometimes, you would like to display an image besides * the text on the select items. * In this case, fill the `pictureGetter` option : * * ```js * import { makeUrl } from "#core:utils/api"; * import {userSearchUsers } from "#openapi" * document.addEventListener("DOMContentLoaded", () => sithSelect2({ * element: document.getElementById("select2-input"), * dataSource: remoteDataSource(await makeUrl(userSearchUsers), { * resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)} * }) * pictureGetter: (user) => user.profilePict, * })); * ``` * * # Binding with alpine * * You can declare your select2 component in an Alpine data. * * ```html *
*