Anon
Anon

Reputation: 1221

Modify the order array when using sortable() (jqueryUI)

I have a list I would sort thanks to the sortable fonction of jQueryUI:

    <ul id="list-cat">
        <li id="cat_0" >Coach</li>
        <li id="cat_1" >Victory</li>
        <li id="cat_2" >Defeat</li> 
    </ul> 

My script:

    $("#list-cat").sortable({ 
        placeholder: 'highlight',
        update: function() {  
            var order = $('#list-cat').sortable('serialize');           
            $.post("my url...",order); 
            alert(order);   // displays for example "cat[]=1&cat[]=0&cat[]=2"
        }
      });

My url receives a $_POST['cat'] array with the $order as key and the $id_cat as value. I need to have the li value (Coach, Victory, etc) as array value instead of the $id_cat, is it possible ?

Upvotes: 0

Views: 264

Answers (1)

gilly3
gilly3

Reputation: 91587

Change your ids to be cat_Coach, cat_Victory, etc.

See the jQuery UI docs on .sortable("serialize"):

Serializes the sortable's item id's into a form/ajax submittable string. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.

It works by default by looking at the id of each item in the format 'setname_number', and it spits out a hash like "setname[]=number&setname[]=number".

You can also give in a option hash as second argument to custom define how the function works. The possible options are: 'key' (replaces part1[] with whatever you want), 'attribute' (test another attribute than 'id') and 'expression' (use your own regexp).

If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes foo_1, foo_5, foo_2 will serialize to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.

Upvotes: 1

Related Questions