diff --git a/src/jquery.jeditable.js b/src/jquery.jeditable.js index a3f46c3..f5e157c 100644 --- a/src/jquery.jeditable.js +++ b/src/jquery.jeditable.js @@ -683,9 +683,21 @@ var _supportInType = function (type) { // Create tuples for sorting var tuples = []; var key; - for (key in json) { - tuples.push([key, json[key]]); // Store: [key, value] + + if (Array.isArray(json) && json.every(Array.isArray)) { + // Process list of tuples + tuples = json // JSON already contains list of [key, value] + json = {}; + tuples.forEach(function(e) { + json[e[0]] = e[1]; // Recreate json object to comply with following code + }); + } else { + // Process object + for (key in json) { + tuples.push([key, json[key]]); // Store: [key, value] + } } + if (settings.sortselectoptions) { // sort it tuples.sort(function (a, b) { diff --git a/tests/tests.js b/tests/tests.js index bd5cc74..f505f0f 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -65,6 +65,67 @@ QUnit.test('Default: Sorting select options', function(assert) { assert.deepEqual(optionsList, ['Letter Disk', 'Letter E', 'Letter F'], 'It does sort the given options list'); }); +QUnit.module('select-boxes input data'); +QUnit.test('List of tuples', function(assert) { + elem.append(''); + var e = $('#select-tester', elem); + + var test_data = [['E', 'Letter E'], ['F', 'Letter F'], ['D', 'Letter Disk']]; + e.editable('http://bla', { + type: 'select', + data: test_data + }); + + e.click(); + + var optionsList = []; + e.find('option').each(function(name, val) { optionsList.push([val.value, val.text]); }); + assert.deepEqual(optionsList, test_data, 'Options keep sorted as defined in input'); +}); +QUnit.test('List of strings', function(assert) { + elem.append(''); + var e = $('#select-tester', elem); + + var test_data = ['E', 'F', 'D']; + var sort = Math.random() > 0.5; + e.editable('http://bla', { + type: 'select', + data: test_data, + sortselectoptions: sort + }); + + e.click(); + + var optionsList = []; + var expected_result = [['0', 'E'],['1', 'F'], ['2', 'D']]; + if (sort) { + expected_result.sort(function(a, b) {return a[1] > b[1];}); + } + e.find('option').each(function(name, val) { optionsList.push([val.value, val.text]); }); + assert.deepEqual(optionsList, expected_result, 'Options get auto assigned integer values in order of input and options are sorted by label based on sortselectoptions option'); +}); +QUnit.test('Object', function(assert) { + elem.append(''); + var e = $('#select-tester', elem); + + var test_data = {'E': 'Letter E', 'F': 'Letter F', 'D': 'Letter Disk'}; + var sort = Math.random() > 0.5; + e.editable('http://bla', { + type: 'select', + data: test_data, + sortselectoptions: sort + }); + + e.click(); + + var optionsList = []; + var expected_result = [['E', 'Letter E'], ['F', 'Letter F'], ['D', 'Letter Disk']]; + if (sort) { + expected_result.sort(function(a, b) {return a[1] > b[1];}); + } + e.find('option').each(function(name, val) { optionsList.push([val.value, val.text]); }); + assert.deepEqual(optionsList, expected_result, 'Options are sorted either in order of object definition or by label depending on sortselectoptions option'); +}); QUnit.module('select-boxes setting selected'); QUnit.test('Explicitly setting a selected option', function(assert) { elem.append( 'Letter F' );