2013-05-01 154 views
1

我有一个评论列表,每个评论有一个投票计数(正面或负面)。我试图拉出前两个评论(基于最高净票数,不是票数),复制他们的HTML,并将它们添加到标题为“顶级创意”的新部分如何根据子元素值对父元素进行排序?

我想复制整个与在

HTML最多的两点意见(过度简化的版本)...这是重复为每个评论:

的jQuery:

jQuery(document).ready(function($) { 
    //number of top comments to show 
    var showResults = 2; 

    //loop through each total rating 
    //only pull the top two (in this case) 
    $('span.num-total').slice(0, showResults).each(function(index){ 

     //calculate the net vote total based on data-voteup and data-votedown 
     var upVotes = $(this).data('voteup'); 
     var downVotes = $(this).data('votedown'); 
     var netVotes = upVotes - downVotes; 

     //get the HTML for those comments 
     var commentHTML = $(this).parents('.comment').html(); 

     //append that HTML to the top comment div 
     $('div.top-comments').append('<div class="comment">' + commentHTML + '</div>'); 

    }); 
}); 

见住在这里的副本:http://jobelty.com/company/apple
jQuery的是从一个名为未来顶级comments.js

+0

我的代码比选择的答案更短,更具可读性。如果它不是逐字地运行的,你应该真正弄清楚如何使它工作。 – Jonah 2013-05-01 01:52:42

回答

1

你下来切割列表之前排序,所以充其量,你会得到两者的两点意见发生文本在顶部。

,抓住两个排名最高的评论全部评论元素,并将其复制那些成.top-comments一个版本:

jQuery(document).ready(function ($) { 
    //number of top comments to show 
    var showResults = 2; 

    var ordered = []; 

    // grab all the comments 
    var comments = $('.commentlist .comment'); 

    $.each(comments, 

    function (i, v) { 
     // for each comment 
     var cmt = $(v); 
     var nums = cmt.find('.num-total'); 
     var upVotes = nums.data('voteup'); 
     var downVotes = nums.data('votedown'); 
     var netVotes = upVotes - downVotes; 

     var pos = ordered.length; 

     // find the first place in the ordered list it fits 
     for (var j = 0; j < ordered.length; ++j) { 
      if (ordered[j].votes < netVotes) { 
       pos = j; 
       break; 
      } 
     } 

     // save element and count for later 
     var vote = { 
      'votes': netVotes, 
      'cmt': cmt 
     }; 

     ordered[pos] = vote; 
    }); 

    var n = Math.min(2, ordered.length); 

    // grab the first (up to) 2 and append to .top-comments  
    for (var i = 0; i < n; ++i) { 
     ordered[i].cmt.clone().appendTo($('.top-comments')); 
    } 
}); 
+0

美女!谢谢! – 2013-05-01 01:51:05

0

从我可以告诉你拥有了一切,除了一部分工作,你确定是什么前两个评论是。这是一种基于首先对评论元素进行排序的方法。代码是未经测试:

jQuery(document).ready(function($) { 
    //number of top comments to show 
    var showResults = 2; 

    var netVotes = function(domElm) {   
     var upVotes = domElm.data('voteup'); 
     var downVotes = domElm.data('votedown'); 
     return upVotes - downVotes; 
    }; 

    var sortedComments = $('span.num-total').slice(0).sort(function(a,b) { 
     return netVotes(a) - netVotes(b); 
    }); 

    sortedComments.slice(0, showResults).each(function(index){ 
     var commentHTML = $(this).parents('.comment').html(); 
     $('div.top-comments').append('<div class="comment">' + commentHTML + '</div>'); 
    }); 
}); 
0

尝试

$(function(){ 
    var voteels = $('.comment .num-total'); 

    var array = voteels.get(); 
    array.sort(function(a1, a2){ 
     var v1 = ($(a1).data('voteup') || 0) - ($(a1).data('votedown') || 0); 
     var v2 = ($(a2).data('voteup') || 0) - ($(a2).data('votedown') || 0); 

     return v1 < v2; 
    }); 
    $(array.slice(0, 2)).closest('.comment').clone().appendTo('#top') 
}) 

演示:Fiddle

相关问题