26

I'm trying to get CakePHP's pagination helper to play nicely with bootstrap. That is, I want my pagination elements to look like bootstrap's but generated by CakePHP.

At the moment I've got this on my view page:

<?php
echo $this->Paginator->numbers(array(
    'before' => '<div class="pagination"><ul>',
    'separator' => '',
    'currentClass' => 'active',
    'tag' => 'li',
    'after' => '</ul></div>'
));
?>

Which produces the following markup:

<div class="pagination">
    <ul>
        <li class="active">1</li>
        <li><a href="/test/posts/page:2">2</a></li>
        <li><a href="/test/posts/page:3">3</a></li>
    </ul>
</div>

The problem is, because the active page (1 in this case) doesn't have an <a> element in the <li> tag, it's not displaying correctly on the page (see here: https://i.stack.imgur.com/VKRlW.png).

I can't seem to find anything on the Cookbook that mentions anything that would fix this.

Can this even be fixed?

James Dawson
  • 5,309
  • 20
  • 72
  • 126

12 Answers12

62

I've used the generic functions of cake php html needed to bootstrap.

Gist code: https://gist.github.com/jruzafa/5302941

<div class="pagination pagination-large">
    <ul class="pagination">
            <?php
                echo $this->Paginator->prev(__('prev'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
                echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
                echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
            ?>
        </ul>
    </div>
jruzafa
  • 4,156
  • 1
  • 24
  • 26
  • 4
    This is the best answer, just copied to my page and worked perfect with bootstrap css, thanks ! – dav Jun 19 '13 at 17:19
  • 4
    This works perfectly but for Bootstrap 3 you need to put the class="pagination" in the UL, not in the DIV – Joseph Sep 13 '13 at 06:33
  • 1
    Is there some way to prevent the ellipsis from appearing off the side and de-centering the whole paginator? – user984976 Dec 09 '13 at 02:58
  • if you are using newer versions of cake you need to style the ellipsis (those 3 dots indicating separation between page 1 and the rest). just add `'ellipsis' => '
  • ...
  • '` to the array in Paginator->numbers() – Mauro Zadunaisky Mar 25 '14 at 14:17
  • I have tried it. It work but the current page index show outside the pagination numbers why??? I am using the cakephp 2.3 and bootstrap 3.0 –  May 12 '14 at 09:53
  • 'disabledTag' was added in CakePHP 2.3 – Christian Strang Aug 08 '14 at 19:06