The user sorts list objects in the way he wants, then he clicks the link 'Save' and the order is saved to the db (with the help of the url). Before the user changes the order the url looks as this: index.php?type=list&action=saveNewListOrder&listId=24.
When the user changes the order of the list objects the function below runs, and creates a string of the list object that is merged with the url.
Everything works fine except that the second time the user moves a list object the url that's retrieved already contains the string with the list objects (since the last move). Every time the user moves a list object the url grows.
After the third move the url can look like this:
index.php?type=list&action=saveNewListOrder&listId=170&listOrder=772.773.775.774
.776&listOrder=772.775.773.774.776&listOrder=775.773.772.774.776
I have thought of using substring (to extract the first part of the url) and also of putting the url in a hidden field that's retrieved every time and never changes. Both of these solutions feels unnecessary complicated, and there must be a more elegant solution.
$('#listElements').sortable({
update: function(event, ui) {
var order = '';
var i = 0;
$('.listObject li').each(function (e) {
if (i != 0) {
order += '.';
}
order += $(this).attr('id');//.push($(this).attr('id'));
i +=1;
});
var url = $('#newOrder').attr("href");
url += "&listOrder=" + order;
$('#newOrder').attr('href',url);
}
});