Select and Select2 Fields
TIP
Default Select Boxes are replaced via the Select2 Library – Version4.1.0-rc.0. Use the class js-select2 (JS) if you want to use Select2, or just c-select for the look.
- search is hidden per default, add the attribute
data-searchto enable it - if you want to allow custom options tags, add
data-custom-options
Default Version
.c-float-container.-has-select > .c-select.js-select2
TIP
Search is hidden per default when initialized by .js-select2
INFO
With Search Field in Dropdown
.js-select2[data-search]
INFO
Multiple Select
<select multiple ..
INFO
Option Groups
optgroup > option ..
INFO
Without Select2
.c-select
INFO
Light Version
.c-float-container.-has-select.-light > .c-select.-light
INFO
Disabled Select
.c-select[disabled]
INFO
Disabled Results
<option value='...' disabled>
INFO
Hidden Disabled Results
.js-select2[data-hide-disabled]
INFO
Link Styling with Icons
.js-select2.c-select.-is-link[.-small]
INFO
Select with Icon
.c-float-container.-select.-has-icon > .c-float-container__icon
INFO
Select with Info-Tooltip
.c-float-container.-input.-has-icon.-icon-right > .c-input.-icon
INFO
Select With Inline-Btn
INFO
AJAX Example (with Search and Clear-Button)
TIP
- allowClear needs a placeholder and placeholder need a corresponding option value (which cannot be an empty string, but can be a single space)
- Select2 AJAX support: See select2.org/data-sources/ajax
// import {Select} from "./Select";
// Select.init();
//
// const $select = $(".js-example");
// const mergedSelectOptions = $.extend(true, {}, Select.getOptionsWithTags("Unbekannter Bestandteil"), {
// ajax: {
// url: "https://jsonplaceholder.typicode.com/posts/", // aktuell Dummy-API, gerne auch andere Endpunkte ausprobieren!
// dataType: "json",
// allowClear: true, // always use in combination with data-placeholder attribute on element
// processResults: function (data) {
// /**
// * Wir setzen "text" (also den Text in der Zeile) hier auf den Namen des Elements.
// * Siehe https://select2.org/data-sources/formats
// * Da die Elemente von der Dummy-API ohnehin schon ein Attribut "id" haben, müssen wir das nicht extra setzen.
// */
// data.forEach(resultObj => {
// resultObj.info = resultObj.body;
// resultObj.text = resultObj.title;
// Select.addOptionTagsIfNotExists($select, resultObj);
// }
// );
// return {
// results: data
// };
// }
// }
// });
//
// $select.each(function () {
// Select.apply($(this), mergedSelectOptions);
// });INFO
Fake Select2
INFO
Word Wrapping Demo
INFO
height: autoheight: autoSettings and API
TIP
Form.init() to activate Floating Labels. See Forms Module.
Select.init() to apply default settings and initialize select2 on all js-select2 selects.
Use Select.apply($select, options); to apply your own options on a specific select-field. You can also extend the default options.
Available default options are:
Select.getDefaultOptions()@returns default settings for standard select-dropdownsSelect.getOptionsWithSearch()@returns extended settings for select-dropdowns with searchSelect.getOptionsWithTags(unknownTerm)@returns extended settings for select-dropdowns with search and custom tags, @param {String} unknownTerm - optional parameter that describes the type of the "unknown custom tag" that is added
const settings = {
selector: {
selectField: ".js-select2",
searchField: ".select2-search__field"
},
class: {
jsClass: "js-select2",
defaultClass: "c-select",
isDisabled: "-disabled",
hideDisabledOptions: '-hide-disabled-options',
hiddenAccessible: "select2-hidden-accessible"
}
};