How can i get first and last record id in yii CGridview?

532 Views Asked by At

I want first and last record id form $dataprovider which is passed to gridview which i need to pass to this link.

array(
         'name' => 'msg',
         'value' => 'CHtml::link(substr(strip_tags($data->msg),0,30)." .....",Yii::app()->createUrl("Mail/view",array("id"=>$data->primaryKey,"flag"=>"inbox","tab"=>'.$tab.',"category"=>'.$category.')))',
         'type' => 'raw',
         'header' => 'Message',
     ), 
1

There are 1 best solutions below

0
On
  1. Declare two attributes in your controller.
public $first=null;
public $last=null;
  1. Define a function in controller to render your link
public function renderMailViewLink($data,$row){
               // you can return anything you want here. whether a link or whatever
               // access $this->first->id , $this->last->id

   return CHtml::link($data->anyAttribute,array('someRoute','id'=>$data->id,'first'=>$this->first->id))
}

CActiveDataProvider has a method getData(), which return array of all active records.

  1. in actionIndex
$dataProvider = $model->search()
$data = $dataProvider->getData();
$this->first = reset($data );
$this->last = end($data);
  1. And finally in your view
array(
         'name' => 'msg',
         'value' => array($this,'renderMailViewLink'),
         'type' => 'raw',
         'header' => 'Message',
     ), 

Please note this code is not tested. But thats how it can be achieved