Skip to content

Commit cf095d9

Browse files
committed
select2#619 : val takes an optional argument to avoid triggering 'change'
1 parent d041014 commit cf095d9

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
@@ -1736,14 +1736,18 @@ the specific language governing permissions and limitations under the Apache Lic
17361736

17371737
// single
17381738
val: function () {
1739-
var val, data = null, self = this;
1739+
var val, triggerChange = true, data = null, self = this;
17401740

17411741
if (arguments.length === 0) {
17421742
return this.opts.element.val();
17431743
}
17441744

17451745
val = arguments[0];
17461746

1747+
if (arguments.length > 1) {
1748+
triggerChange = arguments[1];
1749+
}
1750+
17471751
if (this.select) {
17481752
this.select
17491753
.val(val)
@@ -1753,15 +1757,19 @@ the specific language governing permissions and limitations under the Apache Lic
17531757
});
17541758
this.updateSelection(data);
17551759
this.setPlaceholder();
1756-
this.triggerChange();
1760+
if (triggerChange) {
1761+
this.triggerChange();
1762+
}
17571763
} else {
17581764
if (this.opts.initSelection === undefined) {
17591765
throw new Error("cannot call val() if initSelection() is not defined");
17601766
}
17611767
// val is an id. !val is true for [undefined,null,'']
17621768
if (!val) {
17631769
this.clear();
1764-
this.triggerChange();
1770+
if (triggerChange) {
1771+
this.triggerChange();
1772+
}
17651773
return;
17661774
}
17671775
this.opts.element.val(val);
@@ -2278,19 +2286,25 @@ the specific language governing permissions and limitations under the Apache Lic
22782286

22792287
// multi
22802288
val: function () {
2281-
var val, data = [], self=this;
2289+
var val, triggerChange = true, data = [], self=this;
22822290

22832291
if (arguments.length === 0) {
22842292
return this.getVal();
22852293
}
22862294

22872295
val = arguments[0];
22882296

2297+
if (arguments.length > 1) {
2298+
triggerChange = arguments[1];
2299+
}
2300+
22892301
if (!val) {
22902302
this.opts.element.val("");
22912303
this.updateSelection([]);
22922304
this.clearSearch();
2293-
this.triggerChange();
2305+
if (triggerChange) {
2306+
this.triggerChange();
2307+
}
22942308
return;
22952309
}
22962310

@@ -2302,7 +2316,9 @@ the specific language governing permissions and limitations under the Apache Lic
23022316
data.push({id: $(this).attr("value"), text: $(this).text()});
23032317
});
23042318
this.updateSelection(data);
2305-
this.triggerChange();
2319+
if (triggerChange) {
2320+
this.triggerChange();
2321+
}
23062322
} else {
23072323
if (this.opts.initSelection === undefined) {
23082324
throw new Error("val() cannot be called if initSelection() is not defined")
@@ -2313,7 +2329,9 @@ the specific language governing permissions and limitations under the Apache Lic
23132329
self.setVal(ids);
23142330
self.updateSelection(data);
23152331
self.clearSearch();
2316-
self.triggerChange();
2332+
if (triggerChange) {
2333+
self.triggerChange();
2334+
}
23172335
});
23182336
}
23192337
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)