【问题标题】:How to add Pagination numbers, next, previous with CakePHP如何使用 CakePHP 添加分页号、下一个、上一个
【发布时间】:2015-02-01 20:50:22
【问题描述】:

我正在使用 Cakephp 2.x 尝试对我的模型索引之一进行分页。我已成功使用 Paginator 创建表格标题。我的标题能够毫无问题地对升序和降序进行排序。当我尝试在表格下方添加$this->Paginator->first$this->Paginator->next$this->Paginator->numbers 时,没有可见的分页器链接。我的数据库中现在有 6 个证据,我将分页器的限制设置为 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 添加第一个、最后一个、下一个、最后一个和数字计数?

任何帮助将不胜感激!

【问题讨论】:

    标签: php cakephp pagination


    【解决方案1】:

    通常我总是使用以下来自 cakePHP 网站的代码,并且它一直有效。

    查看代码 ->

    <?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));
    }
    ?>
    

    控制器代码 ->

    //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
    

    “查看”的扩展代码,如果您想了解更多信息 ->

                <?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
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多