Yii Framework Forum: Menampilkan CDetailView dan Daftar Rinciannya - Yii Framework Forum

Jump to content

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

Menampilkan CDetailView dan Daftar Rinciannya Rate Topic: -----

#1 User is offline   M Hermansyah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 25-August 13

Posted 06 April 2017 - 11:20 PM

Kadang kita ingin menampilkan data detail CDetailView dengan daftar rinciannya dalam satu view. Misalnya data Kelas dan daftar peserta yang ikut kelas tersebut.

Saya sudah membuat katalog tebing Indonesia utk kegiatan panjat tebing. Ada kawasan, dalam kawasan bisa terdapat lebih dari satu tebing, dan di suatu tebing bisa terdapat beberapa jalur pemanjatan.

Model yg terlibat adalah Kawasan.php, Tebing.php dan Jalur.php semua ada di protected/models hasil dari Gii.


Buat relasi di model Kawasan.php
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                  'tebing'=>array(self::HAS_MANY, 'Tebing', 'idkawasan'), //nama relasi=tebing
                  'jalur'=>array(self::HAS_MANY, 'Jalur', 'idkawasan'), //nama relasi=jalur
		);
	}

Buat relasi di model Tebing.php
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                 'kawasan' => array(self::BELONGS_TO, 'Kawasan', 'idkawasan'),
                 'jalur'=>array(self::HAS_MANY, 'Jalur', 'idtebing'),
		);
	}

Buat relasi di model Tebing.php
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                  'kawasan' => array(self::BELONGS_TO, 'Kawasan', 'idkawasan'),
                  'tebing' => array(self::BELONGS_TO, 'Tebing', 'idtebing'),
		);
	}


Perhatikan model Kawasan.php, dan nama relasi di model ini. Iya, nama relasi ini akan dipanggil di protected/views/kawasan/view.php

<div class="picture">
<?php
if(!empty($model->foto))
{
 echo CHtml::image(Yii::app()->request->baseUrl.'/../images/data/'.$model->foto,"foto",array("width"=>300, "align"=>left));  
}
else
{
 echo CHtml::image(Yii::app()->request->baseUrl.'/../images/rock1.jpg',"foto manjat",array("width"=>300, "align"=>left));  
}
?>
</div>


<div class="view">
<?php

   $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		'idkawasan',
		'nama',
		'kecamatan',
                 array(
                    'name'=>'city',
                    'value'=>$model->city->cityname,
                       ),
                 array(
                    'name'=>'state',
                    'value'=>$model->state->statename,
                       ),
                'info:html',
		'other',
		//'user',
		//'timestamp',
	),
)); ?>

<br />
<h2>Daftar Tebing </h2>
<ol>
<?php $items = $model->tebing; //memanggil nama relasi tebing dari model Kawasan
foreach ($items as $item) {
	//echo '<li>' . $item->namatradisional .' ('.$item->namapopuler. ')</li>';
	echo '<li>' . CHtml::link($item->concatened, array ('/tebing/view', 'id' => $item->idtebing)) . '</li>';
}
?>
</ol>
    
<br />

<h2>Daftar Jalur Pemanjatan </h2>
<ol>
<?php $items = $model->jalur; //memanggil nama relasi jalur di model Kawasan
foreach ($items as $item) {
	//echo '<li>' . $item->nama .' ('.$item->panjang.' m'. ')</li>';
	echo '<li>' . CHtml::link($item->nama, array ('/jalur/view', 'id' => $item->id)) . '</li>';
}
?>
</ol>


Contoh screenshoot tampilannya terlampir. Kalau mau lihat live Contoh. Mhn maaf, tampilan map pada contoh live tidak dibahas di sini.
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