如何使用CakePHP添加分页数,接下来,之前

时间:2015-02-01 20:50:23

标签: php cakephp pagination

我正在使用Cakephp 2.x来尝试分页我的Model的一个索引。我已成功使用Paginator创建我的表格标题。我的标题可以毫无问题地对升序和降序进行排序。当我尝试在表格下方添加$this->Paginator->first$this->Paginator->next$this->Paginator->numbers时,没有可见的Paginator链接。我的数据库现在有6个证据,我将paginator的限制设置为3.这是我的代码:

EvidencesController.php

<?php
    class EvidencesController extends AppController{
        public $components = array('Session','Paginator');
        var $helpers = array( 'Form','Paginator' );

        public function index(){
            $this->Paginator->setting = array(
                'limit' => 3,
                'order' => array(
                    'Evidence.title' => 'asc'
                )
            );
            $data = $this->Paginator->paginate('Evidence');
            $this->set('evidences',$data);
        }
    }   
?>

index.ctp

<h1>Evidences</h1>
<?php echo $this->Html->link('Create a new Evidence', array('controller'=>'evidence','action'=>'add'))?>
<br>
<table>
    <tr>
        <th><?php echo $this->Paginator->sort('title'); ?></th>
        <th><?php echo $this->Paginator->sort('date'); ?></th>
        <th><?php echo $this->Paginator->sort('sourcetype'); ?></th>
        <th><?php echo $this->Paginator->sort('user_id'); ?></th>
        <th><?php echo $this->Paginator->sort('created'); ?></th>
        <?php if(AuthComponent::user('type') == 1) : ?>
        <th>Edit</th>
        <th>Delete</th>
        <?php endif; ?>
        <th>Add</th>
    </tr>
    <?php
        foreach($evidences as $evidence) :
    ?>
    <tr>
        <td><?php echo $this->Html->link($evidence['Evidence']['title'], array('controller' => 'evidences', 'action'=> 'view', $evidence['Evidence']['id'])); ?></td>
        <td><?php echo $evidence['Evidence']['date']; ?></td>
        <td><?php echo $evidence['Evidence']['sourcetype']; ?></td>
        <td><?php echo $evidence['User']['username']; ?></td>
        <td><?php echo $evidence['Evidence']['created']; ?></td>
    </tr>
    <?php
        endforeach;
        //unset($project);
    ?>
</table>
<?php 
    echo "<div class='paging'>";
    echo $this->Paginator->first("First");
    if($this->Paginator->hasPrev()){
        echo $this->Paginator->prev("Prev");
    }
    echo $this->Paginator->numbers(array('modulus' => 2));
    if($this->Paginator->hasNext()){
        echo $this->Paginator->next("Next");
    }
    echo $this->Paginator->last("Last");
    echo "</div>";
?>

如何使用CakePHP的Paginator添加前,后,下,最后和数字?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

通常情况下,我总是使用下面的代码来自cakePHP网站,它始终有效。

视图代码 - &gt;

<?php
if($this->Paginator->counter('{:pages}') > 1) {
    //If disabled,then last para
    echo $this->Paginator->prev('&laquo;', array( 'tag' => 'li', 'escape' => false), null, array('class' => 'prev disabled prv' ,'tag' => 'li', 'escape' => false));
    echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li' ,'currentClass' => 'active', 'currentTag' => 'a' , 'escape' => false));
    echo $this->Paginator->next('&raquo;', array( 'tag' => 'li', 'escape' => false), null, array('class' => 'next disabled nxt' ,'tag' => 'li', 'escape' => false));
}
?>

控制器代码 - &gt;

//Pagination logic
$this->paginate = array('conditions' => $conditions, 'limit' => PAGINATION_LIMIT, 'order' => 'CourseCategory.id DESC');
$getAllCategories = $this->paginate('CourseCategory');
if(isset($this->params['named']['page'])) //FOR RETAINING PAGINATION ON EDIT
    $this->Session->write('pagingUrl',$this->params['named']['page']);

$this->set('getAllCategories',$getAllCategories);
////Pagination logic

&#34; View&#34;的扩展代码如果你想了解更多信息 - &gt;

            <?php 
                if($getAllCategories){
            ?>
        <table id="tablesorter-demo" class="tabelContent tablesorter" width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead>

                <tr>
                    <th class="tabelHeading" width="33%" align="left">Title</td>
                    <th class="tabelHeading" width="33%" align="left">Status</td>
                    <th class="tabelHeading" width="33%" align="left">Actions</td>                                                          
                </tr>   
            </thead>


            <?php 
                $countCategories = 1;
                foreach($getAllCategories as $categorySingle)
                {
                    $grayClass='';
                    if($countCategories%2!=0)
                    {
                        $grayClass= 'class="lightGray"';
                    }   
            ?>

                    <tr>
                        <td <?php echo $grayClass;?>><?php echo $categorySingle['CourseCategory']['category_name'];?></td>


                        <td <?php echo $grayClass;?>>
                            <?php 
                                if($categorySingle['CourseCategory']['is_active'] == 0)
                                { 
                                echo __("Inactive");
                                }
                                else
                                {
                                echo __("Active");
                                }
                            ?>                          
                        </td>           
                        <td <?php echo $grayClass;?>>
                            <?php
                                echo $this->Html->link(
                                $this->Html->image("edit.png", array('alt' => 'edit')),
                                array('controller'=>'CourseCategories' , 'action'=>'edit',  $categorySingle['CourseCategory']['id']),
                                array('class'=>'actions','escape'=>false)                                   
                                );
                            ?>
                            <?php echo $this->Form->postLink(
                             $this->Html->image("delete.png", array('alt' => 'delete')),                                
                            array('action' => 'delete', $categorySingle['CourseCategory']['id'], null, __('Are you sure you want to delete # %s?', $categorySingle['CourseCategory']['id'])),
                             array('class'=>'actions','escape'=>false),
                                // confirmMessage
                                __("Are you sure you want to delete this category?\n\n(Note : All the dependencies will be permanently deleted.)")                                                                   
                            ); ?>                   
                        </td>                                                                                                                       
                    </tr>   




            <?php
                $countCategories += 1;
                }
                ?>
            </table>
            <div class="clear"></div>
            <ul class="pagination centerPaginate">
                <?php
                if($this->Paginator->counter('{:pages}') > 1) {
                    //If disabled,then last para
                    echo $this->Paginator->prev('&laquo;', array( 'tag' => 'li', 'escape' => false), null, array('class' => 'prev disabled prv' ,'tag' => 'li', 'escape' => false));
                    echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li' ,'currentClass' => 'active', 'currentTag' => 'a' , 'escape' => false));
                    echo $this->Paginator->next('&raquo;', array( 'tag' => 'li', 'escape' => false), null, array('class' => 'next disabled nxt' ,'tag' => 'li', 'escape' => false));
                }
                ?>

            </ul>                   


            <?php
            }