#619 : val takes an optional argument to avoid triggering 'change'
This commit is contained in:
parent
d0410144ba
commit
cf095d9fee
22
select2.js
22
select2.js
@ -1736,7 +1736,7 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
|
|
||||||
// single
|
// single
|
||||||
val: function () {
|
val: function () {
|
||||||
var val, data = null, self = this;
|
var val, triggerChange = true, data = null, self = this;
|
||||||
|
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return this.opts.element.val();
|
return this.opts.element.val();
|
||||||
@ -1744,6 +1744,10 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
|
|
||||||
val = arguments[0];
|
val = arguments[0];
|
||||||
|
|
||||||
|
if (arguments.length > 1) {
|
||||||
|
triggerChange = arguments[1];
|
||||||
|
}
|
||||||
|
|
||||||
if (this.select) {
|
if (this.select) {
|
||||||
this.select
|
this.select
|
||||||
.val(val)
|
.val(val)
|
||||||
@ -1753,7 +1757,9 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
});
|
});
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
this.setPlaceholder();
|
this.setPlaceholder();
|
||||||
|
if (triggerChange) {
|
||||||
this.triggerChange();
|
this.triggerChange();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.opts.initSelection === undefined) {
|
if (this.opts.initSelection === undefined) {
|
||||||
throw new Error("cannot call val() if initSelection() is not defined");
|
throw new Error("cannot call val() if initSelection() is not defined");
|
||||||
@ -1761,7 +1767,9 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
// val is an id. !val is true for [undefined,null,'']
|
// val is an id. !val is true for [undefined,null,'']
|
||||||
if (!val) {
|
if (!val) {
|
||||||
this.clear();
|
this.clear();
|
||||||
|
if (triggerChange) {
|
||||||
this.triggerChange();
|
this.triggerChange();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.opts.element.val(val);
|
this.opts.element.val(val);
|
||||||
@ -2278,7 +2286,7 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
|
|
||||||
// multi
|
// multi
|
||||||
val: function () {
|
val: function () {
|
||||||
var val, data = [], self=this;
|
var val, triggerChange = true, data = [], self=this;
|
||||||
|
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return this.getVal();
|
return this.getVal();
|
||||||
@ -2286,11 +2294,17 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
|
|
||||||
val = arguments[0];
|
val = arguments[0];
|
||||||
|
|
||||||
|
if (arguments.length > 1) {
|
||||||
|
triggerChange = arguments[1];
|
||||||
|
}
|
||||||
|
|
||||||
if (!val) {
|
if (!val) {
|
||||||
this.opts.element.val("");
|
this.opts.element.val("");
|
||||||
this.updateSelection([]);
|
this.updateSelection([]);
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
|
if (triggerChange) {
|
||||||
this.triggerChange();
|
this.triggerChange();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2302,7 +2316,9 @@ the specific language governing permissions and limitations under the Apache Lic
|
|||||||
data.push({id: $(this).attr("value"), text: $(this).text()});
|
data.push({id: $(this).attr("value"), text: $(this).text()});
|
||||||
});
|
});
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
|
if (triggerChange) {
|
||||||
this.triggerChange();
|
this.triggerChange();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.opts.initSelection === undefined) {
|
if (this.opts.initSelection === undefined) {
|
||||||
throw new Error("val() cannot be called if initSelection() is not defined")
|
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
|
|||||||
self.setVal(ids);
|
self.setVal(ids);
|
||||||
self.updateSelection(data);
|
self.updateSelection(data);
|
||||||
self.clearSearch();
|
self.clearSearch();
|
||||||
|
if (triggerChange) {
|
||||||
self.triggerChange();
|
self.triggerChange();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
|
28
test-619.html
Normal file
28
test-619.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
|
||||||
|
<link href="select2.css" rel="stylesheet"/>
|
||||||
|
<script type="text/javascript" src="select2.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<select id="s1">
|
||||||
|
<option value="A"></option>
|
||||||
|
<option value="B"></option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function () {
|
||||||
|
$("#s1").select2({}).on("change", function () {
|
||||||
|
alert("Changed caused by val()");
|
||||||
|
}).select2("val", "A");
|
||||||
|
|
||||||
|
// This should not trigger a change
|
||||||
|
$("#s1").select2("val", "B", false);
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user