Cgridview As Report Generation

Good day guys can you please help me I have here 2 cjuidatetimepicker which will determine the range of date to be generate as report and a button that will trigger generate report. But i dont know what to do after creating those datepicker and button. Can someone please give me an idea about this. Thanks in advance.

Hi

What exactly you want to do ?

If I understood well:

1)You have two datepickers

2)when submit the form a cgridview displayed, depended of those dates ?

Please describe your issue with more details

good day sir tnx for the reply problem solved. i created a form using crud and then used the advanced search form as reference and add a datepicker on the form. After that i add a addbetweencondition at the search criteria in the model and thats it.

  1. Did you have rules for those datepickers on scenario in the specific model ?

  2. what are the conditions of CDBcriteria in the search method of the model ?

Please post the code (view form and model rules,search methods)

PS: Thanks for the voting :)

this is the code for the filter in model




public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('duty_id',$this->duty_id);

		$criteria->compare('duty_date',$this->duty_date,true);

		$criteria->compare('duty_time_in',$this->duty_time_in,true);

		$criteria->compare('duty_time_out',$this->duty_time_out,true);

		$criteria->compare('duty_fullname',$this->duty_fullname);

		$criteria->compare('duty_reliever',$this->duty_reliever);

		//$criteria->compare('duty_date',$this->date_first);

		//$criteria->compare('duty_date',$this->date_last);

		if((isset($this->date_first) && trim($this->date_first) != "") && (isset($this->date_last) && trim($this->date_last) != ""))

			$criteria->addBetweenCondition('duty_date', ''.$this->date_first.'', ''.$this->date_last.'');

			//where duty_date between date_first and date_last

		$criteria->order = 'duty_date DESC';


		$criteria->with=array('dutyReliever', 'dutyFullname');

		$criteria->compare('dutyFullname.dr_fullname',$this->fu_search, true);

		$criteria->compare('dutyReliever.dr_fullname',$this->re_search, true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



code for search form




<?php

	Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

?>


<div class="wide form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'action'=>Yii::app()->createUrl($this->route),

	'method'=>'get',

)); ?>




<table border='0' width="500px" align="center">

<tr>

    <td align="center">

	<div class="row">

    	FROM:

		<?php		

        $form->widget('CJuiDateTimePicker',array(

        	'model'=>$model,

            //'name'=>'dufrom',

			'attribute'=>'date_first',

            'id'=>'dufrom',

            'mode'=>'date', //use "time","date" or "datetime" (default)

            'language' => '',

            'options'=>array('showSecond'=>true,'dateFormat'=>'yy-mm-dd','showButtonPanel'=>true,'changeMonth'=>true,

				'changeYear'=>true,'showAnim'=>'fade','showWeek'=>true,

            ),

            'htmlOptions'=>array('readonly'=>'readonly','size'=>'20','style'=>'width:100px;'),

            )

        );

        ?>

        <?php echo $form->error($model,'date_first', array('style'=>"color:red")); ?>

	</div>

	</td>

	<td align="center">

	<div class="row">

		TO:

        <?php

        $form->widget('CJuiDateTimePicker',array(

        	'model'=>$model,

            //'name'=>'duto',

			'attribute'=>'date_last',

            'id'=>'duto',

            'mode'=>'date', //use "time","date" or "datetime" (default)

            'language' => '',

            'options'=>array('showSecond'=>true,'dateFormat'=>'yy-mm-dd','showButtonPanel'=>true,'changeMonth'=>true,'changeYear'=>true,'showAnim'=>'fade','showWeek'=>true,

                            //'autoOpen'=>true,'themeUrl' => Yii::app()->baseUrl.'/css','theme'=>'overcast',        //try 'bee' also to see the changes

                            //'cssFile'=>array('jquery-ui-1.8.23.custom.css' /*,anotherfile.css, etc.css*/),

            ), // jquery plugin options

            'htmlOptions'=>array('readonly'=>'readonly','size'=>'20','style'=>'width:100px;'),

            )

        );

        ?>

        <?php echo $form->error($model,'date_last', array('style'=>"color:red")); ?>

	</div>

    </td>

</tr>

<tr>

	<td align="center">

	<div class="row">

		<?php echo $form->label($model,'duty_fullname'); ?>

		<?php echo $form->dropdownlist($model,'duty_fullname', $model->getDr(), array('prompt'=>'Select Doctor')); ?>

	</div>

	</td>

    <td align="center">

	<div class="row">

		<?php echo $form->label($model,'duty_reliever'); ?>

		<?php echo $form->dropdownlist($model,'duty_reliever', $model->getDr(), array('prompt'=>'Select Doctor')); ?>

	</div>

    </td>

</tr>

<tr>

	<td align="center" colspan="2">

    <div class="row">

		<?php //echo $form->label($model,'duty_date'); ?>

		<?php //echo $form->textField($model,'duty_date'); ?>

	</div>

    </td>

</tr>

<tr>

	<td align="center" colspan="2">

        <div class="row buttons">

            <?php echo CHtml::submitButton('Generate Report',array('id'=>'button','class'=>'btn btn-success ui-button ui-widget ui-state-default ui-corner-all')); ?>

        </div>

    </td>

</tr>

</table>


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


</div><!-- search-form -->



code for display




<script>

	$(document).ready(function(){

		$('#report').hide();

		$('#button').click(function(){

			if($('#dufrom').val() != "" && $('#duto').val() != "")

			{

				$('#report').show();

			}

			else

			{

				alert("Date from/to must not empty");

			}

		});

		$('#dufrom').change(function(){

			$('#date_first').val($('#dufrom').val());

		});

		$('#duto').change(function(){

			$('#date_last').val($('#duto').val());

		});

	});

</script>


<?php

Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');


$this->breadcrumbs=array(

	'Shis Duties'=>array('index'),

	'Manage',

);


$this->menu=array(

	array('label'=>'List ShisDuty', 'url'=>array('index')),

	array('label'=>'Create ShisDuty', 'url'=>array('create')),

);


Yii::app()->clientScript->registerScript('search', "

	$('.search-form form').submit(function(){		

		$('#shis-duty-grid').yiiGridView('update', {

			data: $(this).serialize()

		});		

		return false;

	});

");

?>


<center>

<div style="width:800px;">

<?php

	$this->beginWidget('zii.widgets.CPortlet', array(

			'title'=>"<center><h3>Report</h3></center>",

		));

?>

<div class="search-form">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div><!-- search-form -->


<div id="report">

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

			'id'=>'shis-duty-grid',

			'dataProvider'=>$model->search(),

			'columns'=>array(

				array(

					'name'=>'duty_date',

				),		

				array(

					'header'=>'Time-in',

					'name'=>'duty_time_in',

					'value'=>function($data){

						if($data->duty_time_in > "12:00")

						{

							$timeIn = date('h:i', strtotime('-12 hours', strtotime($data->duty_time_in)))." pm";

						}

						else

						{

							$timeIn = date('h:i a', strtotime($data->duty_time_in));

						}

						

						return $timeIn;

					},

					'filter'=>CHtml::activeTextField($model,'duty_time_in'),

				),		

				array(

					'header'=>'Time-out',

					'name'=>'duty_time_out',

					'value'=>function($data){

						if($data->duty_time_out > "12:00")

						{

							$timeOut = date('h:i', strtotime('-12 hours', strtotime($data->duty_time_out)))." pm";

						}

						else

						{

							$timeOut = date('h:i a', strtotime($data->duty_time_out));

						}

						

						return $timeOut;

					},

				),	

				array(

					'header'=>'Dr. Fullname',

					'name'=>'dutyFullname.dr_fullname',

				),

				array(

					'header'=>'Reliever',

					'name'=>'dutyReliever.dr_fullname',

					'value'=>'($data->dutyReliever !== null) ? $data->dutyReliever->dr_fullname : "N/A"',

				),

			),

		)); ?>

       	<?php echo CHtml::link('Export to Excel',array('dutyreport/export')); ?>

        <script>			

        	function catchData(){

			var fdate = $('#dufrom').val();

			var ldate = $('#duto').val();

				$.ajax({

				type: 'POST',

				data: {fdate : fdate, ldate : ldate},

				url: '<?php echo CController::createUrl('export'); ?>',		

				})

				

				

			}

        </script>

</div>             


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

</div>

</center>



Hi,

Are you sure you send by the POST the corrected vairables ?

For example fdate (in your ajax form) seems to send by the POST but in your model the same variable name seems does not exists! Also check to safe this variable name on scenario ‘search’

im sorry i forgot to post the controller

this is my controller that will catch the ajax post




public function actionExport()

	{

		$fdate = $_POST['fdate'];

		$ldate = $_POST['ldate'];

		//echo $fdate,$ldate;

		// Load data (scoped)

		$criteria = new CDbCriteria;

		$criteria->condition = 'duty_date BETWEEN '.$fdate.' AND '.$ldate;

		$model=ShisDuty::model()->findAll($criteria); 

	 

		// Export it

		$this->toExcel($model,

		array(

			'duty_id::#',

			'duty_date::Date',

			'duty_time_in::Time-in',

			'duty_time_out::Time-out',

			'dutyFullname.dr_fullname::Doctor',

			'dutyReliever.dr_fullname::Reliever',

		),

		'Duty Report',

		array(

			'creator' => 'Jimuel',

		),

		'Excel2007'

		);

	}