CalleRosa40 Posted June 6, 2016 Share Posted June 6, 2016 Hi! Is there any way to make PW paginate a PageArray that was not created via e.g. $pages->find()? I'm thinking specifically of the PageArray that is returned when accessing a field of type Page. Thanks a lot! Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 7, 2016 Share Posted June 7, 2016 Nope. At least you cannot prevent the field from loading all pages in the first place. You could still choose to show only parts of them, by manually setting setLimit() setTotal() setStart() on the pagearray. Link to comment Share on other sites More sharing options...
CalleRosa40 Posted June 7, 2016 Author Share Posted June 7, 2016 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 More sharing options...
LostKobrakai Posted June 7, 2016 Share Posted June 7, 2016 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 More sharing options...
CalleRosa40 Posted June 7, 2016 Author Share Posted June 7, 2016 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 More sharing options...
LostKobrakai Posted June 7, 2016 Share Posted June 7, 2016 $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. 4 Link to comment Share on other sites More sharing options...
CalleRosa40 Posted June 7, 2016 Author Share Posted June 7, 2016 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 More sharing options...
LostKobrakai Posted June 7, 2016 Share Posted June 7, 2016 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); 3 Link to comment Share on other sites More sharing options...
CalleRosa40 Posted June 7, 2016 Author Share Posted June 7, 2016 Ha, even better. Great. Many thanks! Link to comment Share on other sites More sharing options...
Vigilante Posted December 28, 2017 Share Posted December 28, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now