// // For select 2 // $(".select2").select2(); // Single Select Placeholder $("#select2-with-placeholder").select2({ placeholder: "Select a state", allowClear: true, }); // // Hiding the search box // $("#select2-search-hide").select2({ minimumResultsForSearch: Infinity, }); // // Select With Icon // $("#select2-with-icons").select2({ minimumResultsForSearch: Infinity, templateResult: iconFormat, templateSelection: iconFormat, escapeMarkup: function (es) { return es; }, }); function iconFormat(icon) { var originalOption = icon.element; if (!icon.id) { return icon.text; } var $icon = "" + icon.text; return $icon; } // // Limiting the number of selections // $("#select2-max-length").select2({ maximumSelectionLength: 3, placeholder: "Select only maximum 3 items", }); // //multiple-select2-with-icons // $("#multiple-select2-with-icons").select2({ minimumResultsForSearch: Infinity, templateResult: iconFormat, templateSelection: iconFormat, escapeMarkup: function (es) { return es; }, }); function iconFormat(icon) { var originalOption = icon.element; if (!icon.id) { return icon.text; } var $icon = "" + icon.text; return $icon; } // // DOM Events // var $selectEvent = $(".js-events"); $selectEvent.select2({ placeholder: "DOM Events", }); $selectEvent.on("select2:open", function (e) { alert("Open Event Fired."); }); $selectEvent.on("select2:close", function (e) { alert("Close Event Fired."); }); $selectEvent.on("select2:select", function (e) { alert("Select Event Fired."); }); $selectEvent.on("select2:unselect", function (e) { alert("Unselect Event Fired."); }); $selectEvent.on("change", function (e) { alert("Change Event Fired."); }); // // Programmatic access // var $select = $(".js-programmatic").select2(); var $selectMulti = $(".js-programmatic-multiple").select2(); $selectMulti.select2({ placeholder: "Programmatic Events", }); $(".js-programmatic-set-val").on("click", function () { $select.val("NM").trigger("change"); }); $(".js-programmatic-open").on("click", function () { $select.select2("open"); }); $(".js-programmatic-close").on("click", function () { $select.select2("close"); }); $(".js-programmatic-init").on("click", function () { $select.select2(); }); $(".js-programmatic-destroy").on("click", function () { $select.select2("destroy"); }); $(".js-programmatic-multi-set-val").on("click", function () { $selectMulti.val(["UT", "NM"]).trigger("change"); }); $(".js-programmatic-multi-clear").on("click", function () { $selectMulti.val(null).trigger("change"); }); // // Loading array data // var data = [ { id: 0, text: "Web Designer" }, { id: 1, text: "Mobile App Developer" }, { id: 2, text: "Graphics Designer" }, { id: 3, text: "Hacker" }, { id: 4, text: "Animation Designer" }, ]; $("#select2-data-array").select2({ data: data, }); // // Loading remote data // $(".select2-data-ajax").select2({ placeholder: "Loading remote data", ajax: { url: "http://api.github.com/search/repositories", dataType: "json", delay: 250, data: function (params) { return { q: params.term, // search term page: params.page, }; }, processResults: function (data, params) { // parse the results into the format expected by Select2 // since we are using custom formatting functions we do not need to // alter the remote JSON data, except to indicate that infinite // scrolling can be used params.page = params.page || 1; return { results: data.items, pagination: { more: params.page * 30 < data.total_count, }, }; }, cache: true, }, escapeMarkup: function (markup) { return markup; }, // let our custom formatter work minimumInputLength: 1, templateResult: formatRepo, // omitted for brevity, see the source of this page templateSelection: formatRepoSelection, // omitted for brevity, see the source of this page }); function formatRepo(repo) { if (repo.loading) return repo.text; var markup = "