Updating sort order during sort change event - jQuery UI -


i want value of list element index of sorted position during sort event.

this value should update automatically during sort change event.

        <script type="text/javascript">         $(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 + ')').html(index-2);                     } else {                         $('#sortable li:eq(' + (index + 1) + ')').html(index + 1);                     }                 },                 update : function(event, ui) {                     var index = ui.item.index();                     $('#sortable li:nth-child(' + (index + 1) + ')').html(index);                  },                 axis : 'y'             });         });       </script> 

i created fiddle http://jsfiddle.net/jagan2explore/4mcpq/

to explain requirement.

if move 1'st element 5th position other elements values updated rightly, if move 5th 1'st value updates accordingly.

suppose if move list element 1 5 & 5 2 without leaving (during single sort event ), values not updated accordingly.

am missing something??

any appreciated. in advance

try this:

update : function(event, ui) {     var index = ui.item.index();     var start_pos = ui.item.data('start_pos');      //update html of moved item current index     $('#sortable li:nth-child(' + (index + 1) + ')').html(index);      if (start_pos < index) {         //update items before re-ordered item         for(var i=index; > 0; i--){             $('#sortable li:nth-child(' + + ')').html(i - 1);         }     }else {         //update items after re-ordered item         for(var i=index+2;i <= $("#sortable li").length; i++){             $('#sortable li:nth-child(' + + ')').html(i-1);         }     } }, 

demo: jsfiddle


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -