convert source from x-editable format to select2 format

This commit is contained in:
vitalets 2013-06-23 11:31:46 +04:00
parent 4d1100d4b4
commit 502151b9dc
2 changed files with 21 additions and 5 deletions
src/inputs/select2
test/unit

@ -63,9 +63,9 @@ $(function(){
}
options.select2.ajax.url = source;
} else {
//todo: possible convert x-editable source to select2 source format
options.select2.data = source;
this.sourceData = source;
//check format and convert x-editable format to select2 format (if needed)
this.sourceData = this.convertSource(source);
options.select2.data = this.sourceData;
}
}
@ -186,6 +186,22 @@ $(function(){
$(this).closest('form').submit();
}
});
},
/*
Converts source from x-editable format: {value: 1, text: "1"} to
select2 format: {id: 1, text: "1"}
*/
convertSource: function(source) {
if($.isArray(source) && source.length && source[0].value !== undefined) {
for(var i = 0; i<source.length; i++) {
if(source[i].value !== undefined) {
source[i].id = source[i].value;
delete source[i].value;
}
}
}
return source;
}
});

@ -142,9 +142,9 @@ $(function () {
}, timeout);
});
test("setValue (local)", function () {
test("setValue (local) + x-editable source", function () {
var e = $('<a href="#" data-type="select2" data-name="select2" data-value="1">test2</a>').appendTo('#qunit-fixture').editable({
source: [{id: 1, text: 'text1'}, {id: 2, text: 'text2'}, {id: 3, text: 'text3'}]
source: [{value: 1, text: 'text1'}, {value: 2, text: 'text2'}, {value: 3, text: 'text3'}]
});
//autotext