Skip to content

Commit 6375e2e

Browse files
committed
Working on searching results
1 parent fa3495d commit 6375e2e

17 files changed

Lines changed: 436 additions & 161 deletions

dist/css/select2.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
left: -100000px;
4242
width: 100%;
4343
z-index: 100; }
44+
.select2-container .dropdown .search {
45+
display: block;
46+
padding: 4px; }
47+
.select2-container .dropdown .search input {
48+
outline: 0;
49+
padding: 4px;
50+
width: 100%; }
4451
.select2-container .dropdown .results {
4552
display: block; }
4653
.select2-container .dropdown .results .options {
@@ -85,6 +92,8 @@
8592
.select2-container.select2-theme-default.open .selection .single-select, .select2-container.select2-theme-default.open .selection .multiple-select {
8693
border-bottom-left-radius: 0;
8794
border-bottom-right-radius: 0; }
95+
.select2-container.select2-theme-default .dropdown .search input {
96+
border: 1px solid #aaa; }
8897
.select2-container.select2-theme-default .dropdown .results {
8998
max-height: 200px;
9099
overflow-y: scroll; }

dist/css/select2.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.amd.full.js

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ define('select2/results',[
193193
var $option = $(this);
194194
var item = $option.data('data');
195195

196-
if (selectedIds.indexOf(item.id.toString()) > -1) {
196+
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
197197
$option.addClass('selected');
198198
}
199199
});
@@ -205,7 +205,7 @@ define('select2/results',[
205205
'<li class="option highlightable selectable"></li>'
206206
);
207207

208-
if (data.children && data.children.length > 0) {
208+
if (data.children) {
209209
$option.addClass('group').removeClass('highlightable selectable');
210210

211211
var $label = $('<strong class="group-label"></strong>');
@@ -235,6 +235,10 @@ define('select2/results',[
235235
$option.removeClass('selectable highlightable').addClass('disabled');
236236
}
237237

238+
if (data.id == null) {
239+
$option.removeClass('selectable highlightable');
240+
}
241+
238242
$option.data('data', data);
239243

240244
return $option;
@@ -292,28 +296,6 @@ define('select2/results',[
292296
return Results;
293297
});
294298

295-
define('select2/dropdown',[
296-
'./utils'
297-
], function (Utils) {
298-
function Dropdown ($element, options) {
299-
this.$element = $element;
300-
}
301-
302-
Utils.Extend(Dropdown, Utils.Observable);
303-
304-
Dropdown.prototype.render = function () {
305-
var $dropdown = $(
306-
'<span class="dropdown">' +
307-
'<span class="results"></span>' +
308-
'</span>'
309-
);
310-
311-
return $dropdown;
312-
};
313-
314-
return Dropdown;
315-
});
316-
317299
define('select2/selection/single',[
318300
'../utils'
319301
], function (Utils) {
@@ -605,7 +587,6 @@ define('select2/data/select',[
605587
};
606588
} else if ($option.is('optgroup')) {
607589
data = {
608-
id: -1,
609590
text: $option.attr('label'),
610591
children: []
611592
};
@@ -654,7 +635,7 @@ define('select2/data/select',[
654635
return match;
655636
}
656637

657-
if (data.text.indexOf(params.term) > -1) {
638+
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
658639
return match;
659640
}
660641

@@ -769,19 +750,83 @@ define('select2/data/ajax',[
769750
return AjaxAdapter;
770751
});
771752

753+
define('select2/dropdown',[
754+
'./utils'
755+
], function (Utils) {
756+
function Dropdown ($element, options) {
757+
this.$element = $element;
758+
}
759+
760+
Utils.Extend(Dropdown, Utils.Observable);
761+
762+
Dropdown.prototype.render = function () {
763+
var $dropdown = $(
764+
'<span class="dropdown">' +
765+
'<span class="results"></span>' +
766+
'</span>'
767+
);
768+
769+
return $dropdown;
770+
};
771+
772+
Dropdown.prototype.bind = function (container, $container) {
773+
// Can be implemented in subclasses
774+
};
775+
776+
return Dropdown;
777+
});
778+
779+
define('select2/dropdown/search',[
780+
781+
], function () {
782+
function Search () { }
783+
784+
Search.prototype.render = function (decorated) {
785+
var $rendered = decorated.call(this);
786+
787+
var $search = $(
788+
'<span class="search">' +
789+
'<input type="search" name="search" />' +
790+
'</span>'
791+
);
792+
793+
this.$search = $search.find('input');
794+
795+
$rendered.prepend($search);
796+
797+
return $rendered;
798+
};
799+
800+
Search.prototype.bind = function (decorated, container, $container) {
801+
decorated.call(this, container, $container);
802+
803+
this.$search.on('keyup', function () {
804+
container.trigger('query', {
805+
term: $(this).val()
806+
});
807+
});
808+
};
809+
810+
return Search;
811+
});
812+
772813
define('select2/options',[
773814
'./results',
774815

775-
'./dropdown',
776-
777816
'./selection/single',
778817
'./selection/multiple',
779818

819+
'./utils',
820+
780821
'./data/select',
781822
'./data/array',
782-
'./data/ajax'
783-
], function (ResultsList, Dropdown, SingleSelection, MultipleSelection,
784-
SelectData, ArrayData, AjaxData) {
823+
'./data/ajax',
824+
825+
'./dropdown',
826+
'./dropdown/search'
827+
], function (ResultsList, SingleSelection, MultipleSelection, Utils,
828+
SelectData, ArrayData, AjaxData,
829+
Dropdown, Search) {
785830
function Options (options) {
786831
this.options = options;
787832

@@ -793,8 +838,10 @@ define('select2/options',[
793838
this.dataAdapter = this.dataAdapter || SelectData;
794839
}
795840

841+
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
842+
796843
this.resultsAdapter = ResultsList;
797-
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
844+
this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
798845
this.selectionAdapter = options.selectionAdapter;
799846

800847
if (this.selectionAdapter == null) {
@@ -864,6 +911,8 @@ define('select2/core',[
864911

865912
this.data.bind(this, $container);
866913
this.selection.bind(this, $container);
914+
915+
this.dropdown.bind(this, $container);
867916
this.results.bind(this, $container);
868917

869918
this.$element.on('change', function () {
@@ -906,10 +955,14 @@ define('select2/core',[
906955
});
907956
});
908957

909-
this.data.query({}, function (data) {
910-
self.results.trigger('results:all', data);
958+
this.on('query', function (params) {
959+
this.data.query(params, function (data) {
960+
self.results.trigger('results:all', data);
961+
});
911962
});
912963

964+
this.trigger('query', {});
965+
913966
// Hide the original select
914967

915968
$element.hide();

0 commit comments

Comments
 (0)