/* ------------------------------------------------------------------------------ * * # Select2 selects * * Specific JS code additions for form_select2.html page * * Version: 1.1 * Latest update: Nov 20, 2015 * * ---------------------------------------------------------------------------- */ $(function() { // Basic examples // ------------------------------ // Default initialization $('.select').select2({ minimumResultsForSearch: Infinity }); // Select with search $('.select-search').select2(); // Fixed width. Single select $('.select-fixed-single').select2({ minimumResultsForSearch: Infinity, width: 250 }); // Fixed width. Multiple selects $('.select-fixed-multiple').select2({ minimumResultsForSearch: Infinity, width: 400 }); // Styling options // ------------------------------ // Custom results color $('.select-results-color').select2({ containerCssClass: 'bg-teal-400' }); // Custom menu color $('.select-menu-color').select2({ dropdownCssClass: 'bg-teal-400' }); // Custom menu and results color $('.select-custom-colors').select2({ containerCssClass: 'bg-indigo-400', dropdownCssClass: 'bg-indigo-400' }); // Combine custom colors in multiple $('.select-menu2-color').select2({ containerCssClass: 'bg-indigo-400', dropdownCssClass: 'bg-indigo-400' }); // Menu border and text color $('.select-border-color').select2({ dropdownCssClass: 'border-primary', containerCssClass: 'border-primary text-primary-700' }); // Sizing options // ------------------------------ // Large $('.select-size-lg').select2({ containerCssClass: 'select-lg' }); // Small $('.select-size-sm').select2({ containerCssClass: 'select-sm' }); // Mini $('.select-size-xs').select2({ containerCssClass: 'select-xs' }); // Advanced examples // ------------------------------ // Minimum input length $(".select-minimum").select2({ minimumInputLength: 2, minimumResultsForSearch: Infinity }); // Allow clear selection $('.select-clear').select2({ placeholder: "Select a State", allowClear: true }); // Tagging support $(".select-multiple-tags").select2({ tags: true }); // Maximum input length $(".select-multiple-maximum-length").select2({ tags: true, maximumInputLength: 5 }); // Tokenization $(".select-multiple-tokenization").select2({ tags: true, tokenSeparators: [",", " "] }); // Maximum selection $(".select-multiple-limited").select2({ maximumSelectionLength: 3 }); // Maximum selections allowed $('.select-multiple-maximum').select2({ maximumSelectionSize: 3 }); // // Drag and drop selected items // // Initialize with tags $(".select-multiple-drag").select2({ containerCssClass: 'sortable-target' }); // Add jQuery UI Sortable support $(".sortable-target .select2-selection__rendered").sortable({ containment: '.sortable-target', items: '.select2-selection__choice:not(.select2-search--inline)' }); // // Single select with icons // // Format icon function iconFormat(icon) { var originalOption = icon.element; if (!icon.id) { return icon.text; } var $icon = "" + icon.text; return $icon; } // Initialize with options $(".select-icons").select2({ templateResult: iconFormat, minimumResultsForSearch: Infinity, templateSelection: iconFormat, escapeMarkup: function(m) { return m; } }); // // Customize matched results // // Setup matcher function matchStart (term, text) { if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) { return true; } return false; } // Initialize $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) { $(".select-matched-customize").select2({ minimumResultsForSearch: Infinity, placeholder: "Select a State", matcher: oldMatcher(matchStart) }); }); // // Loading arrays of data // // Data var array_data = [ {id: 0, text: 'enhancement'}, {id: 1, text: 'bug'}, {id: 2, text: 'duplicate'}, {id: 3, text: 'invalid'}, {id: 4, text: 'wontfix'} ]; // Loading array data $(".select-data-array").select2({ placeholder: "Click to load data", minimumResultsForSearch: Infinity, data: array_data }); // // Loading remote data // // Format displayed data function formatRepo (repo) { if (repo.loading) return repo.text; var markup = "