I work with yii1
.
I have a multi-page list of records, which is displayed using CGridView
, sorted in a certain way.
I have id
of the record. And I want to open the page on which this record with this exact id is displayed.
Roughly speaking, link /list?too=123
opens the 12th page of the grid, and among all records on this page there is an entry with id
123.
What's the easiest way to do this?
My way of doing it-
$model->search()
to the CGridView
.Hope it helps.
You can extend CGridView and write your own method to search page.
MyGridView.php
<?php
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView {
public function init()
{
if (!isset($_GET[$this->dataProvider->getPagination()->pageVar]) && isset($_GET['too']))
$this->searchPage();
parent::init();
}
public function searchPage(){
$baseCriteria=$this->dataProvider->getCriteria();
$criteria=clone $this->dataProvider->getCriteria();
if(($sort=$this->dataProvider->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->dataProvider->model->setDbCriteria($c);
}
else
$this->dataProvider->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->dataProvider->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->dataProvider->model->findAll($criteria);
$position = 0;
foreach($data as $model) {
$position++;
if ($model->uid == $_GET['too']) {
$curPage = ceil($position / $this->dataProvider->getPagination()->pageSize);
$_GET[$this->dataProvider->getPagination()->pageVar] = $curPage;
}
}
}}