通过ajax重新加载Yii小部件


Yii widget reload via ajax

我需要通过AJAX每XX秒重新加载我的Yii小部件的内容。
这是我的小部件代码:

class UserOnlineWidget extends CWidget
{
  public function init(){
  }
  public function run(){
    $userOnline=User::getOnlineUser();       
    $this->render("userOnLineWidget", array('userOnline'=>$userOnline));
  }
}

这是视图useronlinewidget。php:

<div class="userOnline">
<h5>User Online</h5>
<ul>
<?php 
    foreach($userOnline as $user) { 
    ?>
    <li>
        <ul>
            <li><?php echo CHtml::link($user['username'], array('/site/view/'.$user['id'])); ?></li>
            <li><?php echo ($user['online'] != null)? 'online' : 'offline'; ?></li>
        </ul>
    </li>
    <?php 
    }
?>
</ul>
</div>

我该怎么做?

下面是一个小示例。我希望这对你有所帮助。

显示服务器每秒钟的时间。
//SiteController
public function actionTest()
{
    $this->render('my_view'); //just rendering view
}
public function actionContent()
{
    $this->widget('time'); //return html of widget. use for ajax
}
//my_view.php
<script src="<?php echo  $this->assetsBase ?>/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        setInterval(function() {
            $.get('/site/content', {}, function(data) {
               $('#widget').html(data);
            });
        }, 1000); //update widget content every second
    });
</script>
<div id="widget">
    <?php $this->widget('time'); // render widget  ?>
</div>
//widget
class Time extends CWidget
{
    public function init(){
    }
    public function run(){
        $date = new DateTime();
        $this->render("test", array('time'=>$date->format('H:i:s')));
    }
}
//view for widget test.php
<p><?= $time ?></p>