1
0
mirror of synced 2025-02-03 21:59:24 +03:00

Rewrote the matcher example

This commit is contained in:
Kevin Brown 2014-11-05 11:09:55 -05:00
parent ed98443d47
commit 69fb428e01

View File

@ -304,6 +304,15 @@ $(".js-example-tags").select2({
<p> <p>
<select class="js-example-matcher-start js-states form-control"></select> <select class="js-example-matcher-start js-states form-control"></select>
</p> </p>
<p>
This custom matcher uses a
<a href="compatibility.html">compatibility module</a> that is only
bundled in the
<a href="getting-started#versions">full version of Select2</a>. You also
have the option of using a
<a href="options.html#matcher">more complex matcher</a>.
</p>
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<h2>Example code</h2> <h2>Example code</h2>
@ -311,40 +320,19 @@ $(".js-example-tags").select2({
<pre data-fill-from=".js-code-matcher-start"></pre> <pre data-fill-from=".js-code-matcher-start"></pre>
<script type="text/x-example-code" class="js-code-matcher-start"> <script type="text/x-example-code" class="js-code-matcher-start">
function matchStart (params, data) { function matchStart (term, text) {
var match = $.extend(true, {}, data); if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
if (data.children) {
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
var matches = matchStart(params, child);
// If there wasn't a match, remove the object in the array
if (matches === null) {
match.children.splice(c, 1);
}
}
if (match.children.length > 0) {
return match;
}
} }
if ($.trim(params.term) === '') { return false;
return match;
}
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
return match;
}
return null;
} }
$(".js-example-matcher-start").select2({ $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
matcher: matchStart $(".js-example-matcher-start").select2({
}) matcher: oldMatcher(matchStart)
})
});
</script> </script>
</div> </div>
</section> </section>