通过AJAX Yii的部件重装 [英] Yii widget reload via ajax
本文介绍了通过AJAX Yii的部件重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要重新加载我的Yii小部件通过AJAX每XX秒的内容。
这是我的小部件code:
I need to reload a content of my Yii widget via AJAX every XX seconds.
This is my widget code:
class UserOnlineWidget extends CWidget
{
public function init(){
}
public function run(){
$userOnline=User::getOnlineUser();
$this->render("userOnLineWidget", array('userOnline'=>$userOnline));
}
}
和本所认为userOnLineWidget.php:
And this is the view 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>
我怎样才能做到这一点?我用Yii的1.1。
How can I do this ? I use Yii 1.1.
推荐答案
下面一个小例子。我希望这可以帮助您。窗口小部件显示来自服务器的时间每一秒。
Here a small example. I hope this help you. Widget which show time from server every second.
//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>
这篇关于通过AJAX Yii的部件重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文