在yii中表格内容的分页

时间:2015-09-11 12:14:12

标签: php yii pagination html-table

我正在使用Yii应用程序。在那里如何给表内容分页(不使用cgridview)。

在视图中,

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

在控制器中,

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,
    ));

}

在main / config.php中

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

输出是,

enter image description here

使用此代码,页面大小(我每页2分)无效。请帮帮我......

1 个答案:

答案 0 :(得分:0)

首先在config/main.php中定义一个新的参数,类似于:

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

现在,修改控制器的操作,如:

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

然后在foreach循环添加CLinkPager窗口小部件后的视图中,如:

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

这就是你需要做的一切,你可能不得不做一些css改变,希望有所帮助:)