Different View For 1St Result In Clistview

I would like to use a different _view file for the first result provided by the dataprovider. And another _view2 file for all the rest


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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>

You can create your own widget that extends the zii.widgets.CListView and change the function renderItems.

You can also add the second template on the widget config


<?php $this->widget('MyCustomCListView', array(

	'dataProvider'=>$dataProvider,


	'firstItemView'=>'_view_first_item',


	'itemView'=>'_view',


)); ?>

Second Option:-

MAke two different dataprovider one with limit other is as it is which you have

in first ypu can write like this in model newsearch function for first record,




in model,


$criteria=new CDbCriteria;

$criteria->limit=1;

$academic_term_data = new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));



Dear Friend

If you want to change the view file for very first item, you can alter the _view file in the following way.

_view.php




<?php

if($data->id==1)

	$this->renderPartial('_view2',array('data'=>$data)); //renders the second view for the first item by choosing correct id.

else {

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('age')); ?>:</b>

	<?php echo CHtml::encode($data->age); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('sex')); ?>:</b>

	<?php echo CHtml::encode($data->sex); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('place')); ?>:</b>

	<?php echo CHtml::encode($data->place); ?>

	<br />


</div>

<?php

} ?>



If you want view file for first item in every page, you can do the following.

index.php




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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'viewData'=>array('dataProvider'=>$dataProvider)

)); ?>



_view.php




<?php

if(array_search($data,$dataProvider->data)==0)

	$this->renderPartial('_view2',array('data'=>$data));

else {

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('age')); ?>:</b>

	<?php echo CHtml::encode($data->age); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('sex')); ?>:</b>

	<?php echo CHtml::encode($data->sex); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('place')); ?>:</b>

	<?php echo CHtml::encode($data->place); ?>

	<br />




</div>

<?php

} ?>




Regards.

You don’t need to pass parameter to view partial. You may use $index inside _view.php (http://www.yiiframework.com/doc/api/1.1/CListView#itemView-detail):

index.php




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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view'

)); ?>



_view.php




<?php

if ($index==0)

	$this->renderPartial('_view2',array('data'=>$data));

else {

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('age')); ?>:</b>

	<?php echo CHtml::encode($data->age); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('sex')); ?>:</b>

	<?php echo CHtml::encode($data->sex); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('place')); ?>:</b>

	<?php echo CHtml::encode($data->place); ?>

	<br />




</div>

<?php

} ?>




Dear Luc

I express my sincere thanks for spotting that out.

Apart from the data, $index and $widget(instance of widget itself) also available at view file.

Thanks again.