I have a form in my view which selects columns from a dropdown list. The SqlDataProvider is used to get the data from the generated Query
I have been trying to use CGridView with SQLDataProvider, its working a bit fine, but still having some issues with pagination. I don't have any keyField so I give it my first column, and without keyField it doesn't work.
Here is my action:
public function actionIndex()
{
$tables = Yii::app()->db->schema->getTables();
if(isset($_POST['yt0'])){ //if from submitted
$query = $this->generateQuery();
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM ( ' . $query . ' ) as count')->queryScalar();
$dataProvider = new CSqlDataProvider($query, array(
'keyField' => $firstColumn,
'totalItemCount'=> $count,
'pagination'=> array(
'pageSize'=>20,
),
));
$columns = $this->getColumnNamesWithoutTable();
$this->render('index',
array( 'tables' => $tables,
'query' => $query,
'dataProvider' => $dataProvider,
'columns' => $columns
)
);
}else{
$this->render('index', array('tables' => $tables));
}
}
Here is widget code in the view, according to what I have read, pagination should work automatically, is it because I am using a custom query rather than Yii Models ?
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enablePagination'=> true,
));
It shows the data first time, but next button doesn't work.
'keyField' => $firstColumn
What is the $firstColumn value.
Keyfield must be index of array retiring by the CSqlDataProvider
I just added totalitemcount in the sqldataprovider and it worked for me, if anyone is still wondering,
$dataProvider=new CSqlDataProvider($sql, array(
'pagination'=>array(
'pageSize'=>10,
),
'totalItemCount'=>'100000'
));