[ Solved - Thx All! ] Ajax - Cgridview's Content Doesn't Refresh

Hi,

I have a little problem. I use an Ajax method to refresh the content of the cGridView at every 2 seconds on the page.

The ajax method is good, I see that the fresh data (content of cGridView) but in the view (at the page) I don’t see any refresh…

In this screenshot you can see that I get back the fresh results, but my cGridView still shows me the old results.

Controller:




public function actionIndex()

	{

                

                //$turn = Turn::model()->find("`setting`='current_turn'");

                $model=new Result('search');

                $model->unsetAttributes(); 

                //$criteria=new CDbCriteria;                

                //$criteria->condition = "turn=".$turn->value;

            if(isset($_GET['Result']))

			$model->attributes=$_GET['Result'];

		//$dataProvider=new CActiveDataProvider('Result', array('criteria' => $criteria));

		$this->render('index',array(

			'model'=>$model

		));

	}

public function actionAjaxIndex()

        {

            //$turn = Turn::model()->find("`setting`='current_turn'");

                

            $model=new Result('search');

            $model->unsetAttributes();  // clear any default values

            

            if(isset($_GET['Result']))

                    $model->attributes=$_GET['Result'];

            

           //print_r($model);

            $this->renderPartial('_ajaxIndex', array('model'=>$model));

        }



Yes, I know in the Ajax message, there is a print_r() write out, but when I removed the print_r() the problem doesn’t solve. So this wasn’t the problem.

Model:


public function searchAjax($data=null)

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

		$criteria->compare('team_id',$this->team_id,true);

		$criteria->compare('value',$this->value,true);

		$criteria->compare('turn',$this->turn);

                

                $criteria->condition = "turn=".$this->getActualTurn();

                

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

This function is the same as the original search() method, I will improve this in the future.

Views:

Index.php view




<h1>Results</h1>


<?php $this->renderPartial('_ajaxIndex', array('model'=>$model)); ?>


<?php //echo CHtml::ajaxButton ("Ajax művelet",

      //                        CController::createUrl('result/AjaxIndex'), 

      //                       array('update' => '#data'));

?>




<script type="text/javascript">

    timeout = 2 * 1000; // in Milliseconds -> multiply with 1000 to use seconds

    function refresh() {

       

        <?php

        echo CHtml::ajax(array(

                'url'=> CController::createUrl("result/AjaxIndex"),

                'type'=>'post',

                'update'=> 'ajax-result-grid',

        ))

        ?>

    }

    window.setInterval("refresh()", timeout);

</script>




_ajaxIndes.php view




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'ajax-result-grid',

	'dataProvider'=>$model->searchAjax(),	

	'columns'=>array(

             array(

                'header'=>'No.',

                'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',       //  row is zero based

        ),

		array(

                    'name'=>'team_id',

                    'value'=>'$data->team->name'

                ),

		'value',		

	),

)); ?>



I tried to put the ajax code to this _ajaxIndex view, but it didn’t solved the problem.

Maybe I missed something.

Hi blaces,

Try:




        echo CHtml::ajax(array(

                'url'=> CController::createUrl("result/AjaxIndex"),

                'type'=>'post',

                'update'=> '#ajax-result-grid', // needs '#'

        ))



:D. You are right! :) I missed the ‘#’ I don’t know, how I didn’t see this mistake. Thank you.

I have a problem with the simple ajax (that you can see in above)

My problem is that, when I call it at first time, it makes an extra (and same) div.




<div id="ajax-result-grid" class="grid-view">

<div id="ajax-result-grid" class="grid-view">

<!-- the content of the ajax-result-grid -->

</div>

</div>



But only at the first time, make this. Yes update the ‘ajax-result-grid’ with itself, and not replace the content!.

When I modified for this my code:




<script type="text/javascript">

    function allFine() {

                $.fn.yiiGridView.update('ajax-result-grid');

    }    

    

    timeout = 10 * 1000; // in Milliseconds -> multiply with 1000 to use seconds

    function refresh() {

       

        <?php

        echo CHtml::ajax(array(

                'url'=> CController::createUrl("result/AjaxIndex"),

                'type'=>'post',

                //'update'=> '#ajax-result-grid',

                'success'=>'allFine'

        ))

        ?>

                 

    }

    window.setInterval("refresh()", timeout);

    

</script>



It okay, because doesn’t make an extra <div> in my html output. But this solution makes an extra ajax call.

You can see the screenshot here. And this call give me back the all page!

And here there all two call back. At first I get back the only datas of the grid, and second I get back the all page.

try ‘replace’ instead of ‘update’.

Thanks, this worked :).