简体   繁体   中英

jQuery save sortable list

I've used this jQuery example: http://jqueryui.com/sortable/#connect-lists-through-tabs

<script>
$(document).ready(function() {
    $(function() {
    $( ".connectedSortable" ).sortable().disableSelection();
    var $tabs = $( "#tabs" ).tabs();

    var $tab_items = $( "ul:first li", $tabs ).droppable({
        accept: ".connectedSortable li",
        hoverClass: "ui-state-hover",
        drop: function( event, ui ) {
            var $item = $( this );
            var $list = $( $item.find( "a" ).attr( "href" ) )
            .find( ".connectedSortable" );

            ui.draggable.hide( "slow", function() {
                $tabs.tabs( "option", "active", $tab_items.index( $item ) );
            $( this ).appendTo( $list ).show( "slow" );
            });
        }
    });
});
  });
</script>

The html:

<div id="tabs">
<ul>
    <li><a href="#Category1">Category1</a></li>
    <li><a href="#Category2">Category2</a></li>
</ul>

<div id="Category1">
    <ul id="sortable-Category1" class="connectedSortable ui-helper-reset">
        <li class="ui-state-default">Forum 1</li>
        <li class="ui-state-default">Forum 2</li>
    </ul>
</div>
<div id="Category2">
    <ul id="sortable-Category2" class="connectedSortable ui-helper-reset">
        <li class="ui-state-default">Forum 3</li>
        <li class="ui-state-default">Forum 4</li>
    </ul>
</div

But now I'd like that when I change something in the list when I reordered an item. I know how to write the changes to the database via AJAX etc, but what method is called when something in the list is dragdropped + how do I implement this in the existing javascript above?

And is there a way that I can get the order of the list in the format '[id-of-the-li-item]-[number in the list]' so that I can use a field in the database named 'order' where the order of the items is specified?

What I want to achieve with this is: I've a forum with Categories & forums. I want to use the code above to order the forums / categories (order of forums in a category and moving forums to other categories)

        $( ".connectedSortable" ).sortable({
            update: function (event, ui) {
                var newSeq = [], p = ui.item.parent(), parentId = p.parent().prop('nodeName') === "LI" ? p.parent().attr('id') : 0;
                ui.item.parent().children().each(function () {
                    newSeq.push(this.id);
                });
                // here you have newSeq... now update it via ajax
            }
        });

This relies on element's ID. There will be ids in newSeq[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM