gpt4 book ai didi

javascript - 按分数对列表进行排序并保持分页

转载 作者:行者123 更新时间:2023-11-28 07:19:59 26 4
gpt4 key购买 nike

我有三个无序列表,其中包含列表项,每个列表包含从 1 到 10 的分数。我有分页,因此页面中有三个相同的 ul 项。

我需要一个按钮,单击该按钮后,按降序数字(即从 10 到 1)对列表项进行排序。

html 是:

<ul class="pagination">
<li class="first active">1</li>
<li class="second">2</li>
<li class="third">3</li>
</ul>
<ul class="sort">
<li class="sortEm">sort by score</li>
</ul>
<div class="block" style="display:block">
<ul class="reviews_list">
<li class="one_review">score:
<div class="score">10</div>
</li>
<li class="one_review">score:
<div class="score">7</div>
</li>
<li class="one_review">score:
<div class="score">9</div>
</li>
<li class="one_review">score:
<div class="score">2</div>
</li>
<li class="one_review">score:
<div class="score">2</div>
</li>
</ul>
</div>
<div class="block">
<ul class="reviews_list">
<li class="one_review">score:
<div class="score">1</div>
</li>
<li class="one_review">score:
<div class="score">2</div>
</li>
<li class="one_review">score:
<div class="score">3</div>
</li>
<li class="one_review">score:
<div class="score">7</div>
</li>
<li class="one_review">score:
<div class="score">9</div>
</li>
</ul>
</div>
<div class="block">
<ul class="reviews_list">
<li class="one_review">score:
<div class="score">10</div>
</li>
<li class="one_review">score:
<div class="score">2</div>
</li>
<li class="one_review">score:
<div class="score">2</div>
</li>
<li class="one_review">score:
<div class="score">1</div>
</li>
<li class="one_review">score:
<div class="score">5</div>
</li>
</ul>
</div>

js 是

$(".pagination li").click(function () {
$(this).toggleClass("active").siblings().removeClass("active");
$(".block").hide();
$(".block").eq($(this).index()).show()
});


$(".sortEm").click(function () {




function sortEm(a, b) {
return parseInt($(".score", a).text()) < parseInt($(".score", b).text()) ? 1 : -1;
}

$("li.one_review").sort(sortEm).prependTo($(".reviews_list"));


});

问题是,使用此功能时,当我单击按钮时,我会丢失分页。这是一个 fiddle :http://jsfiddle.net/3ed03xy9/2/

如何编写 javascript 以便将 li 项目从一个 block 移动到另一个 block ,维护分页并将其从 10 到 1 显示?我不是 js 专家,所以我不知道该怎么做。我现在已经走到这一步了..有人可以帮忙吗?非常感谢:)

最佳答案

您可以将插入排序列表的代码更改为

//store sorted list in variable so we can iterate over it and divide it
//this list combine all '.one_review' (li)s in the 3 reviews_list (ul)s
var sortedList = $("li.one_review").sort(sortEm);

// i iteration variable to let us know where we are in sortedList iteration
// we will increase it every iteration throgh sortedList elements
var i = 0;

// j iteration variable to point to Nth of ul.reviews_list (we have 3 ul.reviews_list)
// we will increase it when i=0, i=5 and i=10
var j = -1;

// the number of items per page (here the sortedList length is 15 and we wanna dived it to 3 pages )
// 15/3 = 5 then round it
var itemsPerPage = Math.round(sortedList.length / 3);

// iterate over each item in sortedList array
// https://api.jquery.com/each/
sortedList.each(function(){

//if reminder of Nth of element / itemsPerPage equal zero that mean
//we need to move to next ul.reviews_list empty it.
if( i % itemsPerPage == 0 ){
//move to next ul.reviews_list
// we will enter here 3 times:
// in first iteration when i=0 then 0%5 = 0 then j will be 0
// in 6th iteration when i=5 then 5%5 = 0 then j will be 1
// in 11th iteration when i=10 then 10%5 = 0 then j will be 2
++j;

// empty current ul.reviews_list
// see : https://api.jquery.com/eq-selector/
// see : http://api.jquery.com/html/
$('.reviews_list:eq('+j+')').html("");
}

// append current li.one_review(this) to current ul.reviews_list (:eq(j))
$('.reviews_list:eq('+j+')').append(this);

++i;
});

http://jsfiddle.net/3ed03xy9/5/

关于javascript - 按分数对列表进行排序并保持分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com