JQueryUI sortable thead and tbody shrinked while dragging row with two fields hidden

后端 未结 3 1096
[愿得一人]
[愿得一人] 2021-01-17 17:15

I have a table with different rows and fields. In one row I have two fields with display:none; and when I make the drag of this rows, there is an effect like la

3条回答
  •  -上瘾入骨i
    2021-01-17 17:27

    Having struggled with the sortable plugin myself for the past few days, I think the following changes need to be done:

    1. Add the helper function to create the correct sizes on the helper (draggable object) to have the correct size.

    2. In the start function, add the item html to the placeholder html, to have the placeholder stay identical to the original.

    Code:

    $("#tbodyproject").sortable({
        items: "> tr",
        appendTo: "parent",
        helper: "clone",
        placeholder: "placeholder-style",
        start: function(event, ui) {
          $(this).find('.placeholder-style td:nth-child(2)').addClass('hidden-td')
    
          //copy item html to placeholder html
    
          ui.placeholder.html(ui.item.html());
    
          //hide the items but keep the height/width. 
          ui.placeholder.css('visibility', 'hidden');
        },
        stop: function(event, ui) {
            ui.item.css('display', '')
        },
    
        //add helper function to keep draggable object the same width
        helper: function(e, tr)
        {
            var $originals = tr.children();
            var $helper = tr.clone();
            $helper.children().each(function(index)
            {
            // Set helper cell sizes to match the original sizes
            $(this).width($originals.eq(index).width());
            });
            return $helper;
        },
        update: function( event, ui ) {
            let newOrder = $(this).sortable('toArray');
            $.ajax({
                type: "POST",
                url:'/admin/projects/updateOrder',
                data: {ids: newOrder}
            })
           .done(function( msg ) {
            location.reload();        
           });
        }
    }).disableSelection();
    

    Updated fiddle

提交回复
热议问题