Yii:根据表单输入更新CgridView

时间:2013-07-04 13:13:30

标签: php ajax yii cgridview

我已经使用CArrayDataProvider

设置了CGridView

控制器

//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);

观看:

<?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"])',
        ),
    ),
));
?>

所以我希望用户从下拉列表中选择选项,选择日期并根据此值进行SQL查询。然后我想将结果传递给CGridView。

请帮助......我被困了

1 个答案:

答案 0 :(得分:2)

这就是gii生成的内容:

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

使用$ .fn.yiiGridView.update更新网格。