Jquery sortable 'change' event element position

后端 未结 5 2034
生来不讨喜
生来不讨喜 2020-11-30 00:23

Is there way to get current position of helper being dragged on over new position ?

$(\"#sortable\").sortable({
        start: function (event, ui) {
                


        
相关标签:
5条回答
  • 2020-11-30 00:44

    Use update, stop and receive events, check it over here

    Jquery Sortable Update Event can called only one time?

    0 讨论(0)
  • 2020-11-30 00:55

    If anyone is interested in a sortable list with a changing index per listitem (1st, 2nd, 3th etc...:

    http://jsfiddle.net/aph0c1rL/1/

    $(".sortable").sortable(
    {
      handle:         '.handle'
    , placeholder:    'sort-placeholder'
    , forcePlaceholderSize: true
    , start: function( e, ui )
    {
        ui.item.data( 'start-pos', ui.item.index()+1 );
    }
    , change: function( e, ui )
      {
          var seq
          , startPos = ui.item.data( 'start-pos' )
          , $index
          , correction
          ;
    
          // if startPos < placeholder pos, we go from top to bottom
          // else startPos > placeholder pos, we go from bottom to top and we need to correct the index with +1
          //
          correction = startPos <= ui.placeholder.index() ? 0 : 1;
    
          ui.item.parent().find( 'li.prize').each( function( idx, el )
          {
            var $this = $( el )
            , $index = $this.index()
            ;
    
            // correction 0 means moving top to bottom, correction 1 means bottom to top
            //
            if ( ( $index+1 >= startPos && correction === 0) || ($index+1 <= startPos && correction === 1 ) )
            {
              $index = $index + correction;
              $this.find( '.ordinal-position').text( $index + ordinalSuffix( $index ) );
            }
    
          });
    
          // handle dragged item separatelly
          seq = ui.item.parent().find( 'li.sort-placeholder').index() + correction;
          ui.item.find( '.ordinal-position' ).text( seq + ordinalSuffix( seq ) );
    } );
    
    // this function adds the correct ordinal suffix to the provide number
    function ordinalSuffix( number )
    {
      var suffix = '';
    
      if ( number / 10 % 10 === 1 )
      {
        suffix = "th";
      }
      else if ( number > 0 )
      {
    
        switch( number % 10 )
        {
          case 1:
            suffix = "st";
            break;
          case 2:
            suffix = "nd";
            break;
          case 3:
            suffix = "rd";
            break;
          default:
            suffix = "th";
            break;
        }
      }
      return suffix;
    }
    

    Your markup can look like this:

    <ul class="sortable ">
    <li >        
        <div>
            <span class="ordinal-position">1st</span>
             A header
        </div>
        <div>
            <span class="icon-button handle"><i class="fa fa-arrows"></i></span>
        </div>
        <div class="bpdy" >
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        </div>
    </li>
     <li >        
        <div>
            <span class="ordinal-position">2nd</span>
             A header
        </div>
        <div>
            <span class="icon-button handle"><i class="fa fa-arrows"></i></span>
        </div>
        <div class="bpdy" >
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        </div>
    </li>
    etc....
    </ul>
    
    0 讨论(0)
  • 2020-11-30 01:00
    $( "#sortable" ).sortable({
        change: function(event, ui) {       
            var pos = ui.helper.index() < ui.placeholder.index() 
                ? { start: ui.helper.index(), end: ui.placeholder.index() }
                : { start: ui.placeholder.index(), end: ui.helper.index() }
    
            $(this)
                .children().removeClass( 'highlight' )
                .not( ui.helper ).slice( pos.start, pos.end ).addClass( 'highlight' );
        },
        stop: function(event, ui) {
            $(this).children().removeClass( 'highlight' );
        }
    });
    

    FIDDLE

    An example of how it could be done inside change event without storing arbitrary data into element storage. Since the element where drag starts is ui.helper, and the element of current position is ui.placeholder, we can take the elements between those two indexes and highlight them. Also, we can use this inside handler since it refers to the element that the widget is attached. The example works with dragging in both directions.

    0 讨论(0)
  • 2020-11-30 01:03

    UPDATED: 26/08/2016 to use the latest jquery and jquery ui version plus bootstrap to style it.

    • demo: http://so.lucafilosofi.com/jquery-sortable-change-event-element-position/
    $(function() {
        $('#sortable').sortable({
            start: function(event, ui) {
                var start_pos = ui.item.index();
                ui.item.data('start_pos', start_pos);
            },
            change: function(event, ui) {
                var start_pos = ui.item.data('start_pos');
                var index = ui.placeholder.index();
                if (start_pos < index) {
                    $('#sortable li:nth-child(' + index + ')').addClass('highlights');
                } else {
                    $('#sortable li:eq(' + (index + 1) + ')').addClass('highlights');
                }
            },
            update: function(event, ui) {
                $('#sortable li').removeClass('highlights');
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-30 01:07

    This works for me:

    start: function(event, ui) {
            var start_pos = ui.item.index();
            ui.item.data('start_pos', start_pos);
        },
    update: function (event, ui) {
            var start_pos = ui.item.data('start_pos');
            var end_pos = ui.item.index();
            //$('#sortable li').removeClass('highlights');
        }
    
    0 讨论(0)
提交回复
热议问题