1
0
mirror of synced 2024-11-25 14:26:03 +03:00

add data method that works with objects instead of ids like val. fixes #166

This commit is contained in:
Igor Vaynberg 2012-06-28 23:44:30 -07:00
parent 3107d9d773
commit e12fbdec7e

View File

@ -1152,7 +1152,7 @@
selection.delegate("abbr", "click", this.bind(function (e) { selection.delegate("abbr", "click", this.bind(function (e) {
if (!this.enabled) return; if (!this.enabled) return;
this.val(""); this.clear();
killEvent(e); killEvent(e);
this.close(); this.close();
this.triggerChange(); this.triggerChange();
@ -1170,6 +1170,13 @@
this.opts.element.bind("open", function() { focusser.hide(); }); this.opts.element.bind("open", function() { focusser.hide(); });
}, },
clear: function() {
this.opts.element.val("");
this.selection.find("span").empty();
this.selection.removeData("select2-data");
this.setPlaceholder();
},
/** /**
* Sets selection based on source element's value * Sets selection based on source element's value
*/ */
@ -1197,7 +1204,7 @@
var opts = this.parent.prepareOpts.apply(this, arguments); var opts = this.parent.prepareOpts.apply(this, arguments);
if (opts.element.get(0).tagName.toLowerCase() === "select") { if (opts.element.get(0).tagName.toLowerCase() === "select") {
// install sthe selection initializer // install the selection initializer
opts.initSelection = function (element, callback) { opts.initSelection = function (element, callback) {
var selected = element.find(":selected"); var selected = element.find(":selected");
// a single select box always has a value, no need to null check 'selected' // a single select box always has a value, no need to null check 'selected'
@ -1274,6 +1281,9 @@
// single // single
updateSelection: function (data) { updateSelection: function (data) {
this.selection.data("select2-data", data);
this.selection this.selection
.find("span") .find("span")
.html(this.opts.formatSelection(data)); .html(this.opts.formatSelection(data));
@ -1316,6 +1326,20 @@
// single // single
clearSearch: function () { clearSearch: function () {
this.search.val(""); this.search.val("");
},
// single
data: function(value) {
if (arguments.length === 0) {
return this.selection.data("select2-data");
} else {
if (!value || value === "") {
this.clear();
} else {
this.opts.element.val(!value ? "" : this.id(value));
this.updateSelection(value);
}
}
} }
}); });
@ -1736,7 +1760,6 @@
this.updateSelection(data); this.updateSelection(data);
} else { } else {
val = (val === null) ? [] : val; val = (val === null) ? [] : val;
this.setVal(val);
// val is a list of objects // val is a list of objects
$(val).each(function () { data.push(self.id(this)); }); $(val).each(function () { data.push(self.id(this)); });
this.setVal(data); this.setVal(data);
@ -1767,7 +1790,7 @@
this.searchContainer.show(); this.searchContainer.show();
// make sure the search container is the last item in the list // make sure the search container is the last item in the list
this.searchContainer.appendTo(this.searchContainer.parent()); this.searchContainer.appendTo(this.searchContainer.parent());
// since we collapsed the width in dragStarteed, we resize it here // since we collapsed the width in dragStarted, we resize it here
this.resizeSearch(); this.resizeSearch();
// update selection // update selection
@ -1777,6 +1800,21 @@
}); });
this.setVal(val); this.setVal(val);
this.triggerChange(); this.triggerChange();
},
// multi
data: function(values) {
var self=this, ids;
if (arguments.length === 0) {
return this.selection
.find(".select2-search-choice")
.map(function() { return $(this).data("select2-data"); })
.get();
} else {
ids = $.map(values, function(e) { return self.opts.id(e)});
this.setVal(ids);
this.updateSelection(values);
}
} }
}); });
@ -1785,7 +1823,7 @@
var args = Array.prototype.slice.call(arguments, 0), var args = Array.prototype.slice.call(arguments, 0),
opts, opts,
select2, select2,
value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown"]; value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"];
this.each(function () { this.each(function () {
if (args.length === 0 || typeof(args[0]) === "object") { if (args.length === 0 || typeof(args[0]) === "object") {