2013-07-04 70 views
1

我有CArrayDataProvider的Yii:基於表單輸入

控制器

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

,並查看設置CGridView更新CgridView:

<?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。

請幫助...我卡

回答

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更新您的網格。