Skip to content

Commit 273ec14

Browse files
committed
Merge pull request select2#771 from phtrivier/issue-619
select2#619 : val takes an optional argument to avoid triggering 'change'
2 parents 3f87728 + cf095d9 commit 273ec14

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

select2.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,14 +1757,18 @@ the specific language governing permissions and limitations under the Apache Lic
17571757

17581758
// single
17591759
val: function () {
1760-
var val, data = null, self = this;
1760+
var val, triggerChange = true, data = null, self = this;
17611761

17621762
if (arguments.length === 0) {
17631763
return this.opts.element.val();
17641764
}
17651765

17661766
val = arguments[0];
17671767

1768+
if (arguments.length > 1) {
1769+
triggerChange = arguments[1];
1770+
}
1771+
17681772
if (this.select) {
17691773
this.select
17701774
.val(val)
@@ -1774,15 +1778,19 @@ the specific language governing permissions and limitations under the Apache Lic
17741778
});
17751779
this.updateSelection(data);
17761780
this.setPlaceholder();
1777-
this.triggerChange();
1781+
if (triggerChange) {
1782+
this.triggerChange();
1783+
}
17781784
} else {
17791785
if (this.opts.initSelection === undefined) {
17801786
throw new Error("cannot call val() if initSelection() is not defined");
17811787
}
17821788
// val is an id. !val is true for [undefined,null,'']
17831789
if (!val) {
17841790
this.clear();
1785-
this.triggerChange();
1791+
if (triggerChange) {
1792+
this.triggerChange();
1793+
}
17861794
return;
17871795
}
17881796
this.opts.element.val(val);
@@ -2302,19 +2310,25 @@ the specific language governing permissions and limitations under the Apache Lic
23022310

23032311
// multi
23042312
val: function () {
2305-
var val, data = [], self=this;
2313+
var val, triggerChange = true, data = [], self=this;
23062314

23072315
if (arguments.length === 0) {
23082316
return this.getVal();
23092317
}
23102318

23112319
val = arguments[0];
23122320

2321+
if (arguments.length > 1) {
2322+
triggerChange = arguments[1];
2323+
}
2324+
23132325
if (!val) {
23142326
this.opts.element.val("");
23152327
this.updateSelection([]);
23162328
this.clearSearch();
2317-
this.triggerChange();
2329+
if (triggerChange) {
2330+
this.triggerChange();
2331+
}
23182332
return;
23192333
}
23202334

@@ -2326,7 +2340,9 @@ the specific language governing permissions and limitations under the Apache Lic
23262340
data.push({id: $(this).attr("value"), text: $(this).text()});
23272341
});
23282342
this.updateSelection(data);
2329-
this.triggerChange();
2343+
if (triggerChange) {
2344+
this.triggerChange();
2345+
}
23302346
} else {
23312347
if (this.opts.initSelection === undefined) {
23322348
throw new Error("val() cannot be called if initSelection() is not defined")
@@ -2337,7 +2353,9 @@ the specific language governing permissions and limitations under the Apache Lic
23372353
self.setVal(ids);
23382354
self.updateSelection(data);
23392355
self.clearSearch();
2340-
self.triggerChange();
2356+
if (triggerChange) {
2357+
self.triggerChange();
2358+
}
23412359
});
23422360
}
23432361
this.clearSearch();

test-619.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
4+
<link href="select2.css" rel="stylesheet"/>
5+
<script type="text/javascript" src="select2.js"></script>
6+
</head>
7+
<body>
8+
<select id="s1">
9+
<option value="A"></option>
10+
<option value="B"></option>
11+
</select>
12+
13+
<script type="text/javascript">
14+
$(document).ready(function () {
15+
$("#s1").select2({}).on("change", function () {
16+
alert("Changed caused by val()");
17+
}).select2("val", "A");
18+
19+
// This should not trigger a change
20+
$("#s1").select2("val", "B", false);
21+
});
22+
23+
</script>
24+
25+
26+
</body>
27+
28+
</html>

0 commit comments

Comments
 (0)