Skip to content

Commit 100015b

Browse files
committed
Added back select2('val')
With the recent changes to how Select2 works internally, this really isn't needed. This has been added to make the migration path easier, and it just internally calls `val` on the underlying select element. The only difference is that the `val` function will now convert any non-string elements to strings. The second argument (`triggerChange`) has not been migrated, as Select2 now internally relies on the `change` event. **Note:** As the old `initSelection` method has not been migrated, it is not possible to set the `val` on remote data sources where the value has not previously been selected.
1 parent 4afb80b commit 100015b

8 files changed

Lines changed: 109 additions & 2 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3750,6 +3750,22 @@ define('select2/core',[
37503750
return this.$container.hasClass('select2-container--open');
37513751
};
37523752

3753+
Select2.prototype.val = function (args) {
3754+
if (args.length === 0) {
3755+
return this.$element.val();
3756+
}
3757+
3758+
var newVal = args[0];
3759+
3760+
if ($.isArray(newVal)) {
3761+
newVal = $.map(newVal, function (obj) {
3762+
return obj.toString();
3763+
});
3764+
}
3765+
3766+
this.$element.val(newVal).trigger('change');
3767+
};
3768+
37533769
Select2.prototype.destroy = function () {
37543770
this.$container.remove();
37553771

dist/js/select2.amd.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3750,6 +3750,22 @@ define('select2/core',[
37503750
return this.$container.hasClass('select2-container--open');
37513751
};
37523752

3753+
Select2.prototype.val = function (args) {
3754+
if (args.length === 0) {
3755+
return this.$element.val();
3756+
}
3757+
3758+
var newVal = args[0];
3759+
3760+
if ($.isArray(newVal)) {
3761+
newVal = $.map(newVal, function (obj) {
3762+
return obj.toString();
3763+
});
3764+
}
3765+
3766+
this.$element.val(newVal).trigger('change');
3767+
};
3768+
37533769
Select2.prototype.destroy = function () {
37543770
this.$container.remove();
37553771

dist/js/select2.full.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13285,6 +13285,22 @@ define('select2/core',[
1328513285
return this.$container.hasClass('select2-container--open');
1328613286
};
1328713287

13288+
Select2.prototype.val = function (args) {
13289+
if (args.length === 0) {
13290+
return this.$element.val();
13291+
}
13292+
13293+
var newVal = args[0];
13294+
13295+
if ($.isArray(newVal)) {
13296+
newVal = $.map(newVal, function (obj) {
13297+
return obj.toString();
13298+
});
13299+
}
13300+
13301+
this.$element.val(newVal).trigger('change');
13302+
};
13303+
1328813304
Select2.prototype.destroy = function () {
1328913305
this.$container.remove();
1329013306

dist/js/select2.full.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.

dist/js/select2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4178,6 +4178,22 @@ define('select2/core',[
41784178
return this.$container.hasClass('select2-container--open');
41794179
};
41804180

4181+
Select2.prototype.val = function (args) {
4182+
if (args.length === 0) {
4183+
return this.$element.val();
4184+
}
4185+
4186+
var newVal = args[0];
4187+
4188+
if ($.isArray(newVal)) {
4189+
newVal = $.map(newVal, function (obj) {
4190+
return obj.toString();
4191+
});
4192+
}
4193+
4194+
this.$element.val(newVal).trigger('change');
4195+
};
4196+
41814197
Select2.prototype.destroy = function () {
41824198
this.$container.remove();
41834199

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ <h1>Programmatic access</h1>
283283
</p>
284284

285285
<p>
286+
<button class="js-programmatic-set-val btn btn-primary">
287+
Set to California
288+
</button>
289+
286290
<button class="js-programmatic-open btn btn-success">
287291
Open
288292
</button>
@@ -304,6 +308,20 @@ <h1>Programmatic access</h1>
304308
<select class="js-example-programmatic js-states form-control"></select>
305309
</p>
306310

311+
<p>
312+
<button class="js-programmatic-multi-set-val btn btn-primary">
313+
Set to California and Alabama
314+
</button>
315+
316+
<button class="js-programmatic-multi-clear btn btn-primary">
317+
Clear
318+
</button>
319+
</p>
320+
321+
<p>
322+
<select class="js-example-programmatic-multi js-states form-control" multiple="multiple"></select>
323+
</p>
324+
307325
</div>
308326
<div class="col-md-8">
309327
<h2>Example code</h2>
@@ -312,12 +330,20 @@ <h2>Example code</h2>
312330

313331
<script type="text/javascript" class="js-code-programmatic">
314332
var $example = $(".js-example-programmatic");
333+
var $exampleMulti = $(".js-example-programmatic-multi");
334+
335+
// Recommended to use $e.val("CA").trigger("change");
336+
$(".js-programmatic-set-val").on("click", function () { $example.select2("val", "CA"); });
315337

316338
$(".js-programmatic-open").on("click", function () { $example.select2("open"); });
317339
$(".js-programmatic-close").on("click", function () { $example.select2("close"); });
318340

319341
$(".js-programmatic-init").on("click", function () { $example.select2(); });
320342
$(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
343+
344+
// Recommended to use $e.val(["CA", "AL"]).trigger("change");
345+
$(".js-programmatic-multi-set-val").on("click", function () { $exampleMulti.select2("val", ["CA", "AL"]); });
346+
$(".js-programmatic-multi-clear").on("click", function () { $exampleMulti.select2("val", null); });
321347
</script>
322348
</div>
323349
</section>
@@ -682,6 +708,7 @@ <h2>Example code</h2>
682708
$disabledResults.select2();
683709

684710
$(".js-example-programmatic").select2();
711+
$(".js-example-programmatic-multi").select2();
685712

686713
$tags.select2({
687714
tags: ['red', 'blue', 'green']

src/js/select2/core.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ define([
297297
return this.$container.hasClass('select2-container--open');
298298
};
299299

300+
Select2.prototype.val = function (args) {
301+
if (args.length === 0) {
302+
return this.$element.val();
303+
}
304+
305+
var newVal = args[0];
306+
307+
if ($.isArray(newVal)) {
308+
newVal = $.map(newVal, function (obj) {
309+
return obj.toString();
310+
});
311+
}
312+
313+
this.$element.val(newVal).trigger('change');
314+
};
315+
300316
Select2.prototype.destroy = function () {
301317
this.$container.remove();
302318

0 commit comments

Comments
 (0)