1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <script type="text/javascript">
var fixHelper = function(e, ui) { ui.children().each(function() { $(this).width($(this).width()); }); return ui; };
$(function() { $( "#sortable").sortable({ handle:".myhandle", cursor: "move", helper: fixHelper, axis:"y", grid:[300,5], containment:"document", opacity:0.8, start:function(e, ui){ ui.helper.css({"background":"#aaa"}); return ui; }, update:function(e,ui){ var selected_item = ui.item; var selected_sort_id = selected_item.find('.sort_id').val(); selected_sort_id = parseInt(selected_sort_id);
var placed_next_item = selected_item.next('tr'); var placed_next_sort_id = placed_next_item.find('.sort_id').val(); placed_next_sort_id = parseInt(placed_next_sort_id);
var tr,new_sort_id,i,changed_count; if(selected_sort_id>placed_next_sort_id){
changed_count = selected_sort_id - placed_next_sort_id; tr=placed_next_item; new_sort_id = placed_next_sort_id+1; for(i=1;i<=changed_count;i++){ tr.find('.sort_id').attr('value',new_sort_id); tr = tr.next(); new_sort_id++; } selected_item.find('.sort_id').attr('value',placed_next_sort_id); }else{
var placed_prev_item = selected_item.prev('tr'); var placed_prev_sort_id = placed_prev_item.find('.sort_id').val(); placed_prev_sort_id = parseInt(placed_prev_sort_id);
changed_count = placed_prev_sort_id-selected_sort_id; tr = placed_prev_item; new_sort_id = placed_prev_sort_id-1; for(i=1;i<=changed_count;i++){ tr.find('.sort_id').attr('value',new_sort_id); tr = tr.prev(); new_sort_id--; } selected_item.find('.sort_id').attr('value',placed_prev_sort_id); } }, }); }); </script>
|