I have some duplicated code below, the “$dataProvider=new CActiveDataProvider(‘LocationsImport’” etc. which runs on the index method and is rendered by the index view & on the go method which is executed via ajax.
I want to refactor this so that the code is only in one place.
Create a method in the controller or in the LocationsImport model. It will return the data provider.
public function actionIndex()
{
$this->render(
'index',
array('dataProvider'=>$this->getMyDataProvider()
));
}
or
public function actionIndex()
{
$this->render(
'index',
array('dataProvider'=>LocationsImport::model()->getMyDataProvider()
));
}
In the actionGo you can call the method on the existing $model.
You can try to put the CGridView in a partial view and call it from actionGo and from the ‘index’ view. I can’t tell for sure if it will work flawlessly (have to try the “load grid view via ajax” part myself, some time).
$this->renderPartial('_grid', array('dataprovider'=>$dataprovider));
or perhaps
echo $this->renderPartial('_grid', array('dataprovider'=>$dataprovider), true);
If the grid doesn’t work after being reloaded, search the forum for ‘ajax’, ‘renderPartial’, ‘processOutput’.
Edit:
If the purpose of actionGo is to just update the grid there’s javascipt support for this loaded with CGridView (IIRC fn.yiiGridView.update). Again, search the forum and/or consult the API reference.