1

I have a paginator on my website, and I need add a button that allows show all results without pagination.

For the pagination, I have this script:

jQuery(function() 
{
    var items = $("article.inmueble");
    var numItems = items.length;
    var perPage = 5;

    items.slice(perPage).hide();

    jQuery("#paginador").pagination({
        items: numItems,
        itemsOnPage: perPage,
        cssStyle: 'light-theme',
        onPageClick: function(pageNumber) { 
            var showFrom = perPage * (pageNumber - 1);
            var showTo = showFrom + perPage;

            items.hide() 
                 .slice(showFrom, showTo).show();
        }
    });
});

This code is in js/initialize_paginator.js.

In my index.php I call this script:

<script type="text/javascript" src="js/initialize_paginator.js"></script>

And in this index.php I have a button:

<button class="btn btn-default btn-sm">Ver todo</button>

How I can do to, when click this button, change the var perPage = 5; to (for example) var perPage = 999999999; to show all results?

2
  • Thanks for editing jurgemaister and sorry Jan 29, 2015 at 9:22
  • perPage is only available in the specific ready handler scope. You could make global it by removing var statement
    – A. Wolff
    Jan 29, 2015 at 9:23

2 Answers 2

1

Use perPage as global variable outside the jQuery(function()

0

Create a function to initialize the pagination. Something like this

function paginate(items,numItems,perPage)
{
   items.slice(perPage).hide();

jQuery("#paginador").pagination({
    items: numItems,
    itemsOnPage: perPage,
    cssStyle: 'light-theme',
    onPageClick: function(pageNumber) { 
        var showFrom = perPage * (pageNumber - 1);
        var showTo = showFrom + perPage;

        items.hide() 
             .slice(showFrom, showTo).show();
    }
});
}

And then use it like this paginate($("article.inmueble"), $("article.inmueble").length, 5); and reinitialize when needed with updated value, like in your case the click event of the btn with the perPage value 9999.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.