6

Các cuộc thảo luận bắt đầu jQuery: What to do with the list that sortable('serialize') returns?jQuery: Làm thế nào để đảo ngược sắp xếp ('serialize') mảng từ cuối đến đầu tiên?

Làm thế nào để đảo ngược nó từ trước đến đầu tiên, updateList.php? Id [] = 5 & id [] = 4 & id [] = 3 & id [] = 2 & id [] = 1 & & action = update?

<ul> 
<li id="oreder-5">5</li> 
<li id="oreder-4">4</li> 
<li id="oreder-3">3</li> 
<li id="oreder-2">2</li> 
<li id="oreder-1">1</li> 
<ul> 

Mã của tôi:

$(document).ready(function(){ 
    order=[]; 
    $('#list ul').children('li').each(function(idx, elm) { order.push(elm.id.split('-')[1]) }); 
    $.post('updateList.php', {'order[]': order, action: 'update'}); 

    function slideout(){ 
     setTimeout(function(){ $("#response").slideUp("slow", function() {}); }, 2000); 
    } 
    $("#response").hide(); 
$(function() { 
    $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() { 
    var order = $(this).sortable("serialize") + '&action=update'; 
    $.post("updateList.php", order, function(theResponse){ 
    $("#response").html(theResponse); 
    $("#response").slideDown('slow'); 
    slideout(); 
    }); 
    }});    
}); 
}); 

Trả lời

7

này sẽ làm điều đó:

var reversed = $(this).sortable("serialize").split("&").reverse().join("&"); 
var order = reversed + '&action=update'; 

Nói cách khác:

  • Split chuỗi tại '&'.
  • Reverse mảng kết quả.
  • Join mảng có ký tự '&' để tạo chuỗi được tuần tự đảo ngược.
+0

Cảm ơn, @ karim79! Nó hoạt động tốt cho phần jquery thứ hai với con trỏ: hiệu ứng 'di chuyển', nhưng không hoạt động cho order = []; $ ('# list ul') .mỗi đứa trẻ ('li') mỗi (hàm (idx, elm) {order.push (elm.id.split ('-') [1])}); $ .post ('updateList.php', {'order []': thứ tự, hành động: 'cập nhật'}); Bạn có thể vui lòng nhận giải pháp cho điều đó không !? – Binyamin

+0

Tuyệt vời! Cám ơn vì cái này! –

1

Cần order.reverse(); trước $ .post (..);