Yii Framework Forum: How Can I Get Total Number Of Records In Cgridview? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How Can I Get Total Number Of Records In Cgridview? How can i get total number of records in cgridview? Rate Topic: ****- 2 Votes

#1 User is offline   prashant.tyagi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 14-September 12

Posted 26 November 2012 - 07:35 AM

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
0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 26 November 2012 - 08:29 AM

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())
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   prashant.tyagi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 14-September 12

Posted 20 December 2012 - 01:35 AM

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
0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,317
  • Joined: 21-February 11
  • Location:Greece

Posted 22 December 2012 - 04:00 PM

View Postprashant.tyagi, on 20 December 2012 - 01:35 AM, said:

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,

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   prashant.tyagi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 14-September 12

Posted 30 January 2013 - 11:20 PM

thanks,but my dataprovider is a function like this

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'wishlist-grid',
'dataProvider'=>$model->xxxxxxxxx($xxxx,$yyyy),
'ajaxUpdate'=>false,
'columns'=>$attributes,
'filter'=>$model,
));
?>
0

#6 User is offline   prashant.tyagi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 14-September 12

Posted 31 January 2013 - 10:52 PM

anybody?
0

#7 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 01 February 2013 - 03:01 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users