0

hi i am using jquery ui sortable to sort my divs . i can get the div oder to save in the db , this is my code

<div style="margin:0 auto !important;">
    <ul id="sortable1" class="connectedSortable">
        <li id="one_1" class="ui-state-default"><div>Item 1</div></li>
        <li id="one_2" class="ui-state-default"><div>Item 2</div></li>
        <li id="one_3" class="ui-state-default"><div>Item 3</div></li>
        <li id="one_4" class="ui-state-default"><div>Item 4</div></li>
        <li id="one_5" class="ui-state-default"><div>Item 5</div></li>
    </ul>

    <ul id="sortable2" class="connectedSortable">
        <li id="two_1" class="ui-state-highlight"><div>Item 1</div></li>
        <li id="two_2" class="ui-state-highlight"><div>Item 2</div></li>
        <li id="two_3" class="ui-state-highlight"><div>Item 3</div></li>
        <li id="two_4" class="ui-state-highlight"><div>Item 4</div></li>
        <li id="two_5" class="ui-state-highlight"><div>Item 5</div></li>
    </ul>
</div>



<script type="text/javascript">
  jQuery(function() {

    var sortable1 = 'one_1,one_2,one_3,one_4,one_5';
    var sortable2 = 'two_1,two_2,two_3,two_4,two_5';

    jQuery( "#sortable1, #sortable2" ).sortable({
        update: function(event, ui) {

            var newOrder = jQuery(this).sortable('toArray').toString();
            if(jQuery(this).attr('id')=="sortable1"){
                sortable1 = newOrder;
            }
            else {
                sortable2 = newOrder;
            }

            console.log(sortable1);
            console.log(sortable2);
        }

    }).disableSelection();
  });
  </script>

i can save the sorted div order to DB correctly , but i have no idea how to populate the div's to correct order again . please help me . thanks in advance .

3
  • You would need to provide the php code that populates the div. Commented Feb 18, 2013 at 6:00
  • How are you saving the sort order in the DB? Commented Feb 18, 2013 at 6:04
  • as a comma separated string vaue -> 1,2,3,4,5 Commented Feb 18, 2013 at 6:13

1 Answer 1

1

Instead of saving the sort order to the DB as the comma separated value 1,2,3,4,5, why not just save the 'sort column' e.g. #sortable1 such that when you are loading the divs you just pass this value to your js logic.

Example:

if you save the sort column name say #sortable1 or #sortableN in the DB then you end up with

    var newOrder = jQuery(this).sortable('toArray').toString();
    if(jQuery(this).attr('id')== "<% $your_php_variable %>"){
        sortable1 = newOrder;
    }
    else {
        sortable2 = newOrder;
    }

Am not sure how you are passing the php variable to the UI that's what i've put as <% $your_php_variable %>. At some point in your php code you will have set $your_php_variable = '#sortableX';

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.