jquery - 将元素显示为 block 的分页 - CSS Jquery

标签 jquery css filter pagination firebug

我正在尝试对我的内容实现分页,并允许用户过滤掉结果。

这是我创建的:Click Here

问题: 过滤内容时,比如去掉黑色和绿色,一共应该显示4个结果。然而,当这样做时,分页数字不会更新我猜测的 CSS 问题,因为这 4 个结果应该显示在第一页上,而不是在 3 页上。

使用 Firebug 查看 HTML,您可以清楚地看到发生了什么。我将分页设置为每页显示 5 个站点名称。因此,当过滤掉结果时,它会隐藏内容,但分页仍将它们显示为“ block ”,这就是分页仍显示在 3 页而不是 1 页(取消选中黑色和绿色时)的原因。

我已经尝试解决这个问题一段时间了,但找不到解决方案,所以我希望有人能帮助我解决这个问题。

感谢您的帮助。

最佳答案

以下应该可以解决问题.. 让我指出一些我已经改变的东西..

  1. 非常重要的是你是 将功能分配给 paginateIt 中的复选框 函数所以每次运行时(在 每次点击一个复选框)你是 添加额外的点击事件 每个复选框.. 点击几下后 浏览器会开始卡住..

  2. 所以我将点击事件移动到了 在 paginateIt 之外赋值 功能(只有一次)你不需要 识别每个组如果复选框 因为正在进行的 Action 在所有情况下执行的都是相同的 ...我将所有隐藏类更改为 一个名为隐藏的。

  3. $('#content').children().filter 总是会返回所有元素 所以我将其更改为 $('#content div div').filter,但这是特定的 到目前的实现 你的HTML。这正在发生 因为 children() 是 选择器的直接 child 因为你有一个 div 包装 围绕每个最终元素( 一个隐藏或不隐藏的) 它总是会返回最大值 数...


$(document).ready(function(){

function paginateIt(){
 //how much items per page to show
 var show_per_page = 5; 
 //getting the amount of elements inside content div
 var number_of_items = $('#content div div').filter(":not(.hidden)").size();

 //calculate the number of pages we are going to have
 var number_of_pages = Math.ceil(number_of_items/show_per_page);

 //set the value of our hidden input fields
 $('#current_page').val(0);
 $('#show_per_page').val(show_per_page);

 //now when we got all we need for the navigation let's make it '

 /* 
 what are we going to have in the navigation?
  - link to previous page
  - links to specific pages
  - link to next page
 */
 var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a>';
 var current_link = 0;
 while(number_of_pages > current_link){
  navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
  current_link++;
 }
 navigation_html += '<a class="next_link" href="javascript:next();">Next</a>';

 $('#page_navigation').html(navigation_html);

 //add active_page class to the first page link
 $('#page_navigation .page_link:first').addClass('active_page');

 //hide all the elements inside content div
 $('#content div div').filter(":not(.hidden)").css('display', 'none');

 //and show the first n (show_per_page) elements
 $('#content div div').filter(":not(.hidden)").slice(0, show_per_page).css('display', 'block');
}


 $("input:checkbox").click(function() {

   if($(this).is(':checked')) {
    $("#events div."+$(this).attr('id')).removeClass('hidden');
    $("#events div").not(".hidden").show();
   } else {
    $("#events div."+$(this).attr('id')).addClass('hidden');
    $("#events div."+$(this).attr('id')).hide();
   }
   paginateIt();
  });

 paginateIt();

});

function previous(){

 new_page = parseInt($('#current_page').val()) - 1;
 //if there is an item before the current active link run the function
 if($('.active_page').prev('.page_link').length==true){
  go_to_page(new_page);
 }

}

function next(){
 new_page = parseInt($('#current_page').val()) + 1;
 //if there is an item after the current active link run the function
 if($('.active_page').next('.page_link').length==true){
  go_to_page(new_page);
 }

}
function go_to_page(page_num){
 //get the number of items shown per page
 var show_per_page = parseInt($('#show_per_page').val());

 //get the element number where to start the slice from
 start_from = page_num * show_per_page;

 //get the element number where to end the slice
 end_on = start_from + show_per_page;

 //hide all children elements of content div, get specific items and show them
 $('#content div div').filter(":not(.hidden)").css('display', 'none').slice(start_from, end_on).css('display', 'block');

 /*get the page link that has longdesc attribute of the current page and add active_page class to it
 and remove that class from previously active page link*/
 $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');

 //update the current page input field
 $('#current_page').val(page_num);
} 

关于jquery - 将元素显示为 block 的分页 - CSS Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2003442/

相关文章:

python - Django:如何从与用户相关的子查询集中获取不同的父列表?

android - 带有日期分隔符的 GridView

Javascript parseInt 总是返回 NaN

css - 图片未垂直对齐

html - 如何将绝对子项的垂直位置与父项 div 对齐

javascript - VideoJS 分辨率选择器在 Wordpress 中不起作用

.net - .NET 跟踪中的 "switch"和 "filter"有什么区别?

jquery - 如何在jquery中指定的div中加载图像

javascript - Rails 4 ajax 更新 div

javascript - 我如何加载外部 html 并使其与 jquery 一起工作