Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI: sortable('toArray') returns an empty array

This has me stumped. The follow code returns ",,,,,,":

<script type="text/javascript">
$(function() {
    $('#listB').sortable({
        connectWith: '#listA',
        update: function(event, ui) {
            var result = $(this).sortable('toArray');
            alert(result);
            }
    });
    $('#listA').sortable({
        connectWith: '#listB'
    });
});
</script>

<div id="boxA">
    <ul id="listA" class="myList">
        <li value="1">Item A</li>
        <li value="2">Item B</li>
        <li value="3">Item C</li>
        <li value="4">Item D</li>
        <li value="5">Item E</li>
        <li value="6">Item F</li>
        <li value="7">Item G</li>
    </ul>
</div>

<div id="boxB">
    <ul id="listB" class="myList">
        <li value="1">Item A</li>
        <li value="2">Item B</li>
        <li value="3">Item C</li>
        <li value="4">Item D</li>
        <li value="5">Item E</li>
        <li value="6">Item F</li>
        <li value="7">Item G</li>
    </ul>
</div>

Why?! It's driving me insane! Any suggestions?

like image 766
bjork24 Avatar asked Nov 02 '09 19:11

bjork24


3 Answers

You can define which attribute to fetch like this:

var result = $(this).sortable('toArray', {attribute: 'value'});
like image 130
Niklaus Avatar answered Oct 23 '22 03:10

Niklaus


.sortable('toArray') serializes items Ids into array, and your items have no Ids, that's why you have empty strings.

like image 46
manji Avatar answered Oct 23 '22 02:10

manji


I see a few purely javascript answers. They work but beware, they may not return the items in the order that is visible on the screen. Using the code below, see jtsalva above, will return the items in the proper sorted order. This had me stumped for a while because I wanted to save the new order to a database, so I could reload the grid where someone left off.

var result = $(this).sortable('toArray', {attribute: 'value'});
like image 4
Keith Aymar Avatar answered Oct 23 '22 02:10

Keith Aymar