Yii Framework Forum: [ Solved - Thx All! ] Ajax - Cgridview's Content Doesn't Refresh - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[ Solved - Thx All! ] Ajax - Cgridview's Content Doesn't Refresh simple ajax update vs $.fn.yiiGridView.update Rate Topic: -----

#1 User is offline   blaces 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 06-April 12

Posted 24 August 2013 - 01:04 PM

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.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 25 August 2013 - 05:58 AM

Hi blaces,

Try:
        echo CHtml::ajax(array(
                'url'=> CController::createUrl("result/AjaxIndex"),
                'type'=>'post',
                'update'=> '#ajax-result-grid', // needs '#'
        ))

0

#3 User is offline   blaces 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 06-April 12

Posted 25 August 2013 - 07:35 AM

View Postsoftark, on 25 August 2013 - 05:58 AM, said:

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.
0

#4 User is offline   blaces 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 06-April 12

Posted 25 August 2013 - 08:30 AM

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.
0

#5 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 25 August 2013 - 10:37 AM

try 'replace' instead of 'update'.
0

#6 User is offline   blaces 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 06-April 12

Posted 25 August 2013 - 02:23 PM

View PostTsunami, on 25 August 2013 - 10:37 AM, said:

try 'replace' instead of 'update'.


Thanks, this worked :).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users