如何才能在yii CGridview中获得第一个和最后一个记录ID?

时间:2015-03-27 04:55:06

标签: yii cgridview yii-url-manager yii-cactiverecord

我想要第一个和最后一个记录ID表单$dataprovider,它传递给gridview,我需要传递给此链接。

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 个答案:

答案 0 :(得分:0)

  1. 在控制器中声明两个属性。
  2. public $first=null;
    public $last=null;
    
    1. 在控制器中定义一个功能以呈现链接
    2. 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有一个方法getData(),它返回所有活动记录的数组。

      1. in actionIndex
      2. $dataProvider = $model->search()
        $data = $dataProvider->getData();
        $this->first = reset($data );
        $this->last = end($data);
        
        1. 最后在你看来
        2. array(
                   'name' => 'msg',
                   'value' => array($this,'renderMailViewLink'),
                   'type' => 'raw',
                   'header' => 'Message',
               ), 
          

          请注意,此代码未经过测试。但那就是如何实现

相关问题