From e12fbdec7ef07a14c7e2fa5daa0e0f840dd821c2 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Thu, 28 Jun 2012 23:44:30 -0700 Subject: [PATCH] add data method that works with objects instead of ids like val. fixes #166 --- select2.js | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/select2.js b/select2.js index da057400..fe79c560 100755 --- a/select2.js +++ b/select2.js @@ -1152,7 +1152,7 @@ selection.delegate("abbr", "click", this.bind(function (e) { if (!this.enabled) return; - this.val(""); + this.clear(); killEvent(e); this.close(); this.triggerChange(); @@ -1170,6 +1170,13 @@ 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 */ @@ -1197,7 +1204,7 @@ var opts = this.parent.prepareOpts.apply(this, arguments); if (opts.element.get(0).tagName.toLowerCase() === "select") { - // install sthe selection initializer + // install the selection initializer opts.initSelection = function (element, callback) { var selected = element.find(":selected"); // a single select box always has a value, no need to null check 'selected' @@ -1274,6 +1281,9 @@ // single updateSelection: function (data) { + + this.selection.data("select2-data", data); + this.selection .find("span") .html(this.opts.formatSelection(data)); @@ -1316,6 +1326,20 @@ // single clearSearch: function () { 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); } else { val = (val === null) ? [] : val; - this.setVal(val); // val is a list of objects $(val).each(function () { data.push(self.id(this)); }); this.setVal(data); @@ -1767,7 +1790,7 @@ this.searchContainer.show(); // make sure the search container is the last item in the list 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(); // update selection @@ -1777,6 +1800,21 @@ }); this.setVal(val); 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), opts, 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 () { if (args.length === 0 || typeof(args[0]) === "object") {