2014-02-22 41 views
0

我在我的控制器:Yii通过ajax按钮更新cgridview不起作用。

public function actionFilterClients { 
    if (Yii::app()->request->isAjaxRequest) { 
     if (isset($_POST['category_id'])) { 
      $criteria = new CDbCriteria; 
      $criteria->condition = "user_id=:user_id"; 
      $criteria->params = array(':user_id' => Yii::app()->user->id); 
      $criteria->compare('category_id',$_POST['category_id'],true); 

      $dataProvider = new CActiveDataProvider('Client', array(
            'criteria'=>$criteria, 
           )); 
      $this->renderPartial('transfer_step_3' , array('dataProvider'=>$dataProvider)) ; 
     } 
    } 
} 

以我除其他事项外视图我有:

<?php $filter=$this->beginWidget('CActiveForm', array(
      'id'=>'client-filter-form', 
      'enableAjaxValidation'=>false, 
      'htmlOptions'=>array('class'=>'form-horizontal'), 
     )); ?> 
     <label for="category_id">View clients in category:</label> 
     <?php echo CHtml::dropDownList('category_id','',Client::clientCategories(), array('options' => array('2'=>array('selected'=>true)))); ?> 

     <?php 
      echo CHtml::ajaxButton(
       'Filter Clients', 
       'filterclients', 
       array(
        'type'=>'POST', 
        'update' => 'client-grid' , 
        'success' =>"function(data) { 
     \$.fn.yiiGridView.update('client-grid');}", 
       ) 
      ); 
     ?> 
     <?php $this->endWidget(); ?> 

<?php $this->widget('bootstrap.widgets.TbGridView',array(
      'type'=>'bordered striped condensed', 
      'id'=>'client-grid', 
      'ajaxUpdate' => true , 
      'rowCssClassExpression'=>'($data->duplicate==2)?"yellow":($data->duplicate==1?"blue":"")', 
      'dataProvider'=>(isset($dataProvider)?$dataProvider:$clients->moveclients()), 
      'template'=>"{items}\n{pager}", 
      'columns'=>array(
       array(
        'class'=>'CCheckBoxColumn', 
        'selectableRows'=>2, 
        'id'=>'clients', 
       ), 
       'name', 
       'surname', 
       'telephone', 
       'email', 
       array(
        'header'=>'Category', 
        'name' => 'category_title', 
        'type' => 'raw', 
        'value' => '$data->category->title', 
       ), 
      ), 
     )); ?> 

因为这是一个多步骤的形式中,cgridview dataprovider默认列出所有客户端($ clients-> moveclients()列出所有客户端)。

ajax按钮将category_id正确地发布到客户端/ filterclients url。

我可以用萤火是actionFilterClients正确返回呈现的HTML(与正确的客户端)看到,但在GridView不更新......

为什么没有任何想法?

+0

请从您的代码中删除此行,看看有什么需要? '成功'=>“函数(数据)$ .fn.yiiGridView.update('client-grid');}” –

+0

现在filterclients正确调用,但萤火虫POST响应为空 – ImWorkingOnIt

+0

好吧,看起来像isset $ _POST)返回false,因此如果代码不在里面。请写下这些行CVarDumper :: Dump($ _ POST,100,true); die()紧接着你检查的地方isAjaxRequest –

回答

0

最后,我添加了另一个观点,即它只有一个gridview,因此修改了代码:

控制器:

$this->renderPartial('_ajax_transfer_step_3' , array('dataProvider'=>$dataProvider)) ; 

原始视图:

<?php $filter=$this->beginWidget('CActiveForm', array(
      'id'=>'customer-filter-form', 
      'enableAjaxValidation'=>false, 
      'htmlOptions'=>array('class'=>'form-horizontal'), 
     )); ?> 
     <label for="category_id">View customers in category:</label> 
     <?php 

      echo CHtml::dropDownList('category_id', '', Customer::customerCategories(), 
       array(
         'ajax' => array(
           'type'=>'POST', 
           'url'=>CController::createUrl('filtercustomers'), 
           'data'=>'js:jQuery(this).serialize()', 
           'success'=>'function(response) { 
             jQuery("#customer-grid").html(response) 

           }', 
         ) 
       ) 
      ); 
     ?> 
     <?php $this->endWidget(); ?> 

现在它只是替换页面的一部分。

我还没有想出为什么我的原始代码没有更新gridview。