Question

I have setup CGridView with CArrayDataProvider

Controller

//Get data for form dropdown:

        $criteria = new CDbCriteria;
        $criteria->select = 'status';
        $criteria->group = 'status';
        $status_get = Model::model()->findAll($criteria);


            foreach ($status_get as $s) {
                $status[$s->status] = $s->status;
            }

            if(!isset($_GET['ajax'])){
               //Display empty array - show no data before user input
               $rawData=array();

            } else {
                // Here I want to be able to create custom array based on user input (from SQL query)
                $rawData=array();
            }

            $arrayDataProvider=new CArrayDataProvider($rawData, array(
                'id'=>'id',
                /* 'sort'=>array(
                    'attributes'=>array(
                        'id', 'status',
                    ),
                ), */
                'pagination'=>array(
                    'pageSize'=>50,
                ),
            ));

            $params =array(
                'arrayDataProvider'=>$arrayDataProvider,
                'status'=>$status,
            );

            if(!isset($_GET['ajax'])) $this->render('byDay', $params);
            else  $this->renderPartial('byDay', $params);

And View:

<?php
/* @var $this RaportController */

$this->breadcrumbs = array(
    'Raport' => array('/raport'),
    'ByDay',
);
?>
<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>

<div class="form">

    <?php $form = $this->beginWidget('CActiveForm', array(
        'id' => 'person-form-edit_person-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array(
            'onsubmit' => "return false;", /* Disable normal form submit */
            'onkeypress' => " if(event.keyCode == 13){ send(); } " /* Do ajax call when user presses enter key */
        ),
    )); ?>



    <?php

    if (isset($status)) {
        echo CHtml::DropDownList('status', 'attribute', $status);

        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'name' => 'day',
            'options' => array(
                'showAnim' => 'fold',
                'showOn' => 'both',

            ),
            'htmlOptions' => array(
                'style' => 'width:80px;vertical-align:top; margin-left:20px'
            ),
        ));
    }

    ?>



    <div class="row buttons">


        <?php echo CHtml::Button('SUBMIT', array('onclick' => 'send();')); ?>


    </div>

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

</div><!-- form -->
<script type="text/javascript">

    function send() {
        var data = $("form").serialize();


        $.ajax({
            type: 'POST',
            url: '<?php echo Yii::app()->createAbsoluteUrl("raport/byDay"); ?>',
            data: data,
            success: function (data) {

            },
            error: function (data) { // if error occured
                alert("Error occured.please try again");
                alert(data);
            }

//            dataType:'html'
        });

    }


</script>

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $arrayDataProvider,
    'template' => "{items}",
    'htmlOptions' => array(
        'style' => 'margin:8px;'
    ),
    'columns' => array(
        array(
            'name' => 'Order id',
            'value' => 'CHtml::encode($data["order"])',
            'htmlOptions' => array(
                'style' => 'width:50px;'
            )
        ),
        array(
            'name' => 'Status',
            'value' => 'CHtml::encode($data["status"])',
        ),
        array(
            'name' => 'Timestamp',
            'value' => 'CHtml::encode($data["change_ts"])',
        ),
    ),
));
?>

So I want user to select option from dropdown, select date and make SQL query from this values. Then I want to pass results to CGridView.

Please Help... I'm stuck

Was it helpful?

Solution

This is what gii generates:

Yii::app()->clientScript->registerScript('search', "
$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('category-grid', {
        data: $(this).serialize()
    });
    return false;
});
");

Use the $.fn.yiiGridView.update to update your grid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top