Hiding HTML when data provider returns empty

Hello there

I have been using Yii for a few weeks now and I am loving it!

I have a view file that contains the following:

<?php $deleteSelected= BootHtml::submitButton('Delete selected',array('class'=>'btn primary small'));?>

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




	'ajaxUpdate' =>false,


	'template'=>"{summary}\n{items}\n$deleteSelected\n{pager}", //Used to instert button above pager



                    'class' => 'CCheckBoxColumn',
















Because I want to display my submit button ($deleteSelected) above the pager I am having to output it in the template string. However, when the data provider returns no results the button is still printed.

My question is how do I access a method of the data provider object within the widget construction above.

I only want to output the $deleteSelected var if that data provider returns some data.

Many thanks…

Try this


(not tested)


Thank you, but $model->pagination->itemCount is not a property of the model. But you have put me on the right track. I need to work out how to access the methods and properties of the grid widget and its parents inside the widget construct. I know $this gets me the controller class but this is no good to me either.

I will keep looking…

My mistake. $model->search() will return the CActiveDataProvider instance. You should be able to assign the value to a local variable in the statement used for assigning to the dataprovider property. Since I’m not sure about how param evaluation order is defined in the language, just assign $model->search() to a local variable before the widget creation. Then use the local variable (e.g. $dp) instead of $model->search().

Additional edit:

$dp->itemCount it is. Not $dp->pagination->itemCount


OK. If I access the itemCount property of CAtiveDataProvider (before it is returned to the widget) and make it a property of the model then I can use it in the widget.

My model:

class User extends CActiveRecord


	public $itemCount;


	public function search()


		$criteria=new CDbCriteria;





		$dataProvider= new CActiveDataProvider($this, array(



		//Pass CActiveDataProvider::itemCount to this model



		return $dataProvider;


In my view:

<?php $deleteSelected= BootHtml::submitButton('Delete selected',array('class'=>'btn primary small'));?>


	'template'=>"{summary}\n{items}".($model->itemCount ? $deleteSelected : "") ."\n{pager}", //Use to instert code above pager etc...


Is there a better/cleaner way of doing this?

I posted #5 before seeing your #4 post. I think it is along the same lines.

Many thanks for your time and help.