在yii中表格内容的分页 [英] Pagination for a table contents in yii

查看:242
本文介绍了在yii中表格内容的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Yii application. In that how to give pagination to a table contents (not using 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中

In main/config.php

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

输出是,

页面大小(我每页给2个)使用此代码无法正常工作。请帮助我...

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

推荐答案

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

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

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

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

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

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

这篇关于在yii中表格内容的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆