Recommend this page to a friend! |
Classes of fathurrahman | mnTemplate | public/asset/vendor/select2/docs/pages/11.searching/docs.md | Download |
|
![]() title: Search taxonomy:
process:
never_cache_twig: trueA search box is added to the top of the dropdown automatically for select boxes where only a single option can be selected. The behavior and appearance of the search box can be easily customized with Select2. Customizing how results are matchedWhen users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the Select2 will pass each option as represented by its internal representation into this callback to determine if it should be displayed:
>>>> Matching grouped optionsOnly first-level objects will be passed in to the This example matches results only if the term appears in the beginning of the string: <div class="s2-example">
</div> <pre data-fill-from=".js-code-example-matcher"></pre> <script type="text/javascript" class="js-code-example-matcher"> function matchStart(params, data) { // If there are no search terms, return all of the data if ($.trim(params.term) === '') {
} // Skip if there is no 'children' property if (typeof data.children === 'undefined') {
} //
}); // If we matched any of the timezone group's children, then set the matched children on the group // and return the group object if (filteredChildren.length) {
} // Return $(".js-example-matcher-start").select2({ matcher: matchStart }); </script> >>> A compatibility module exists for using v3-style matcher callbacks. Minimum search term lengthSometimes when working with large data sets, it is more efficient to start filtering the results only when the search term is a certain length. This is very common when working with remote data sets, as it allows for only significant search terms to be used. You may set a minimum search term length by using the
Maximum search term lengthIn some cases, search terms need to be limited to a certain range. Select2 allows you to limit the length of the search term such that it does not exceed a certain length. You may limit the maximum length of search terms by using the
Limiting display of the search box to large result setsThe This option is useful for cases where local data is used with a small result set, and the search box would simply be a waste of screen real estate. Set this value to -1 to permanently hide the search box.
Hiding the search boxSingle selectFor single selects, Select2 allows you to hide the search box using the <div class="s2-example">
</div> <pre data-fill-from="#js-code-example-basic-hide-search"></pre> <script type="text/javascript" id="js-code-example-basic-hide-search"> $("#js-example-basic-hide-search").select2({
}); </script> Multi-selectFor multi-select boxes, there is no distinct search control. So, to disable search for multi-select boxes, you will need to set the <div class="s2-example">
</div> <pre data-fill-from="#js-code-example-basic-hide-search-multi"></pre> <script type="text/javascript" id="js-code-example-basic-hide-search-multi"> $('#js-example-basic-hide-search-multi').select2(); $('#js-example-basic-hide-search-multi').on('select2:opening select2:closing', function( event ) {
}); </script> See this issue for the source of this solution. |