make it possible to overwrite how the id is retreived from a choice. issue #51
This commit is contained in:
parent
011770a5d5
commit
bf5e7d16de
69
select2.js
Executable file → Normal file
69
select2.js
Executable file → Normal file
@ -1,15 +1,15 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2012 Igor Vaynberg
|
Copyright 2012 Igor Vaynberg
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in
|
||||||
compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
|
compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software distributed under the License is
|
Unless required by applicable law or agreed to in writing, software distributed under the License is
|
||||||
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
See the License for the specific language governing permissions and limitations under the License.
|
See the License for the specific language governing permissions and limitations under the License.
|
||||||
*/
|
*/
|
||||||
(function ($, undefined) {
|
(function ($, undefined) {
|
||||||
"use strict";
|
"use strict";
|
||||||
/*global document, window, jQuery, console */
|
/*global document, window, jQuery, console */
|
||||||
@ -291,7 +291,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
return function (query) {
|
return function (query) {
|
||||||
var t = query.term.toUpperCase(), filtered = {results: []};
|
var t = query.term.toUpperCase(), filtered = {results: []};
|
||||||
$(data).each(function () {
|
$(data).each(function () {
|
||||||
var isObject = this.text!==undefined,
|
var isObject = this.text !== undefined,
|
||||||
text = isObject ? this.text : this;
|
text = isObject ? this.text : this;
|
||||||
if (t === "" || text.toUpperCase().indexOf(t) >= 0) {
|
if (t === "" || text.toUpperCase().indexOf(t) >= 0) {
|
||||||
filtered.results.push(isObject ? this : {id: this, text: this});
|
filtered.results.push(isObject ? this : {id: this, text: this});
|
||||||
@ -341,7 +341,9 @@ See the License for the specific language governing permissions and limitations
|
|||||||
var results, search, resultsSelector = ".select2-results";
|
var results, search, resultsSelector = ".select2-results";
|
||||||
|
|
||||||
// prepare options
|
// prepare options
|
||||||
this.opts = this.prepareOpts(opts);
|
this.opts = opts = this.prepareOpts(opts);
|
||||||
|
|
||||||
|
this.id=opts.id;
|
||||||
|
|
||||||
// destroy if called on an existing component
|
// destroy if called on an existing component
|
||||||
if (opts.element.data("select2") !== undefined) {
|
if (opts.element.data("select2") !== undefined) {
|
||||||
@ -392,8 +394,8 @@ See the License for the specific language governing permissions and limitations
|
|||||||
|
|
||||||
installKeyUpChangeEvent(search);
|
installKeyUpChangeEvent(search);
|
||||||
search.bind("keyup-change", this.bind(this.updateResults));
|
search.bind("keyup-change", this.bind(this.updateResults));
|
||||||
search.bind("focus", function() { search.addClass("select2-focused");});
|
search.bind("focus", function () { search.addClass("select2-focused");});
|
||||||
search.bind("blur", function() { search.removeClass("select2-focused");});
|
search.bind("blur", function () { search.removeClass("select2-focused");});
|
||||||
|
|
||||||
this.container.delegate(resultsSelector, "click", this.bind(function (e) {
|
this.container.delegate(resultsSelector, "click", this.bind(function (e) {
|
||||||
if ($(e.target).closest(".select2-result:not(.select2-disabled)").length > 0) {
|
if ($(e.target).closest(".select2-result:not(.select2-disabled)").length > 0) {
|
||||||
@ -427,16 +429,22 @@ See the License for the specific language governing permissions and limitations
|
|||||||
},
|
},
|
||||||
|
|
||||||
prepareOpts: function (opts) {
|
prepareOpts: function (opts) {
|
||||||
var element, select;
|
var element, select, idKey;
|
||||||
|
|
||||||
opts = $.extend({}, {
|
opts = $.extend({}, {
|
||||||
formatResult: function (data) { return data.text; },
|
formatResult: function (data) { return data.text; },
|
||||||
formatSelection: function (data) { return data.text; },
|
formatSelection: function (data) { return data.text; },
|
||||||
formatNoMatches: function () { return "No matches found"; },
|
formatNoMatches: function () { return "No matches found"; },
|
||||||
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
|
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
|
||||||
minimumResultsForSearch: 0
|
minimumResultsForSearch: 0,
|
||||||
|
id: function (e) { return e.id; }
|
||||||
}, opts);
|
}, opts);
|
||||||
|
|
||||||
|
if (typeof(opts.id) !== "function") {
|
||||||
|
idKey = opts.id;
|
||||||
|
opts.id = function (e) { return e[idKey]; }
|
||||||
|
}
|
||||||
|
|
||||||
element = opts.element;
|
element = opts.element;
|
||||||
|
|
||||||
if (element.get(0).tagName.toLowerCase() === "select") {
|
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);
|
query.callback(data);
|
||||||
});
|
});
|
||||||
|
opts.id=function(e) { return e.id; };
|
||||||
} else {
|
} else {
|
||||||
if (!("query" in opts)) {
|
if (!("query" in opts)) {
|
||||||
if ("ajax" 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
|
* @param initial whether or not this is the call to this method right after the dropdown has been opened
|
||||||
*/
|
*/
|
||||||
updateResults: function (initial) {
|
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");
|
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
|
// create a default choice and prepend it to the list
|
||||||
if (this.opts.createSearchChoice && search.val() !== "") {
|
if (this.opts.createSearchChoice && search.val() !== "") {
|
||||||
def = this.opts.createSearchChoice.call(null, search.val(), data.results);
|
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(
|
if ($(data.results).filter(
|
||||||
function () {
|
function () {
|
||||||
return equal(this.id, def.id);
|
return equal(self.id(this), self.id(def));
|
||||||
}).length === 0) {
|
}).length === 0) {
|
||||||
data.results.unshift(def);
|
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
|
// find the selected element in the result list
|
||||||
|
|
||||||
this.results.find(".select2-result").each(function (i) {
|
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;
|
selected = i;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -981,12 +990,12 @@ See the License for the specific language governing permissions and limitations
|
|||||||
onSelect: function (data) {
|
onSelect: function (data) {
|
||||||
var old = this.opts.element.val();
|
var old = this.opts.element.val();
|
||||||
|
|
||||||
this.opts.element.val(data.id);
|
this.opts.element.val(this.id(data));
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
this.close();
|
this.close();
|
||||||
this.selection.focus();
|
this.selection.focus();
|
||||||
|
|
||||||
if (!equal(old, data.id)) { this.triggerChange(); }
|
if (!equal(old, this.id(data))) { this.triggerChange(); }
|
||||||
},
|
},
|
||||||
|
|
||||||
updateSelection: function (data) {
|
updateSelection: function (data) {
|
||||||
@ -1021,7 +1030,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
} else {
|
} else {
|
||||||
// val is an object. !val is true for [undefined,null,'']
|
// 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.updateSelection(val);
|
||||||
}
|
}
|
||||||
this.setPlaceholder();
|
this.setPlaceholder();
|
||||||
@ -1172,8 +1181,8 @@ See the License for the specific language governing permissions and limitations
|
|||||||
var placeholder = this.getPlaceholder();
|
var placeholder = this.getPlaceholder();
|
||||||
|
|
||||||
if (placeholder !== undefined
|
if (placeholder !== undefined
|
||||||
&& this.getVal().length === 0
|
&& this.getVal().length === 0
|
||||||
&& this.search.hasClass("select2-focused")===false) {
|
&& this.search.hasClass("select2-focused") === false) {
|
||||||
|
|
||||||
this.search.val(placeholder).addClass("select2-default");
|
this.search.val(placeholder).addClass("select2-default");
|
||||||
// stretch the search box to full width of the container so as much of the placeholder is visible as possible
|
// stretch the search box to full width of the container so as much of the placeholder is visible as possible
|
||||||
@ -1215,8 +1224,8 @@ See the License for the specific language governing permissions and limitations
|
|||||||
|
|
||||||
// filter out duplicates
|
// filter out duplicates
|
||||||
$(data).each(function () {
|
$(data).each(function () {
|
||||||
if (indexOf(this.id, ids) < 0) {
|
if (indexOf(self.id(this), ids) < 0) {
|
||||||
ids.push(this.id);
|
ids.push(self.id(this));
|
||||||
filtered.push(this);
|
filtered.push(this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -1255,7 +1264,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
|
|
||||||
addSelectedChoice: function (data) {
|
addSelectedChoice: function (data) {
|
||||||
var choice,
|
var choice,
|
||||||
id = data.id,
|
id = this.id(data),
|
||||||
parts,
|
parts,
|
||||||
val = this.getVal();
|
val = this.getVal();
|
||||||
|
|
||||||
@ -1311,7 +1320,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
choices.each(function () {
|
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) {
|
if (indexOf(id, val) >= 0) {
|
||||||
choice.addClass("select2-disabled");
|
choice.addClass("select2-disabled");
|
||||||
} else {
|
} else {
|
||||||
@ -1377,7 +1386,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
},
|
},
|
||||||
|
|
||||||
val: function () {
|
val: function () {
|
||||||
var val, data = [];
|
var val, data = [], self=this;
|
||||||
|
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return this.getVal();
|
return this.getVal();
|
||||||
@ -1397,7 +1406,7 @@ See the License for the specific language governing permissions and limitations
|
|||||||
this.setVal(val);
|
this.setVal(val);
|
||||||
// val is a list of objects
|
// 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.setVal(data);
|
||||||
this.updateSelection(val);
|
this.updateSelection(val);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user