Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Accept list of tuples as items of select (#209)
Browse files Browse the repository at this point in the history
* Accept list of tuples as items of select
* Add tests for various formats of input data

Signed-off-by: Michal Juranyi <[email protected]>
  • Loading branch information
michal-juranyi authored and NicolasCARPi committed Aug 27, 2019
1 parent 8accf58 commit 1b633eb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/jquery.jeditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
61 changes: 61 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<span id="select-tester"></span>');
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('<span id="select-tester"></span>');
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('<span id="select-tester"></span>');
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( '<span id="selected-tester">Letter F</span>' );
Expand Down

0 comments on commit 1b633eb

Please sign in to comment.