Skip to content

Commit e18fa1b

Browse files
author
Zubair
committed
fix callback using this.current.
add example.
1 parent 1c88460 commit e18fa1b

8 files changed

Lines changed: 97 additions & 51 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,20 +2863,23 @@ define('select2/data/maximumSelectionLength',[
28632863

28642864
MaximumSelectionLength.prototype.query =
28652865
function (decorated, params, callback) {
2866+
var self = this;
28662867

2867-
var count = this.$element.val() != null ? this.$element.val().length : 0;
2868-
if (count >= this.maximumSelectionLength) {
2869-
this.trigger('results:message', {
2870-
message: 'maximumSelected',
2871-
args: {
2872-
maximum: this.maximumSelectionLength
2868+
this.current(function (currentData) {
2869+
var count = currentData != null ? currentData.length : 0;
2870+
if (count >= self.maximumSelectionLength) {
2871+
self.trigger('results:message', {
2872+
message: 'maximumSelected',
2873+
args: {
2874+
maximum: self.maximumSelectionLength
2875+
}
2876+
});
2877+
return;
28732878
}
2879+
decorated.call(self, params, callback);
28742880
});
28752881

2876-
return;
2877-
}
28782882

2879-
decorated.call(this, params, callback);
28802883
};
28812884

28822885
return MaximumSelectionLength;

dist/js/select2.amd.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,20 +2863,23 @@ define('select2/data/maximumSelectionLength',[
28632863

28642864
MaximumSelectionLength.prototype.query =
28652865
function (decorated, params, callback) {
2866+
var self = this;
28662867

2867-
var count = this.$element.val() != null ? this.$element.val().length : 0;
2868-
if (count >= this.maximumSelectionLength) {
2869-
this.trigger('results:message', {
2870-
message: 'maximumSelected',
2871-
args: {
2872-
maximum: this.maximumSelectionLength
2868+
this.current(function (currentData) {
2869+
var count = currentData != null ? currentData.length : 0;
2870+
if (count >= self.maximumSelectionLength) {
2871+
self.trigger('results:message', {
2872+
message: 'maximumSelected',
2873+
args: {
2874+
maximum: self.maximumSelectionLength
2875+
}
2876+
});
2877+
return;
28732878
}
2879+
decorated.call(self, params, callback);
28742880
});
28752881

2876-
return;
2877-
}
28782882

2879-
decorated.call(this, params, callback);
28802883
};
28812884

28822885
return MaximumSelectionLength;

dist/js/select2.full.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12398,20 +12398,23 @@ define('select2/data/maximumSelectionLength',[
1239812398

1239912399
MaximumSelectionLength.prototype.query =
1240012400
function (decorated, params, callback) {
12401+
var self = this;
1240112402

12402-
var count = this.$element.val() != null ? this.$element.val().length : 0;
12403-
if (count >= this.maximumSelectionLength) {
12404-
this.trigger('results:message', {
12405-
message: 'maximumSelected',
12406-
args: {
12407-
maximum: this.maximumSelectionLength
12403+
this.current(function (currentData) {
12404+
var count = currentData != null ? currentData.length : 0;
12405+
if (count >= self.maximumSelectionLength) {
12406+
self.trigger('results:message', {
12407+
message: 'maximumSelected',
12408+
args: {
12409+
maximum: self.maximumSelectionLength
12410+
}
12411+
});
12412+
return;
1240812413
}
12414+
decorated.call(self, params, callback);
1240912415
});
1241012416

12411-
return;
12412-
}
1241312417

12414-
decorated.call(this, params, callback);
1241512418
};
1241612419

1241712420
return MaximumSelectionLength;

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3291,20 +3291,23 @@ define('select2/data/maximumSelectionLength',[
32913291

32923292
MaximumSelectionLength.prototype.query =
32933293
function (decorated, params, callback) {
3294+
var self = this;
32943295

3295-
var count = this.$element.val() != null ? this.$element.val().length : 0;
3296-
if (count >= this.maximumSelectionLength) {
3297-
this.trigger('results:message', {
3298-
message: 'maximumSelected',
3299-
args: {
3300-
maximum: this.maximumSelectionLength
3296+
this.current(function (currentData) {
3297+
var count = currentData != null ? currentData.length : 0;
3298+
if (count >= self.maximumSelectionLength) {
3299+
self.trigger('results:message', {
3300+
message: 'maximumSelected',
3301+
args: {
3302+
maximum: self.maximumSelectionLength
3303+
}
3304+
});
3305+
return;
33013306
}
3307+
decorated.call(self, params, callback);
33023308
});
33033309

3304-
return;
3305-
}
33063310

3307-
decorated.call(this, params, callback);
33083311
};
33093312

33103313
return MaximumSelectionLength;

dist/js/select2.min.js

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

docs/examples.html

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,35 @@ <h2>Example code</h2>
6767
<script type="text/x-example-code" class="js-code-multiple">
6868
$(".js-example-basic-multiple").select2();
6969

70+
<select class="js-example-basic-multiple" multiple="multiple">
71+
<option value="AL">Alabama</option>
72+
...
73+
<option value="WY">Wyoming</option>
74+
</select>
75+
</script>
76+
</div>
77+
</section>
78+
79+
<section id="multiple_max" class="row">
80+
<div class="col-md-4">
81+
<h1>Limited Multiple select boxes</h1>
82+
<p>Select2 multi-value select boxes can set restrictions regarding the maximum number of options selected.
83+
The select below is declared with the <code>multiple</code> attribute with <code>maxSelectionLength</code> in the select2 options</p>
84+
85+
<p>
86+
<select class="js-example-basic-multiple-limit js-states form-control" multiple="multiple"></select>
87+
</p>
88+
</div>
89+
<div class="col-md-8">
90+
<h2>Example code</h2>
91+
92+
<pre data-fill-from=".js-code-multiple-limit"></pre>
93+
94+
<script type="text/x-example-code" class="js-code-multiple-limit">
95+
$(".js-example-basic-multiple-limit").select2({
96+
maximumSelectionLength: 2
97+
});
98+
7099
<select class="js-example-basic-multiple" multiple="multiple">
71100
<option value="AL">Alabama</option>
72101
...
@@ -812,6 +841,7 @@ <h2>Example code</h2>
812841
function (Select2, Utils, oldMatcher) {
813842
var $basicSingle = $(".js-example-basic-single");
814843
var $basicMultiple = $(".js-example-basic-multiple");
844+
var $limitMultiple = $(".js-example-basic-multiple-limit");
815845

816846
var $dataArray = $(".js-example-data-array");
817847
var $dataArraySelected = $(".js-example-data-array-selected");
@@ -830,7 +860,10 @@ <h2>Example code</h2>
830860
var $language = $(".js-example-language");
831861

832862
$basicSingle.select2();
833-
$basicMultiple.select2()
863+
$basicMultiple.select2();
864+
$limitMultiple.select2({
865+
maximumSelectionLength: 2
866+
});
834867

835868
$dataArray.select2({
836869
data: data

src/js/select2/data/maximumSelectionLength.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ define([
99

1010
MaximumSelectionLength.prototype.query =
1111
function (decorated, params, callback) {
12+
var self = this;
1213

13-
var count = this.current() != null ? this.current().length : 0;
14-
if (count >= this.maximumSelectionLength) {
15-
this.trigger('results:message', {
16-
message: 'maximumSelected',
17-
args: {
18-
maximum: this.maximumSelectionLength
14+
this.current(function (currentData) {
15+
var count = currentData != null ? currentData.length : 0;
16+
if (count >= self.maximumSelectionLength) {
17+
self.trigger('results:message', {
18+
message: 'maximumSelected',
19+
args: {
20+
maximum: self.maximumSelectionLength
21+
}
22+
});
23+
return;
1924
}
25+
decorated.call(self, params, callback);
2026
});
21-
22-
return;
23-
}
24-
25-
decorated.call(this, params, callback);
2627
};
2728

2829
return MaximumSelectionLength;

0 commit comments

Comments
 (0)