How Can I Get Total Number Of Records In Cgridview?

Hi

How can i get total number of records in cgridview? i have to show total number of records on the top of list.so please tell me how can i get it in view file.thanks

Hi

you can get the count of records by the related CDataProvider of CGridview

$yourprovider->getItemCount()

$yourprovider->getTotalItemCount(); //(idepended of pagination)

or you can get it by count($yourprovider->->getData())

Hi KonApaz

thanks for your reply. as i am a new to yii can you explain a bit more as where to write these functions call, in model or controller? and how can i access these values in view.thanks

the think you have to do for any part of code (in most cases) :

Is it about display ?

Is it about logic (checking, decision and control) ?

Is it about fetching data ?

All the above things called MVC (Model viewer, controller)

So create the dataprovider in Controller with all its parameters

(in order the CActiveDataProvider fetch the data from model) and pass it in the viewer

example:


in Controller


 public function actionIndex() {

   $dataProvider = new CActiveDataProvider('YourModel') 

   $this->render('index', array('dataProvider' => $dataProvider));

 }




in viewer (index)


        $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$dataProvider,

thanks,but my dataprovider is a function like this

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

                    'id'=&gt;'wishlist-grid',


                    'dataProvider'=&gt;&#036;model-&gt;xxxxxxxxx(&#036;xxxx,&#036;yyyy),


                    'ajaxUpdate'=&gt;false,


                    'columns'=&gt;&#036;attributes,


                    'filter'=&gt;&#036;model,


            ));


            ?&gt;

anybody?

Dear Prasanth

I am just confused. CgridView already displays dynamically before filtering and also after filtering as summary. at the top right.

OK.Anyway things are easy because you have disabled ajaxUpdate.

Just insert the first two lines of the following code above the widget code.




<?php

$total=$model->search()->getTotalItemCount();

echo "<h6>Total number of Items: ".$total."</h6>";

?>

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'worker-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'ajaxUpdate'=>false,

	'columns'=>array(

		'id',

		'name',

		'assigned',

		'completed',

		'g_id',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); 


?>



If you want the same functionality with ajax enabled, we have to overide the CGridView.

components/GridView.php




<?php

Yii::import('zii.widgets.grid.CGridView');

class GridView extends CGridView

{

    public function run()	

    {

	$this->registerClientScript();	

	echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";


        $total=$this->dataProvider->getTotalItemCount();//added line

        echo "<h6>Total number of Items: ".$total."</h6>";//added line


	$this->renderContent();

	$this->renderKeys();


	echo CHtml::closeTag($this->tagName);

    }

	

}

?>



Now admin.php uses this derived class with ajax enabled.




<?php $this->widget('GridView', array(  //instance of GridView

	'id'=>'worker-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'ajaxUpdate'=>true, //AJAX enabled

	'columns'=>array(

		'id',

		'name',

		'assigned',

		'completed',

		'g_id',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); 



I hope I helped a bit.

Regards.