1
0
mirror of synced 2025-02-18 04:53:15 +03:00

make it possible to overwrite how the id is retreived from a choice. issue #51

This commit is contained in:
Igor Vaynberg 2012-05-03 15:00:48 -07:00
parent 011770a5d5
commit bf5e7d16de

43
select2.js Executable file → Normal file
View File

@ -1,4 +1,4 @@
/*
/*
Copyright 2012 Igor Vaynberg
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in
@ -341,7 +341,9 @@ See the License for the specific language governing permissions and limitations
var results, search, resultsSelector = ".select2-results";
// prepare options
this.opts = this.prepareOpts(opts);
this.opts = opts = this.prepareOpts(opts);
this.id=opts.id;
// destroy if called on an existing component
if (opts.element.data("select2") !== undefined) {
@ -427,16 +429,22 @@ See the License for the specific language governing permissions and limitations
},
prepareOpts: function (opts) {
var element, select;
var element, select, idKey;
opts = $.extend({}, {
formatResult: function (data) { return data.text; },
formatSelection: function (data) { return data.text; },
formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
minimumResultsForSearch: 0
minimumResultsForSearch: 0,
id: function (e) { return e.id; }
}, opts);
if (typeof(opts.id) !== "function") {
idKey = opts.id;
opts.id = function (e) { return e[idKey]; }
}
element = opts.element;
if (element.get(0).tagName.toLowerCase() === "select") {
@ -476,6 +484,7 @@ See the License for the specific language governing permissions and limitations
});
query.callback(data);
});
opts.id=function(e) { return e.id; };
} else {
if (!("query" in opts)) {
if ("ajax" in opts) {
@ -663,7 +672,7 @@ See the License for the specific language governing permissions and limitations
* @param initial whether or not this is the call to this method right after the dropdown has been opened
*/
updateResults: function (initial) {
var search = this.search, results = this.results, opts = this.opts;
var search = this.search, results = this.results, opts = this.opts, self=this;
search.addClass("select2-active");
@ -686,10 +695,10 @@ See the License for the specific language governing permissions and limitations
// create a default choice and prepend it to the list
if (this.opts.createSearchChoice && search.val() !== "") {
def = this.opts.createSearchChoice.call(null, search.val(), data.results);
if (def !== undefined && def !== null && def.id !== undefined && def.id !== null) {
if (def !== undefined && def !== null && self.id(def) !== undefined && self.id(def) !== null) {
if ($(data.results).filter(
function () {
return equal(this.id, def.id);
return equal(self.id(this), self.id(def));
}).length === 0) {
data.results.unshift(def);
}
@ -956,7 +965,7 @@ See the License for the specific language governing permissions and limitations
// find the selected element in the result list
this.results.find(".select2-result").each(function (i) {
if (equal($(this).data("select2-data").id, self.opts.element.val())) {
if (equal(self.id($(this).data("select2-data")), self.opts.element.val())) {
selected = i;
return false;
}
@ -981,12 +990,12 @@ See the License for the specific language governing permissions and limitations
onSelect: function (data) {
var old = this.opts.element.val();
this.opts.element.val(data.id);
this.opts.element.val(this.id(data));
this.updateSelection(data);
this.close();
this.selection.focus();
if (!equal(old, data.id)) { this.triggerChange(); }
if (!equal(old, this.id(data))) { this.triggerChange(); }
},
updateSelection: function (data) {
@ -1021,7 +1030,7 @@ See the License for the specific language governing permissions and limitations
this.updateSelection(data);
} else {
// val is an object. !val is true for [undefined,null,'']
this.opts.element.val(!val ? "" : val.id);
this.opts.element.val(!val ? "" : this.id(val));
this.updateSelection(val);
}
this.setPlaceholder();
@ -1215,8 +1224,8 @@ See the License for the specific language governing permissions and limitations
// filter out duplicates
$(data).each(function () {
if (indexOf(this.id, ids) < 0) {
ids.push(this.id);
if (indexOf(self.id(this), ids) < 0) {
ids.push(self.id(this));
filtered.push(this);
}
});
@ -1255,7 +1264,7 @@ See the License for the specific language governing permissions and limitations
addSelectedChoice: function (data) {
var choice,
id = data.id,
id = this.id(data),
parts,
val = this.getVal();
@ -1311,7 +1320,7 @@ See the License for the specific language governing permissions and limitations
self = this;
choices.each(function () {
var choice = $(this), id = choice.data("select2-data").id;
var choice = $(this), id = self.id(choice.data("select2-data"));
if (indexOf(id, val) >= 0) {
choice.addClass("select2-disabled");
} else {
@ -1377,7 +1386,7 @@ See the License for the specific language governing permissions and limitations
},
val: function () {
var val, data = [];
var val, data = [], self=this;
if (arguments.length === 0) {
return this.getVal();
@ -1397,7 +1406,7 @@ See the License for the specific language governing permissions and limitations
this.setVal(val);
// val is a list of objects
$(val).each(function () { data.push(this.id); });
$(val).each(function () { data.push(self.id(this)); });
this.setVal(data);
this.updateSelection(val);
}