diff --git a/docs/examples.html b/docs/examples.html index 38e33989..7f798207 100644 --- a/docs/examples.html +++ b/docs/examples.html @@ -630,7 +630,7 @@ $(".js-example-tokenizer").select2({ This custom matcher uses a compatibility module that is only bundled in the - full version of Select2. You also + full version of Select2. You also have the option of using a more complex matcher.
diff --git a/docs/options.html b/docs/options.html index e1cb7193..e1b81000 100644 --- a/docs/options.html +++ b/docs/options.html @@ -423,7 +423,7 @@ placeholder: { the one below. -+language: { // You can find all of the options in the language files provided in the // build. They all must be functions that return the string that should be @@ -517,7 +517,7 @@ language: { custom options that Select2 will intercept, allowing you to customize the request as it is being made. -+ajax: { // The number of milliseconds to wait for the user to stop typing before // issuing the ajax request. @@ -615,6 +615,70 @@ ajax: { This is the same as how array data works, and has similar limitations. + ++ Change how options are matched when searching +
+ ++ When users filter down the results by entering search terms into the + search box, Select2 uses an internal "matcher" to match search terms to + results. When a remote data set is used, Select2 expects that the + returned results have already been filtered. +
+ +
matcher
+ params
and the
+ data
object.
+
+ Select2 will pass the individual data objects that have been passed back
+ from the data adapter into the matcher
individually to
+ determine if they should be displayed. Only the first-level objects will
+ be passed in, so if you are working with nested data, you need to
+ match those individually.
+
+matcher: function (params, data) { + // If there are no search terms, return all of the data + if ($.trim(params.term) === '') { + return data; + } + + // `params.term` should be the term that is used for searching + // `data.text` is the text that is displayed for the data object + if (data.text.indexOf(params.term) > -1) { + var modifiedData = $.extend({}, data, true); + modifiedData.text += ' (matched)'; + + // You can return modified objects from here + // This includes matching the `children` how you want in nested data sets + return modifiedData; + } + + // Return `null` if the term should not be displayed + return null; +} ++ +
+ This allows for more advanced matching when working with nested objects, + allowing you to handle them however you want. For those who are not + looking to implement highly customized matching, but instead are just + looking to change the matching algorithm for the text, a + compatibility modules has been created to + make it easier. +
select2/compat
.
+ + Added in Select2 4.0.0. + This method was added to make upgrading easier from earlier versions of + Select2. +
+ +
+ During the Select2 4.0.0 release, the
+ matcher
function was changed to allow for more complex
+ matching of nested objects.
+
matcher
+ term
and the data object
+ text
.
+ oldMatcher
+
+ The custom matcher example provides a
+ guide for how to use this in your own application. For those upgrading
+ from older versions of Select2, you just need to wrap your old
+ matcher
with this function to maintain compatibility.
+
initSelection