Yii Framework Forum: Menampilkan Data Berdasarkan Rentang Tanggal Yii 1.1 - Yii Framework Forum

Jump to content

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

Menampilkan Data Berdasarkan Rentang Tanggal Yii 1.1 Rate Topic: -----

#1 User is offline   M Hermansyah 

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

Posted 15 February 2018 - 04:40 PM

Misalkan kita punya tabel data yang mempunyai kolom tanggal dengan nama date dg format 1970-01-01 (PHP/MySQL bentuknya Y-m-d), nah kita ingin menampilkan data dengan rentang dari tanggal tertentu hingga tanggal tertentu, misalnya dari tanggal 2017-01-01 hingga 2017-12-31.

Berikut saya share tips step-by-step berdasarkan postingan CrazyCat yang saya modifikasi agar sesuai dg situasi yg saya hadapi.

Tapi tips Ini hanya untuk Yii.1.1. Hasil pencarian sekitar 2 minggu yang cukup membuat pening (buat saya).

1. Buat file .php dengan nama EDateRangeSearchBehavior.php letakkan di direktory protected/components/behaviors.

2.a. Copy kode berikut:
<?php
/**
 * This model behavior builds a date range search condition.
 */
class EDateRangeSearchBehavior extends CActiveRecordBehavior
{
 
    /**
     * @param the default 'from' date when nothing is entered.
     */
    public $dateFromDefault = '1900-01-01';
 
    /**
     * @param the default 'to' date when nothing is entered.
     */
    public $dateToDefault = '2099-12-31';
 
    /*
     * Date range search criteria
     * public $attribute name of the date attribute
     * public $value value of the date attribute
     * @return instance of CDbCriteria for the model's search() method
     */
    public function dateRangeSearchCriteria($attribute, $value)
    {
        // Create a new db criteria instance
        $criteria = new CDbCriteria;
 
        // Check if attribute value is an array
        if (is_array($value))
        {
            // Check if either key in the array has a value
            if (!empty($value[0]) || !empty($value[1]))
            {
                // Set the date 'from' variable to the first value in the array
                $dateFrom = $value[0];
                if (empty($dateFrom))
                {
                    // Set the 'from' date to the default
                    $dateFrom = $this->dateFromDefault;
                }
 
                // Set the date 'to' variable to the second value in the array
                $dateTo = $value[1];
                if (empty($dateTo))
                {
                    // Set the 'to' date to the default
                    $dateTo = $this->dateToDefault;
                }
 
                // Check if the 'from' date is greater than the 'to' date
                if ($dateFrom > $dateTo)
                {
                    // Swap the dates around
                    list($dateFrom, $dateTo) = array($dateTo, $dateFrom);
                }
 
                // Add a BETWEEN condition to the search criteria
                $criteria->addBetweenCondition($attribute, $dateFrom, $dateTo);
            }
            else
            {
                // The value array is empty so set it to an empty string
                $value = '';
 
                // Add a compare condition to the search criteria
                $criteria->compare($attribute, $value, true);
            }
        }
        else
        {
            // Add a standard compare condition to the search criteria
            $criteria->compare($attribute, $value, true);
        }
 
        // Return the search criteria to merge with the model's search() method
        return $criteria;
    }
 
}


2.b. Paste ke file EDateRangeSearchBehavior.php yang sudah dibuat.

3.a. Modif file model nya (misalnya Journal.php), tambahkan kode berikut (klo saya sih taruh di bagian akhir mode):
        public function behaviors()
        {
            return array(
                'dateRangeSearch'=>array(
                    'class'=>'application.components.behaviors.EDateRangeSearchBehavior',
                    ),
            );
        }   


3.b. Modif lagi file model Journal.php yang sama, pada
public function search()
yang digunakan pada search di view nya seperti ini:
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		//$criteria->compare('date',$this->date,true); //ini baris originalnya
                $criteria->mergeWith($this->dateRangeSearchCriteria('date', $this->date)); //ini baris gantinya utk filter date range
                $criteria->compare('description',$this->description,true);
		$criteria->compare('kurs',$this->kurs,true);


4. Sekarang ubah _search.php di direktori views (misalnya views/journal) sbg berikut:

<div class="wide form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
)); ?>

	<div class="row">
                <?php
                    // Date range search inputs
                    $attribute = 'date';
                    for ($i = 0; $i <= 1; $i++)
                    {
                    $year1='2016'; //bisa disesuaikan dg kebutuhan, bisa juga tiga tahun yang lalu date('Y')-3
                    $year3=date('Y'); 

                        echo ($i == 0 ? Yii::t('main', '<b>From</b>') : Yii::t('main', '<b>To</b>'));
                        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                            'id'=>CHtml::activeId($model, $attribute.'_'.$i),
                            'model'=>$model,
                            'attribute'=>$attribute."[$i]",
                            'options' => array(
                            'dateFormat' => 'yy-mm-dd',
                            'changeMonth'=>true,
                            'changeYear'=>true,
                            'yearRange'=>$year1.':'.$year3,
                    ),
                        )); 
                    }
                ?>
	</div>
    
        <div class="row">
		<?php echo $form->label($model,'id'); ?>
		<?php echo $form->textField($model,'id'); ?>
	</div>
    
	<div class="row">
		<?php echo $form->label($model,'description'); ?>
		<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>250)); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Search'); ?>
	</div>

<?php $this->endWidget(); ?>

</div>


5. Di file view yg ada pencarian menggunakan _search() misalnya admin.php, gak ada yg perlu diubah:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'journal-grid',
	'dataProvider'=>$model->search(), //ini ambil dari model yg sudah kita modif di atas.
	'filter'=>$model,
	'columns'=>array(
                array(
                'header'=>'No.',
                'value'=>'$this->grid->dataProvider->pagination->offset + $row+1',      //  row is zero based
                'htmlOptions'=>array('width'=>'5%'),
                ), 
		array(
                    'header'=>'ID',
                    'name'=>'id',
                    'value'=>'$data->id',
                    'htmlOptions'=>array('width'=>'5%','style' => 'text-align: left;'),
                    ),
		array(
                    'header'=>'Date',
                    'value'=>'$data->date',
                    'htmlOptions'=>array('width'=>'10%','style' => 'text-align: left;'),
                    ),
		array(
                    'header'=>'Description',
                    'value'=>'$data->description',
                    'htmlOptions'=>array('width'=>'30%','style' => 'text-align: left;'),
                    ),
               .....


6. That's it..selamat mencoba. Di saya sudah dicoba dan berhasil.
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