2012-12-31 32 views
0

For this question我創建了以下示例解決方案:http://jsfiddle.net/PKcnb/3/只有最後的AJAX請求在加載的表中排序

該代碼通過YouTube API請求50個視頻(由於請求限制)。每個請求將追加一個新的行到最終表。我想要一個簡單的排序解決方案,所以我使用jquery.sortElements.js。

sortElement.js似乎可以工作,但它只是對上一次請求的視頻進行排序。爲什麼整個表沒有排序?搜索周圍,它看起來像我需要implement .live()但我的嘗試一直不成功。

相關的JQuery

// Recursive function to grab the next set of videos 
function getVideos(index, max) { 
    $.ajax({ 
     url: 'https://gdata.youtube.com/feeds/api/playlists/UUAuUUnT6oDeKwE6v1NGQxug?v=2&orderby=duration&max-results=50&start-index=' + index, 
     // 'https://gdata.youtube.com/feeds/api/users/tedtalksdirector/uploads', 
     dataType: "xml", 
     success: function(xml) { 
      var videos = $(xml).find("entry"); 

      videos.each(function() { 
       var title = $(this).find("title").text(); 
       var duration = $(this).find("duration").attr("seconds"); 
       var minutes = Math.floor(duration/60); 
       var seconds = (duration % 60); 

       if (seconds < 10) seconds = "0" + seconds; 

       var newRow = $("<tr></tr>"); 
       newRow.append("<td>" + title + "</td>"); 
       newRow.append("<td class='dur'>" + duration + "</td>"); 
       $("tbody#videos").append(newRow); 

      }); 

      newIndex = index + 50; 

      $('#VideosLoaded').html(newIndex - 1); 

      if (newIndex < max) { 
       getVideos(newIndex, max); 
      } 
     } 
    }); 
} 

// Make table sortable (jquery.sortElements.js) 
// via https://stackoverflow.com/questions/5066002/sending-one-ajax-request-at-a-time-from-a-loop 
var table = $('table'); 
$('#Title, #Duration').wrapInner('<span title="sort this column"/>').each(function() { 

    var th = $(this), 
     thIndex = th.index(), 
     inverse = false; 

    th.click(function() { 

     table.find('td').filter(function() { 

      return $(this).index() === thIndex; 

     }).sortElements(function(a, b) { 

      return $.text([a]) > $.text([b]) ? inverse ? -1 : 1 : inverse ? 1 : -1; 

     }, function() { 
      // parentNode is the element we want to move 
      return this.parentNode; 
     }); 
     inverse = !inverse; 
    }); 
});​ 
+1

最大的問題是,它做的時間列文本排序,而不是數字排序。 –

+0

現場是爲事件處理程序...不適合您的分揀機。只需要在添加新數據後調用排序 – charlietfl

+0

@ Beetroot-Beetroot,就是這個問題。在我添加parseInt()後,表中的所有項目都能正確排序。謝謝你指出我正確的方向。 – JSuar

回答

1

我認爲,排序過程將更好地工作是這樣的:

$('#Title, #Duration').wrapInner('<span title="sort this column"/>').each(function() { 
    var th = $(this), 
     thIndex = th.index(), 
     inverse = false; 
    th.click(function() { 
     table.find('td').filter(function() { 
      return $(this).index() === thIndex; 
     }).sortElements(function(a, b) { 
      var id = th.attr('id'), 
       a = (id === 'Duration') ? parseInt($(a).text()) : $(a).text(), 
       b = (id === 'Duration') ? parseInt($(b).text()) : $(b).text(), 
       x = (a === b) ? 0 : (a > b) ? 1 : -1; 
      return (x === 0) ? 0 : inverse ? -x : x; 
     }, function() { 
      return this.parentNode;  
     }); 
     inverse = !inverse; 
    }); 
}); 

Updated fiddle

+0

你說得對。我非常懶惰,沒有考慮非數字列。 – JSuar

+0

JSuar,讓我感到驚訝的是,考慮到它被視爲數字,你的代碼如何將非數字列排序。我認爲我的版本提供的主要改進是計算'x'和'inverse'的應用,儘管代碼比你的更詳細。 –