Pagination for a table contents in yii

2019-09-09 21:34发布

I'm using Yii application. In that how to give pagination to a table contents (not using cgridview).

In view,

<table data-provides="rowlink" data-page-size="20" data-filter="#mailbox_search" class="table toggle-square default footable-loaded footable line-content" id="mailbox_table">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th data-hide="phone,tablet">From</th>
                                        <th>Subject</th>
                                        <th data-hide="phone" class="footable-last-column">Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach ($model as $item) {
                                        if ($item->fromuser->usertypeid === '0') {
                                            $name = $item->fromuser->username;
                                        } else if ($item->fromuser->usertypeid === '1') {
                                            $student = Student::model()->findByPk($item->fromuser->userid);
                                            $name = $student->student_admissionno . " - " . $student->student_firstname . " " . $student->student_middlename . " " . $student->student_lastname;
                                        } else if ($item->fromuser->usertypeid === '3') {
                                            $guardian = Guardian::model()->findByPk($item->fromuser->userid);
                                            $name = $guardian->guardian_name;
                                        } else {
                                            $employee = Employeemaster::model()->findByPk($item->fromuser->userid);
                                            $name = $employee->employee_code . " - " . $employee->employee_firstname . " " . $employee->employee_middlename . " " . $employee->employee_lastname;
                                        }

                                        if ($item->email_status == 1)
                                            echo '<tr id="' . $item->emailid . '" class="unreaded rowlink" style="display: table-row;">';
                                        else
                                            echo '<tr id="' . $item->emailid . '" class="rowlink" style="display: table-row;">';
                                        echo '<td class="nolink footable-first-column">';
                                        echo '<span class="footable-toggle"></span>';
                                        echo '<input type="checkbox" class="mbox_msg_select" name="msg_sel"></td>';
                                        echo '</span></td>';
                                        echo '<td>' . $name . '</td>';
                                        echo '<td>' . $item->email_subject . '</td>';
                                        $originalDate = $item->time;
                                        $newDate = date("Y-M-d H:i:s", strtotime($originalDate));


                                        echo '<td class="footable-last-column">' . $newDate . '</td></tr>';
                                    }
                                    ?>
                                </tbody>
                            </table>

                            <div class="main-detail-con" style="width:700px;">
                            <div style="width:700px; padding-left: 0px;" class="listing-center-numbers">
                              <?php
                            $this->widget('CLinkPager', array(
                                'pages' => $pages,
                                'currentPage' => $pages->getCurrentPage(),
                                'itemCount' => $item_count,
                                'pageSize' => $page_size,
                                'maxButtonCount' => 5,

                            ));
                            ?>
                                </div>
                    </div>     
      <div class="clear"></div>

In Controller,

public function actionEmail() {
    $criteria = new CDbCriteria;
    $criteria->order = 'time DESC';

    $criteria->condition = 'to_userid = :id';
    $criteria->params = array(':id' => Yii::app()->user->id);
    $criteria->addCondition('t.email_status= "2" OR 
                            t.email_status= "1"');

    $item_count = Email::model()->count($criteria);
    $model = Email::model()->findAll($criteria);

    $pages = new CPagination($item_count);
   $pages->setPageSize(Yii::app()->params['listPerPage']);
   $pages->applyLimit($criteria);
    $this->render('inbox', array(
        'model' => $model,
        'item_count' => $item_count,
        'page_size' => Yii::app()->params['listPerPage'],
        'items_count' => $item_count,
        'pages' => $pages,
    ));

}

In main/config.php

'params' => array(
    'listPerPage'=> 2,//default pagination size
),

Output is,

enter image description here

The page size (I am giving 2 for per page) is not working by using this code. Please help me...

1条回答
乱世女痞
2楼-- · 2019-09-09 21:51

First of all define a new params in config/main.php, something like:

'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
        'listPerPage'=> 10,//default pagination size
    ),

Now, modify your controller's action like:

public function actionOutbox() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'from_userid = :id';
        $criteria->order = 'time DESC';
        $criteria->params = array (':id'=>Yii::app()->user->id);

        $item_count = Email::model()->count($criteria);
        $model      = Email::model()->findAll($criteria);

        $pages = new CPagination($item_count);
        $pages->setPageSize(Yii::app()->params['listPerPage']);
        $pages->applyLimit($criteria);

        $this->render('outbox', array(
            'model' => $model,
            'item_count'=>$item_count,
            'page_size'=>Yii::app()->params['listPerPage'],
            'items_count'=>$item_count,
            'pages'=>$pages,
        ));
    }

Then in your view after foreach loop add CLinkPager widget, like:

<?php
$this->widget('CLinkPager', array(
            'currentPage' => $pages->getCurrentPage(),
            'itemCount' => $item_count,
            'pageSize'=> $page_size,
            'maxButtonCount' => 5,
            //'nextPageLabel' =>'My text >',
            'htmlOptions' => array('class'=>'pages'),
        ));
?>

Thats all you need to do, you might have to do some css changes though, hope that helps :)

查看更多
登录 后发表回答