diff --git a/src/inputs/select2/select2.js b/src/inputs/select2/select2.js index 271d212..f4dbfc2 100644 --- a/src/inputs/select2/select2.js +++ b/src/inputs/select2/select2.js @@ -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; } }); diff --git a/test/unit/select2.js b/test/unit/select2.js index fcba99c..053298a 100644 --- a/test/unit/select2.js +++ b/test/unit/select2.js @@ -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