ProcessWire伪分页的实现

比如以图片资源为例,如果图片过多在页面中需要分页展示则可以使用以下代码实现:

用于page的images字段

// get the images you are going to display
$items_per_page = 4;
$start = ($input->pageNum - 1) * $items_per_page;
$total = count($page->images);
$images = $page->images->slice($start, $items_per_page);
// make this to give MarkupPagerNav what it needs
$a = new PageArray();
$a->setDuplicateChecking(false); 
// add in some generic placeholder pages
foreach($images as $unused) $a->add(new Page());
// tell the PageArray some details it needs for pagination
// (something that PW usually does internally, for pages it loads)
$a->setTotal($total);
$a->setLimit($items_per_page);
$a->setStart($start);
// output your images
foreach($images as $p) {
$img = $p->img;
echo "<img src='{$img->url}' alt='{$img->description}' />";
}
// output the pagination navigation
echo $a->renderPager();

用于pagearray的page数组

$listed = $pages->find($selector);
$items_per_page = 10;
$start = ($input->pageNum - 1) * $items_per_page;
$total = count($listings);
$listed = array_slice($listings, $start, $items_per_page); // Key difference
$a = new PageArray();
$a->setDuplicateChecking(false);
foreach($listed as $listing) $a->add(new Page());
$a->setTotal($total);
$a->setLimit($items_per_page);
$a->setStart($start);

echo $a->renderPager();

其它对象如PageArray()的分页实现可以参考这里

Post Comment