簡體   English   中英

如何在yii CGridview中獲取第一個和最后一個記錄ID?

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

我想要第一個和最后一個記錄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. 在您的控制器中聲明兩個屬性。
public $first=null;
public $last=null;
  1. 在控制器中定義一個函數以呈現您的鏈接
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. 在actionIndex
$dataProvider = $model->search()
$data = $dataProvider->getData();
$this->first = reset($data );
$this->last = end($data);
  1. 最后在您看來
array(
         'name' => 'msg',
         'value' => array($this,'renderMailViewLink'),
         'type' => 'raw',
         'header' => 'Message',
     ), 

請注意,此代碼未經測試。 但這就是如何實現的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM