リファクタリングまたは設計の助けが必要です:)。私は自分のwebappをやった、それはうまくいく。しかし、Yii の Ajax プロセスはまだよく理解できません。たとえば、$.fn.yiiGridView.update()
メソッドを使用すると、 のコンテンツだけでなく、すべての Web ページでこの戻りが見られましたCGridView
。私にとっては面白かったです。
しかし今: インデックス ビューでは、CGridView
ページャーを使用せずに を使用しています。これは単純な賭けゲームの Web アプリです。そして、index.php でページに 10 のベット/結果のみを表示し、10 秒後に次の 10 のベット/結果を表示し、10 秒後に次の 10 の結果/ベットを表示します :)、JavaScript を使用します。
このような単純なプロセス:
- actionIndex() が呼び出され、index.php がレンダリングされます (index.php にはデザインの JS コードが含まれていますが、webapp が結果を表示する CGridView は含まれていません)。
- index.php は、_ajaxIndex.php ファイルの内容をレンダリングします。
- JS コードは、次の 10 件の結果を計算します。これは Web ページで表示する必要があります。
- actionAjaxIndex() が呼び出されます。これにより、_ajaxIndex.php の内容が更新され、3. から再び繰り返されます。
- JS コードは、次の 10 個の結果を再計算します...
注意:管理者が管理者の Web ページに結果を挿入している間、Web アプリケーションは一時的な結果を表示する必要があります。これが、_ajaxIndex.php の概要とラウンド JS 変数を更新する必要がある理由です。
コントローラ
/**
* Lists all models.
*/
public function actionIndex() {
Yii::app()->language='hu';
$model = new Result('search');
$model->unsetAttributes();
if (isset($_GET['Result']))
$model->attributes = $_GET['Result'];
if (isset($_POST['offset']) && $_POST['offset'] >= 0)
$model->offset = $_POST['offset'];
$summary = Result::getCountSavedResults();
$model->isLimited = true;
$this->layout='projector';
$this->render('index', array('model' => $model, 'summary'=>$summary));
//$this->actionAjaxIndex();
}
/**
* List all models by Ajax request.
*/
public function actionAjaxIndex() {
Yii::app()->language='hu';
$model = new Result('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Result']))
$model->attributes = $_GET['Result'];
if (isset($_POST['offset']) && $_POST['offset'] >= 0)
$model->offset = $_POST['offset'];
$summary = Result::getCountSavedResults();
$model->isLimited = true;
$this->renderPartial('_ajaxIndex', array('model' => $model, 'summary'=>$summary));
}
actionIndex() でこのコードの繰り返しを終了したいと思います。しかし、どうしたらよいかわかりません... actionAjaxIndex などを呼び出そうとしましたが、actionAjaxIndex を呼び出す前に、Yii から PHP エラーが発生しました。(集計変数が存在しない等)
表示 - Index.php
<!--<h1><?php echo Yii::t('strings','Results'); ?></h1>-->
<?php
echo CHtml::image(Yii::app()->request->baseUrl.'/images/toplista.jpg', "Fogadás");
?>
<script type="text/javascript">
// Initialize the variables for calculating
var summary = <?php echo $summary ?>; // get all stored results
var timeout = 10 * 1000; // in Milliseconds -> multiply with 1000 to use seconds
var current = 0;
var turn = 0;
var rounds = Math.floor(summary / 10);
</script>
<?php $this->renderPartial('_ajaxIndex', array('model'=>$model, 'summary'=>$summary)); ?>
<script type="text/javascript">
// Refresh the CGridView's content in _ajaxIndex.php
window.setInterval("refresh()", timeout);
// Get the offset to the search() to set the criteria
// Increase turn.
function counter(){
turn += 1;
if(turn > rounds){
turn = 0;
}
return turn *10;
}
function refresh() {
<?php
echo CHtml::ajax(array(
'url'=> CController::createUrl("result/ajaxIndex"),
'type'=>'post',
'data'=> array('offset'=>'js: counter()'),
'replace'=> '#ajax-result-grid',
))
?>
}
</script>
表示 - _ajaxIndex.php
<?php
/* @var $model Result */
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ajax-result-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'header'=>Yii::t('strings','No.'),
'value'=> $model->offset.' + $row+1',
'htmlOptions'=>array('style'=>'width:50px;'),
),
array(
'header'=>Yii::t('strings','team_name'),
'name'=>'team_id',
'value'=>'$data->team->name'
),
array(
'header'=>Yii::t('strings','value'),
'name'=>'value',
'value'=>'$data->value'
),
),
)); ?>
<script type="text/javascript">
// This is need while the admins insert the results during this page is run.
summary = <?php echo $summary ?>;
rounds = Math.floor(summary / 10);
</script>
はい、私は Yii の Ajax プロセスを明確に理解していないと思います :/.