Zend pagination with limited number of links and dots between

3 min read 07-10-2024
Zend pagination with limited number of links and dots between


Navigating Large Datasets with Grace: Zend Framework Pagination and Link Optimization

Pagination is crucial when dealing with large datasets to prevent overwhelming users with information. While Zend Framework's built-in pagination functionality is powerful, displaying every single page link can be visually cumbersome and impractical. This article dives into optimizing your Zend pagination, focusing on limiting the number of displayed links and strategically implementing dots (ellipses) for better user experience.

The Problem: Too Many Links, Too Much Clutter

Imagine a website displaying thousands of products. Using Zend Framework's standard pagination, the resulting page navigation could be a long, monotonous list of page numbers, confusing users and discouraging exploration. Here's a simplified example:

use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;

$items = array_fill(0, 1000, 'Product'); // Simulate 1000 products
$adapter = new ArrayAdapter($items);
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber(5); // Currently on page 5

echo $paginator->getPages();

This code would generate a navigation with 1000 page links, which is clearly impractical.

The Solution: Tailored Navigation with Dots

Instead of displaying all page numbers, we can create a more concise navigation by:

  • Limiting the number of visible links: Display only a few page numbers around the current page.
  • Using dots (ellipses) to represent skipped pages: Indicate that other pages exist between visible ones.

Here's how to achieve this using Zend Framework's Paginator class:

// ... (previous code) ...

$paginator->setCurrentPageNumber(5); 
$paginator->setItemCountPerPage(10);  // Display 10 items per page
$paginator->setPagesToShow(5);      // Show 5 links at most

echo $paginator->getPages();

In this modified code:

  • setItemCountPerPage(10) sets the number of items displayed per page.
  • setPagesToShow(5) limits the number of visible page links to 5.

This would result in a more user-friendly navigation like:

1 ... 3 4 5 6 7 ... 100

Implementing Custom Page Link Generation

Zend Framework offers flexibility for customizing the output of your pagination. You can create a custom view helper or a template to generate the page links. For instance, you can use a custom view helper to achieve the desired outcome:

class CustomPaginationHelper extends AbstractHelper
{
    public function __invoke(Paginator $paginator)
    {
        $html = '';
        $currentPage = $paginator->getCurrentPageNumber();
        $totalPages = $paginator->count();

        // Display "Previous" link
        if ($currentPage > 1) {
            $html .= '<a href="' . $paginator->getPreviousPageUrl() . '">Previous</a>';
        }

        // Display first few pages
        for ($i = 1; $i <= 3 && $i <= $totalPages; $i++) {
            if ($i == $currentPage) {
                $html .= '<span class="active">' . $i . '</span>';
            } else {
                $html .= '<a href="' . $paginator->getPages()->getLink($i) . '">' . $i . '</a>';
            }
        }

        // Display ellipses if necessary
        if ($currentPage > 4 && $totalPages > 5) {
            $html .= ' ... ';
        }

        // Display middle pages (around current page)
        $maxPages = min($totalPages, $currentPage + 2);
        for ($i = max(1, $currentPage - 2); $i <= $maxPages; $i++) {
            if ($i == $currentPage) {
                $html .= '<span class="active">' . $i . '</span>';
            } else {
                $html .= '<a href="' . $paginator->getPages()->getLink($i) . '">' . $i . '</a>';
            }
        }

        // Display ellipses if necessary
        if ($totalPages > $maxPages && $totalPages > 5) {
            $html .= ' ... ';
        }

        // Display last few pages
        for ($i = max(1, $totalPages - 2); $i <= $totalPages; $i++) {
            if ($i == $currentPage) {
                $html .= '<span class="active">' . $i . '</span>';
            } else {
                $html .= '<a href="' . $paginator->getPages()->getLink($i) . '">' . $i . '</a>';
            }
        }

        // Display "Next" link
        if ($currentPage < $totalPages) {
            $html .= '<a href="' . $paginator->getNextPageUrl() . '">Next</a>';
        }

        return $html;
    }
}

This custom helper dynamically constructs the pagination links, displaying only a limited number of pages while strategically adding dots for clarity.

Conclusion

Optimizing pagination with Zend Framework is crucial for providing a seamless user experience when navigating large datasets. By limiting the number of visible page links and strategically utilizing dots, you can create a cleaner, more user-friendly interface that enhances the overall browsing experience. Remember, a well-designed pagination system should be both functional and visually appealing.