Yii Framework Forum: Different View For 1St Result In Clistview - Yii Framework Forum

Jump to content

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

Different View For 1St Result In Clistview Rate Topic: -----

#1 User is offline   swampone 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 28-March 11

Posted 07 April 2013 - 05:53 PM

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',
)); ?>

0

#2 User is offline   jneto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-November 12
  • Location:Portugal

Posted 07 April 2013 - 08:20 PM

View Postswampone, on 07 April 2013 - 05:53 PM, said:

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',

)); ?>

0

#3 User is offline   hiral darji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 31-May 12
  • Location:Ahmedabad

Posted 08 April 2013 - 12:02 AM

View Postswampone, on 07 April 2013 - 05:53 PM, said:

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',
)); ?>



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,
		));

0

#4 User is offline   seenivasan 

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

Posted 08 April 2013 - 04:35 AM

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

#5 User is offline   luk1999 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 20-April 11
  • Location:Poland

Posted 08 April 2013 - 07:32 AM

View Postseenivasan, on 08 April 2013 - 04:35 AM, said:

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)
)); ?>

(...)

You don't need to pass parameter to view partial. You may use $index inside _view.php (http://www.yiiframew...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
} ?>


1

#6 User is offline   seenivasan 

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

Posted 08 April 2013 - 08:51 AM

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