Jump to content

Pagination for pages stored in Page field?


CalleRosa40
 Share

Recommended Posts

Thanks, LostKobrakai.
 
Hm. Then do you or does anyone have any thoughts on how to solve what I am trying to do without using the Page field type?

My scenario (quite simple and common, I guess, however I'm stuck): I want to feature a user-defined set of pages on the starting page of my site. These pages come from different categories all over the site. Users should be able to change the order of items in a simple and user-friendly way in the backend. I don't want copies of the starting-page items, but rather a simple way to link to the original pages and put them together on the starting page. Using a Page field (with PageListSelectMultiple selector) seemed like a good idea, but if I have to write my own code for pagination (which, I assume, isn't something I could whip up in an hour or so), this doesn't work for me.

Thanks so much for helping out.

Link to comment
Share on other sites

The question is to how many of those "features" you need this to scale. Are we talking about 10s, 100s or 1000s of them? Just implementing pagination for a pagearray is quite easy, as long as you've no problem laoding the whole pagearray into memory each time.

Link to comment
Share on other sites

Are we talking about 10s, 100s or 1000s of them?

I'd put it at 100 starting-page items max (older items will be removed manually once in a while). I'm definitely not a pro programmer, so writing my own code to display a paginated PageArray seems like a daunting task to me. However, I'm willing to learn. Any hints or links on how to do this the "quite easy" way?

Link to comment
Share on other sites

$pageArray = $page->your_field;

$total = $pageArray->count();
$limit = 20;
$pageNum = $input->pageNum - 1; // make it zero based for calculation
$start = $pageNum * $limit; 

$paginatablePageArray = $pageArray->setStart($start)->setLimit($limit)->setTotal($total);

This way you'll get a PageArray, which will work like any $pages->find() one in terms of pagination.

  • Like 4
Link to comment
Share on other sites

Cool, thank you very much. "Quite easy" indeed. Automatic HTML/CSS generation via $paginatedPageArray->renderPager(); works this way, but if you display the array, it always contains the full set of starting-page items on any of the paginated pages. I guess this has something to do with there being no real selector the array was "built" with (as it would have using $pages->find(), cf. http://cheatsheet.processwire.com/pagearray-wirearray/pagearray-only/a-getselectors/).

I added

$outputPageArray = new PageArray();

for($i = $start; $i < $start + $limit; $i++)
    $outputPageArray->add($paginatedPageArray[$i]);

after your snippet to create a new output array that contains only a subset of pages (e.g. 1-10, 11-20 etc.). So far, this seems to work fine.

Link to comment
Share on other sites

I added

$outputPageArray = new PageArray();

for($i = $start; $i < $start + $limit; $i++)
    $outputPageArray->add($paginatedPageArray[$i]);

after your snippet to create a new output array that contains only a subset of pages (e.g. 1-10, 11-20 etc.). 

You could simple use $pageArray->slice($start, $limit);

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

I had this exact same issue. I wonder why it's not more common to need, given the almost necessary use of a multi-page field in most sites.

In any case, I solved it easy enough while maintaining all usual PW pagination tools, simply by using the normal find() anyway!

$items = $pages->find("limit=10, id=". $page->page_reference);

This returns functional paginated results and works fine.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...