If someone is interested in a sorted list with a changing index for the list (1st, 2nd, 3rd, 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 might 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>
Mattijs Oct 29 '15 at 11:28 2015-10-29 11:28
source share