(oleh : Badiyanto)
Jika Anda akan membuat laporan yang menbutuhkan total pada masing masing kolom, Anda harus membuat fungsi sendiri, karna keterbatasan di GridView. Rancangan dan tabel Anda menggunakan tabel gaji di sini.
1. Pada Bagian Model Gaji.php
public function searchTotalgaji()
{
$sql="SELECT
nip
, nama
, gaji_pokok
, tunjangan
, gaji_pokok+tunjangan as total_gaji
, (gaji_pokok+tunjangan) * 5/100 as pajak
, gaji_pokok+tunjangan-(gaji_pokok+tunjangan) * 5/100 gaji_bersih
FROM
kuliah.gaji;";
$dataGaji=Yii::app()->db->createCommand($sql)->queryAll();
$larikGaji=array();
$totalGapok=0;
$totalTunjangan=0;
$totalPajak=0;
$totalGaji=0;
$totalGaber=0;
$i=0;
foreach ($dataGaji as $dataGaji1){
$i++;
$totalGapok = $totalGaji + $dataGaji1["gaji_pokok"];
$totalTunjangan = $totalTunjangan + $dataGaji1["tunjangan"];
$totalGaji = $totalGaji + $dataGaji1["total_gaji"];
$totalGaber = $totalGaber + $dataGaji1["gaji_bersih"];
$totalPajak = $totalPajak + $dataGaji1["pajak"];
$larikGaji[] = array(
'no'=>$i,
'nip'=>$dataGaji1["nip"],
'nama'=>$dataGaji1["nama"],
'gaji_pokok'=>$dataGaji1["gaji_pokok"],
'tunjangan'=>$dataGaji1["tunjangan"],
'total_gaji'=>$dataGaji1["total_gaji"],
'pajak'=>$dataGaji1["pajak"],
'gaji_bersih'=>$dataGaji1["gaji_bersih"]
);
}
$larikGaji[] = array(
'no'=>"",
'nip'=>"",
'nama'=>"JUMLAH",
'gaji_pokok'=>$totalGapok,
'tunjangan'=>$totalTunjangan,
'total_gaji'=>$totalGaji,
'pajak'=>$totalPajak,
'gaji_bersih'=>$totalGaber
);
$dataProvider=new CArrayDataProvider(
$larikGaji,array(
'keyField'=>false,
'pagination'=>array('pageSize'=>100,),
));
return $dataProvider;
}
2. Bagian Controller GajiController.php
public function actionTotalGaji()
{
$model=new Gaji('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Gaji']))
$model->attributes=$_GET['Gaji'];
$this->render('totalGaji',array(
'model'=>$model,
));
}
3. Buatlah file totalGaji.php di bagian views sbb:
<h1>Tabel Gaji</h1>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'gaji-grid',
'dataProvider'=>$model->searchTotalgaji(),
//'filter'=>$model,
'columns'=>array(
array(
'name'=>'no',
'type'=>'raw',
'header'=>'No',
'value'=>'CHtml::encode($data["no"])',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
array(
'name'=>'nip',
'type'=>'raw',
'header'=>'NIP',
'value'=>'CHtml::encode($data["nip"])',
'htmlOptions'=>array('width'=>''),
),
array(
'name'=>'nama',
'type'=>'raw',
'header'=>'Nama',
'value'=>'CHtml::encode($data["nama"])',
'htmlOptions'=>array('width'=>''),
),
array(
'name'=>'gaji_pokok',
'type'=>'raw',
'header'=>'Gaji Pokok',
'value'=>'CHtml::encode(number_format($data["gaji_pokok"],2))',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
array(
'name'=>'tunjangan',
'type'=>'raw',
'header'=>'Tunjangan',
'value'=>'CHtml::encode(number_format($data["tunjangan"],2))',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
array(
'name'=>'nip',
'type'=>'raw',
'header'=>'Jumlah Gaji',
'value'=>'CHtml::encode(number_format($data["total_gaji"],2))',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
array(
'name'=>'nip',
'type'=>'raw',
'header'=>'Pajak',
'value'=>'CHtml::encode(number_format($data["pajak"],2))',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
array(
'name'=>'nip',
'type'=>'raw',
'header'=>'Gaji Bersih',
'value'=>'CHtml::encode(number_format($data["gaji_bersih"],2))',
'htmlOptions'=>array('width'=>'','style'=>'text-align:right'),
),
),
)); ?>
Lihat Hasilnya :