分页显示元素作为块 - CSS Jquery

时间:2010-01-05 00:51:02

标签: jquery css filter pagination firebug

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

以下是我创建的内容:Click Here

问题: 过滤内容时,例如取出黑色和绿色的颜色应总共显示4个结果。但是,在执行此操作时,分页编号不会更新为CSS问题,因为这4个结果应显示在第一页上,而不是3页。

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

我一直试图解决这个问题一段时间后无法找到解决方案,所以我希望有人可以帮我解决这个问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

以下应该做的伎俩.. 让我指出一些我改变过的事情。

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

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

  3. $('#content').children().filter 总会返回所有元素 所以我把它改成$('#content div div').filter,但这是具体的 到目前的实施 你的HTML。这发生了 因为children()是 选择器的直接子项 因为你有一个div包裹 围绕每一个最终元素( 一个隐藏或不隐藏的) 它总会返回最大值 号码......

  4. 
    $(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);
    }