support id() and text() functions of select2, see

This commit is contained in:
vitalets
2013-06-26 10:26:49 +04:00
parent 2c678a10ae
commit e6ac02124c
3 changed files with 23 additions and 10 deletions

@ -1,6 +1,11 @@
X-editable changelog X-editable changelog
============================= =============================
Version 1.4.6 wip
----------------------------
[bug #276] support id() and text() functions of select2 (vitalets)
Version 1.4.5 Jun 23, 2013 Version 1.4.5 Jun 23, 2013
---------------------------- ----------------------------
[enh #245] highlight element after update (vitalets) [enh #245] highlight element after update (vitalets)

@ -139,7 +139,10 @@
return []; return [];
} }
valueProp = valueProp || 'value'; if (typeof(valueProp) !== "function") {
var idKey = valueProp || 'value';
valueProp = function (e) { return e[idKey]; };
}
var isValArray = $.isArray(value), var isValArray = $.isArray(value),
result = [], result = [],
@ -151,11 +154,11 @@
} else { } else {
/*jslint eqeq: true*/ /*jslint eqeq: true*/
if(isValArray) { if(isValArray) {
if($.grep(value, function(v){ return v == (o && typeof o === 'object' ? o[valueProp] : o); }).length) { if($.grep(value, function(v){ return v == (o && typeof o === 'object' ? valueProp(o) : o); }).length) {
result.push(o); result.push(o);
} }
} else { } else {
if(value == (o && typeof o === 'object' ? o[valueProp] : o)) { if(value == (o && typeof o === 'object' ? valueProp(o) : o)) {
result.push(o); result.push(o);
} }
} }

@ -109,15 +109,20 @@ $(function(){
$(this).closest('form').parent().triggerHandler('resize'); $(this).closest('form').parent().triggerHandler('resize');
}); });
} }
//store function that extracs ID from element
this.idFunc = this.$input.data('select2').opts.id;
this.formatSelection = this.$input.data('select2').opts.formatSelection;
}, },
value2html: function(value, element) { value2html: function(value, element) {
var text = '', data; var text = '', data,
that = this;
if(this.options.select2.tags) { //in tags mode just assign value if(this.options.select2.tags) { //in tags mode just assign value
data = value; data = value;
} else if(this.sourceData) { } else if(this.sourceData) {
data = $.fn.editableutils.itemsByValue(value, this.sourceData, 'id'); data = $.fn.editableutils.itemsByValue(value, this.sourceData, this.idFunc);
} else { } else {
//can not get list of possible values (e.g. autotext for select2 with ajax source) //can not get list of possible values (e.g. autotext for select2 with ajax source)
} }
@ -127,10 +132,10 @@ $(function(){
//collect selected data and show with separator //collect selected data and show with separator
text = []; text = [];
$.each(data, function(k, v){ $.each(data, function(k, v){
text.push(v && typeof v === 'object' ? v.text : v); text.push(v && typeof v === 'object' ? that.formatSelection(v) : v);
}); });
} else if(data) { } else if(data) {
text = data.text; text = that.formatSelection(data);
} }
text = $.isArray(text) ? text.join(this.options.viewseparator) : text; text = $.isArray(text) ? text.join(this.options.viewseparator) : text;
@ -149,7 +154,7 @@ $(function(){
var item, items; var item, items;
//if sourceData loaded, use it to get text for display //if sourceData loaded, use it to get text for display
if(this.sourceData) { if(this.sourceData) {
items = $.fn.editableutils.itemsByValue(value, this.sourceData, 'id'); items = $.fn.editableutils.itemsByValue(value, this.sourceData, this.idFunc);
if(items.length) { if(items.length) {
item = items[0]; item = items[0];
} }