Jump to content

Pagination ignoring current page


heldercervantes
 Share

Recommended Posts

Hi guys

I'm having a weird issue with pagination. This is a not too straightforward search that in some circumstances even joins two pagearrays. The pagination renders correctly, with working links, whitelists the variables, the items list is correct as I navigate the pages but... for some reason the pagination always marks the first page as being the current one even if I go to the second, third, and so on.

Here's my abbridged code:

  $results = ** a somewhat complex search that returns a pagearray **

  $perPage = 20;
  $results->setLimit($perPage);
  if ($input->get->f) $input->whiteList('f', $input->get->f);
  if ($input->get->l) $input->whiteList('l', $input->get->l);
  if ($input->get->a) $input->whiteList('a', $input->get->a);
  if ($input->get->t) $input->whiteList('t', $input->get->t);
  $pagination = $results->renderPager([
    'nextItemClass' => 'next',
    'previousItemClass' => 'previous',
    'currentItemClass' => 'current',
    'separatorItemClass' => 'separator',
    'numPageLinks' => 5
  ]);

  $start = ($input->pageNum-1) * $perPage;

  foreach($results->slice($start, $perPage) as $item) {
    // render the thing
  }

  echo $pagination;

Any ideas?

Thanks

Link to comment
Share on other sites

This sounds eerily familiar. Some  situations where this might happen are if you have a "start" specified in your selector, if page numbers are not enabled for the template, or if you have multiple pagers resulting in some kind of conflict.

If nothing else helps but $input->pageNum has the correct value, you could perhaps go at this the other way around — not tested, but if you instantiate MarkupPagerNav manually you can try to force the page num:

  $results = ** a somewhat complex search that returns a pagearray **

  $perPage = 20;
  $results->setLimit($perPage);
  if ($input->get->f) $input->whiteList('f', $input->get->f);
  if ($input->get->l) $input->whiteList('l', $input->get->l);
  if ($input->get->a) $input->whiteList('a', $input->get->a);
  if ($input->get->t) $input->whiteList('t', $input->get->t);
  $pager = $modules->get('MarkupPagerNav');
  $pager->setPageNum($input->pageNum);
  $pagination = $pager->render($results, [
    'nextItemClass' => 'next',
    'previousItemClass' => 'previous',
    'currentItemClass' => 'current',
    'separatorItemClass' => 'separator',
    'numPageLinks' => 5
  ]);

  $start = ($input->pageNum-1) * $perPage;

  foreach($results->slice($start, $perPage) as $item) {
    // render the thing
  }

  echo $pagination;

 

  • Like 1
Link to comment
Share on other sites

47 minutes ago, teppo said:

This sounds eerily familiar. Some  situations where this might happen are if you have a "start" specified in your selector, if page numbers are not enabled for the template, or if you have multiple pagers resulting in some kind of conflict.

If nothing else helps but $input->pageNum has the correct value, you could perhaps go at this the other way around — not tested, but if you instantiate MarkupPagerNav manually you can try to force the page num:

No cigar.

"Start" is only used on that "slice" when I'm rendering the results. Page numbers are enabled on the template and there's only one pager in the page.

I also double-checked, and $input->pageNum outputs the correct value. 

Got to keep hacking at it ?

Link to comment
Share on other sites

Found the solution. Thanks @teppo for your help. Your suggestion gave me the clue I needed. Here's the fixed code:

 

  $results = ** a somewhat complex search that returns a pagearray **

  $perPage = 20;
  $start = ($input->pageNum -1) * $perPage;
  $results->setLimit($perPage);
  $results->setStart($start);
  if ($input->get->f) $input->whiteList('f', $input->get->f);
  if ($input->get->l) $input->whiteList('l', $input->get->l);
  if ($input->get->a) $input->whiteList('a', $input->get->a);
  if ($input->get->t) $input->whiteList('t', $input->get->t);
  $pager = $modules->get('MarkupPagerNav');
  $pager->setPageNum($input->pageNum);
  $pagination = $pager->render($results, [
    'nextItemClass' => 'next',
    'previousItemClass' => 'previous',
    'currentItemClass' => 'current',
    'separatorItemClass' => 'separator',
    'numPageLinks' => 5
  ]);

  foreach($results->slice($start, $perPage) as $item) {
    // render the thing
  }

  echo $pagination;

Basically all that was missing was that setStart() call. Makes sense, as usual.

  • Like 4
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...