Ngu ngơ học làm web (x21) - CakePHP2 - Element pagination.ctp

Tiếp theo của: Ngu ngơ học làm web (x20) - CakePHP2 – Layout default.ctp
-----

Phần x21. CakePHP2 – Element pagination.ctp


Xem (clip số 24c – chickenrainshop):

Hiển thị 8 quyển sách và thiết lập lại giá trị title cho trang Sách mới:

[BooksController.php, action latest_book()]
            public function latest_books() {
                        $this->paginate = [
                                    'fields' => ['id', 'title', 'slug', 'image', 'sale_price'],
                                    'order' => ['created' => 'desc'],
                                    'limit' => 8,
                                    'contain' => ['Writer' => ['name', 'slug']
                                                ],
                                    'conditions' => ['published' => 1],
                                    'paramType' => 'querystring'
                        ];
                        $books = $this->paginate();
                        $this->set('books', $books);
                        $this->set('title_for_layout', 'Sách mới');
            }

[View\Books\latest_book.ctp]

<div class='panel'>
            <h4 class="panel-heading"><span class="glyphicon glyphicon-th"></span> Sách mới:
            <small class="sorts pull-right"> Sắp xếp theo:
                        <?php echo $this->paginator->sort('title','tên'); ?>
                        <?php echo $this->paginator->sort('created','cũ/mới'); ?>
            </small>
            </h4>
            <?php echo $this->element('book', ['books' => $books]); ?>
</div>
<?php echo $this->element('pagination', ['object' => 'quyển sách']); ?>    

[Elements\pagination.ctp]

            <ul class="pagination">
                        <?php echo $this->Paginator->prev('<<', ['tag' => 'li'], '<<', ['tag' => 'li', 'disabledTag' => 'a', 'class' => 'disabled']); ?>
                        <?php echo $this->Paginator->numbers([
                                    'separator' => '',
                                    'tag' => 'li',
                                    'currentClass' => 'active',
                                    'currentTag' => 'a'
                                    ]); ?>
                        <?php echo $this->Paginator->next('>>', ['tag' => 'li'], '>>', ['tag' => 'li', 'disabledTag' => 'a', 'class' => 'disabled']); ?>
            </ul>

[View\Writers\view.ctp]

<?php if (!empty($books)): ?>
            <div class="panel panel-info">
                        <h4 class="panel-heading"><span class="glyphicon glyphicon-user"></span> <?php echo h($writer['Writer']['name']); ?></h4>
                        <?php echo h($writer['Writer']['biography']); ?>
            </div>
            <div class="panel">
                        <h4 class="panel-heading"><span class="glyphicon glyphicon-th"></span><small> Các sách của tác giả: </small><?php echo h($writer['Writer']['name']); ?></h4>
                        <?php echo $this->element('book', ['books' => $books]); ?>
            </div>
            <?php echo $this->element('pagination'); ?>
<?php endif; ?>

[View\Categories\view.ctp]

<?php if (!empty($books)): ?>
            <div class="panel">
                        <h4 class="panel-heading"><span class="glyphicon glyphicon-th"></span><small> Chuyên mục: </small><?php echo h($category['Category']['name']); ?>
                        <small class="sorts pull-right"> Sắp xếp theo:
                                    <?php echo $this->paginator->sort('title','tên'); ?>
                                    <?php echo $this->paginator->sort('created','cũ/mới'); ?>
                        </small>
                        </h4>
                        <?php echo $this->element('book', ['books' => $books]); ?>
            </div>
            <?php echo $this->element('pagination'); ?>

<?php endif; ?>
-----------
Cập nhật 26/5/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web