Order exercises in request body according to manual sort order in UI

This commit is contained in:
Maximilian Grundke
2018-07-16 10:38:34 +02:00
parent 620a0841e8
commit 24c5e0e88d
2 changed files with 16 additions and 2 deletions

View File

@ -127,8 +127,22 @@ $(function() {
.attr("class", "line maximum-working-time")
.attr("d", maxWorkingTime);
} else if (exerciseList.isPresent()) {
var exerciseSelect = $('#exercise-select');
var list = $("#sortable");
list.sortable();
var updateExerciseList = function () {
// remove all options from the hidden select and add all selected exercises in the new order
exerciseSelect.find('option').remove();
var exerciseIdsInSortedOrder = list.sortable('toArray', {attribute: 'data-id'});
for (var i = 0; i < exerciseIdsInSortedOrder.length; i += 1) {
exerciseSelect.append('<option value="' + exerciseIdsInSortedOrder[i] + '" selected></option>')
}
}
list.sortable({
items: 'tr',
update: updateExerciseList
});
list.disableSelection();
var addExercisesForm = $('#exercise-selection');

View File

@ -28,7 +28,7 @@
a.remove-exercise href='#' = t('shared.destroy')
button.btn.btn-primary type='button' data-toggle='modal' data-target='#add-exercise-modal' = t('exercise_collections.form.add_exercises')
.hidden
= f.collection_select(:exercise_ids, Exercise.all, :id, :title, {}, {class: 'form-control', multiple: true})
= f.collection_select(:exercise_ids, Exercise.all, :id, :title, {}, {id: 'exercise-select', class: 'form-control', multiple: true})
.actions = render('shared/submit_button', f: f, object: @exercise_collection)