【问题标题】:Call Render Partial using time interval in YII在 YII 中使用时间间隔调用 Render Partial
【发布时间】:2013-07-26 13:26:23
【问题描述】:

我想通过刷新整个页面自动更新 div 内容。所以我在 YII 中做了 Ajax renderPartial。现在我使用 AJAX 按钮 onclick 实现

我的代码如下

<?php 
      echo CHtml::ajaxButton ("Update data",
      CController::createUrl("blog/UpdateAjax?url=$url"), 
      array('update' => '#inrscrn'));
?>

现在我想在时间限制内渲染,请帮忙

【问题讨论】:

  • 当你说“有时间限制的渲染”时,你到底是什么意思?
  • 您将需要一个 javascript 时间事件。像这样window.setInterval("javascript function",milliseconds);

标签: php jquery ajax yii


【解决方案1】:

你的问题不是很清楚。我想您想在 div 中设置内容的自动和定期刷新,而不是单击按钮。

这是您页面上需要的 JavaScript:

<script type="text/javascript">
    timeout = 60 * 1000; // in Milliseconds -> multiply with 1000 to use seconds
    function refresh() {
        <?php
        echo CHtml::ajax(array(
                'url'=> CController::createUrl("blog/UpdateAjax?url=".$url),
                'type'=>'post',
                'update'=> '#inrscrn',
        ))
        ?>
    }
    window.setInterval("refresh()", timeout);
</script>

但是将 URL 发送到您的控制器不是一个好方法,而是直接请求对需要返回对应数据的控制器进行特殊的 AJAX 返回。

<?php
public function actionTest(){
        if (isset($_REQUEST['AJAX']) || Yii::app()->getRequest()->getIsAjaxRequest()) {
            $this->renderPartial(
                'test',
                array('model' => $model),
                false,
                true
            );
        } else {
            $this->render(
                'test',
                array('model' => $model),
            );
        }
}
?>

【讨论】:

    猜你喜欢
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多