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.