Tuesday, September 3, 2013

CPagination หรือคำสั่งในการแบ่งหน้า

การแบ่งหน้าเช่นมี 30 รายการ แต่อยากให้แสดงทีละ 10 รายการต่อ 1 หน้า

ทาง yii สามารถทำโดยการใช้ datagridview ก็ได้ มันจะแบ่งหน้าเองอัตโนมัติ หรือ ใช้ CPagination พ่วงกับการแสดงข้อมูลแบบปกติได้เช่นเดียวกัน


models/post.php
<?php

/**
 * This is the model class for table "post".
 *
 * The followings are the available columns in table 'post':
 * @property integer $id
 * @property string $title
 */
class Post extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'post';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title', 'length', 'max'=>255),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, title', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'title' => 'Title',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('title',$this->title,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Post the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}


views/post/index.php
 <p><?php echo $sort->link('id')?></p>
<p><?php echo $sort->link('title')?></p>

<ol>
<?php foreach($models as $model):?>
<li>
    <h2><?php echo $model->id?> - <?php echo $model->title?></h2>
</li>
<?php endforeach?>
</ol>

<?php $this->widget('CLinkPager', array(
    'pages' => $pages,
))?>

controller/PostController.php
<?php

class PostController extends Controller
{
    public function actionIndex(){
        $criteria = new CDbCriteria();
        $count = Post::model()->count($criteria);
        $pages=new CPagination($count);
       
        $pages->pageSize=5;
        $pages->applyLimit($criteria);
       
        $sort = new CSort('Post');
        $sort->attributes = array(
            'id',
            'title'
        );
        $sort->applyOrder($criteria);
       
        $models = Post::model()->findAll($criteria);
       
        $this->render('index', array(
        'models' => $models,
        'pages' =>    $pages,
        'sort' => $sort,
        ));
   

    }
}


ผลลัพธ์ที่ได้ออกมาจะเป็นแบบนี้

No comments:

Post a Comment

Blog Archive

http://cksc.co.th/site/

Total Pageviews

Popular Posts