0

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);
        }
    });
holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • 2
    why don't you just save the list order in a variable until the user actually hits save. then append it to the url. – thescientist Oct 23 '12 at 19:35
  • Possible dupe of: http://stackoverflow.com/questions/4197591/parsing-url-hash-fragment-identifier-with-javascript – hexist Oct 23 '12 at 19:35
  • 1
    or create a custom attribute for the link and save it there? or jquery.data() http://api.jquery.com/jQuery.data/ –  Oct 23 '12 at 19:38
  • 1
    I agree with @thescientist, however, if it helps you any, i have a jQuery plguin that makes getting and manipulating the base url a true breeze. [See here my jsFiddle of it in action.](http://jsfiddle.net/SpYk3/2ZuTe/) [See here my blog post on how to use it.](http://spyk3lc.blogspot.com/2012/03/jquery-myurl-extension-how-to-get-that.html) The plugin code should be availble in both places, tho i cant say i've checked to make sure they are both at same revision, lol. My horrible procrastination! – SpYk3HH Oct 23 '12 at 19:40
  • isnt an array better than a normal variable so you dont have to split, join ...? –  Oct 23 '12 at 19:48
  • @DanielRuf, i don't know what you mean. the variable would just keep getting overwritten with the most recent sort order. then you just append that to the URL. – thescientist Oct 23 '12 at 20:11

2 Answers2

2

why don't you just save the list order in a variable until the user actually hits save. then append it to the url.

thescientist
  • 2,906
  • 1
  • 20
  • 15
0

You can throw away the listOrder parameter and append the new order before setting the href attribute of #newOrder

var url = $('#newOrder').attr("href");
var arrurl = url.split('&listOrder=');
url = arrurl[0];
url += "&listOrder=" + order;
$('#newOrder').attr('href',url);
webdev
  • 56
  • 3
  • 3
    You can do smth like jQuery('#listElements').sortable('toArray'); to get the list of ids in the order displayed – webdev Oct 23 '12 at 20:20