|
255 | 255 | formatResult: function (data) { return data.text; }, |
256 | 256 | formatSelection: function (data) { return data.text; }, |
257 | 257 | formatNoMatches: function () { return "No matches found"; }, |
258 | | - formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; } |
| 258 | + formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, |
| 259 | + minimumResultsForSearch: 0 |
259 | 260 | }, opts); |
260 | 261 |
|
261 | 262 | element = opts.element; |
|
382 | 383 |
|
383 | 384 | this.container.addClass("select2-dropdown-open").addClass("select2-container-active"); |
384 | 385 |
|
385 | | - this.updateResults(); |
386 | | - this.ensureHighlightVisible(); |
| 386 | + this.updateResults(true); |
387 | 387 | this.alignDropdown(); |
388 | 388 | this.dropdown.show(); |
389 | 389 | this.focusSearch(); |
|
511 | 511 | } |
512 | 512 | }; |
513 | 513 |
|
514 | | - AbstractSelect2.prototype.updateResults = function () { |
| 514 | + /** |
| 515 | + * @param initial whether or not this is the call to this method right after the dropdown has been opened |
| 516 | + */ |
| 517 | + AbstractSelect2.prototype.updateResults = function (initial) { |
515 | 518 | var search = this.search, results = this.results, opts = this.opts; |
516 | 519 |
|
517 | 520 | search.addClass("select2-active"); |
|
551 | 554 | var d = data.results[i]; |
552 | 555 | $(this).data("select2-data", d); |
553 | 556 | }); |
554 | | - this.postprocessResults(); |
| 557 | + this.postprocessResults(data, initial); |
555 | 558 | })}); |
556 | 559 | }; |
557 | 560 |
|
|
737 | 740 | } |
738 | 741 | }; |
739 | 742 |
|
740 | | - SingleSelect2.prototype.postprocessResults = function () { |
| 743 | + SingleSelect2.prototype.postprocessResults = function (data, initial) { |
741 | 744 | var selected = 0, self = this; |
| 745 | + |
| 746 | + // find the selected element in the result list |
| 747 | + |
742 | 748 | this.results.find(".select2-result").each(function (i) { |
743 | 749 | if ($(this).data("select2-data").id === self.opts.element.val()) { |
744 | 750 | selected = i; |
745 | 751 | return false; |
746 | 752 | } |
747 | 753 | }); |
| 754 | + |
| 755 | + // and highlight it |
| 756 | + |
748 | 757 | this.highlight(selected); |
| 758 | + |
| 759 | + // hide the search box if this is the first we got the results and there are a few of them |
| 760 | + |
| 761 | + if (initial===true) { |
| 762 | + this.search.toggle(data.results.length>=this.opts.minimumResultsForSearch); |
| 763 | + } |
| 764 | + |
749 | 765 | }; |
750 | 766 |
|
751 | 767 | SingleSelect2.prototype.onSelect = function (data) { |
|
898 | 914 | }); |
899 | 915 |
|
900 | 916 | this.updateSelection(data); |
901 | | - // preload all results |
902 | | - this.updateResults(); |
903 | 917 | } |
904 | 918 |
|
905 | 919 | // set the placeholder if necessary |
|
0 commit comments