I have in my controller:

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

In my view among other things I have:

<?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(); ?>

and

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

Because this is a multi-step form, the cgridview dataprovider defaults to listing all clients ($clients->moveclients() lists all clients).

The ajax button posts the category_id to the client/filterclients url correctly.

I can see with firebug that actionFilterClients returns the rendered html correctly (with the correct clients) but the gridview is not updated...

Any ideas on why not?

没有正确的解决方案

其他提示

In the end I added another view that had only a gridview in it and modified my code thus:

Controller:

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

Original view:

<?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(); ?>

Now it just replaces a portion of the page.

I still haven't figured out why my original code didn't update the gridview though.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top