clean up val. now it only accepts ids and only when initselection is set
This commit is contained in:
parent
ad8fd257aa
commit
116528908a
57
select2.js
57
select2.js
@ -1302,6 +1302,7 @@
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// single
|
||||||
clear: function() {
|
clear: function() {
|
||||||
this.opts.element.val("");
|
this.opts.element.val("");
|
||||||
this.selection.find("span").empty();
|
this.selection.find("span").empty();
|
||||||
@ -1438,7 +1439,6 @@
|
|||||||
val = arguments[0];
|
val = arguments[0];
|
||||||
|
|
||||||
if (this.select) {
|
if (this.select) {
|
||||||
// val is an id
|
|
||||||
this.select
|
this.select
|
||||||
.val(val)
|
.val(val)
|
||||||
.find(":selected").each2(function (i, elm) {
|
.find(":selected").each2(function (i, elm) {
|
||||||
@ -1446,23 +1446,22 @@
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
|
this.setPlaceholder();
|
||||||
} else {
|
} else {
|
||||||
// val is an object. !val is true for [undefined,null,'']
|
if (this.opts.initSelection === undefined) {
|
||||||
if (this.opts.initSelection && val) {
|
throw new Error("cannot call val() if initSelection() is not defined");
|
||||||
that = this;
|
}
|
||||||
this.opts.initSelection(this.opts.element.val(val), function(data){
|
// val is an id. !val is true for [undefined,null,'']
|
||||||
|
if (!val) {
|
||||||
|
this.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.opts.initSelection(this.opts.element, function(data){
|
||||||
self.opts.element.val(!data ? "" : self.id(data));
|
self.opts.element.val(!data ? "" : self.id(data));
|
||||||
self.updateSelection(data);
|
self.updateSelection(data);
|
||||||
self.setPlaceholder();
|
self.setPlaceholder();
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
this.opts.element.val(!val ? "" : this.id(val));
|
|
||||||
this.updateSelection(val);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
this.setPlaceholder();
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// single
|
// single
|
||||||
@ -1907,31 +1906,33 @@
|
|||||||
|
|
||||||
val = arguments[0];
|
val = arguments[0];
|
||||||
|
|
||||||
if (this.select) {
|
if (!val) {
|
||||||
|
this.opts.element.val("");
|
||||||
|
this.updateSelection([]);
|
||||||
|
this.clearSearch();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// val is a list of ids
|
// val is a list of ids
|
||||||
this.setVal(val);
|
this.setVal(val);
|
||||||
|
|
||||||
|
if (this.select) {
|
||||||
this.select.find(":selected").each(function () {
|
this.select.find(":selected").each(function () {
|
||||||
data.push({id: $(this).attr("value"), text: $(this).text()});
|
data.push({id: $(this).attr("value"), text: $(this).text()});
|
||||||
});
|
});
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
} else {
|
} else {
|
||||||
if (this.opts.initSelection && val !== null) {
|
if (this.opts.initSelection === undefined) {
|
||||||
this.opts.initSelection(this.opts.element.val(val), function(newVal){
|
throw new Error("val() cannot be called if initSelection() is not defined")
|
||||||
$(newVal).each(function () { data.push(self.id(this)); });
|
|
||||||
self.setVal(data);
|
|
||||||
self.updateSelection(newVal);
|
|
||||||
self.clearSearch();
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
val = (val === null) ? [] : val;
|
|
||||||
// val is a list of objects
|
|
||||||
$(val).each(function () { data.push(self.id(this)); });
|
|
||||||
this.setVal(data);
|
|
||||||
this.updateSelection(val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.opts.initSelection(this.opts.element, function(data){
|
||||||
|
var ids=$(data).map(self.id);
|
||||||
|
self.setVal(ids);
|
||||||
|
self.updateSelection(data);
|
||||||
|
self.clearSearch();
|
||||||
|
});
|
||||||
|
}
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user