【问题标题】:Pagination displaying elements as block - CSS Jquery将元素显示为块的分页 - CSS Jquery
【发布时间】:2010-01-06 06:06:44
【问题描述】:

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

这是我创建的:Click Here

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

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

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

感谢您的帮助。

【问题讨论】:

  • 我要过滤的内容具有相同的 id 名称是否与此问题有关?

标签: jquery css filter pagination firebug


【解决方案1】:

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

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

  2. 所以我将点击事件移动到 分配在paginateIt 之外 您不需要的功能(仅一次) 如果复选框识别每个组 因为正在发生的动作 在所有情况下执行的都是相同的 ...我将所有隐藏类更改为 一个名为 hidden。

  3. $('#content').children().filter 将始终返回所有元素 所以我把它改成了$('#content div div').filter,但这是特定的 对目前的实施 你的html。这正在发生 因为children() 是 选择器的直接子代 因为你有一个 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);
} 

【讨论】:

  • 好的,我很抱歉......由于 StackOverflow 解析了实际标签,某些部分(创建导航标签的地方)被吃掉了......(我已经编辑了原始答案)现在参加那部分,应该没问题
  • 试过上面的脚本,分页成功了!但是当过滤例如颜色时,如果我只取消选中蓝色和绿色并保留黑色,它会显示 2 个结果,而它应该总共显示 5 个(所有黑色)。我猜它只是过滤页面而不显示为其他项目的块。这个问题有解决方案吗? - 这是更新后的脚本 -> jsbin.com/ekexe
  • 代码中有 3 个地方我没有将 ("#content").children().filter 更改为 ("#content div div").filter(现已更新)跨度>
  • 请记住,您的代码逻辑当前与每个过滤器本身不对齐(它不会组合它们)。例如,如果您取消选择所有颜色,它将显示 0 个项目,但如果您取消选择然后重新选择汽车过滤器,它将显示所有汽车项目,即使它们的颜色应该是隐藏的)
  • 效果很好,谢谢。关于过滤器的有趣点,我没有意识到。如果您有任何关于使其结构更好的建议,那就太好了,再次感谢您的帮助。
猜你喜欢
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2020-09-21
相关资源
最近更新 更多