Silverstripe 使用 jquery ajax 自动加载内容
Silverstripe auto load content with jquery ajax
我需要一些帮助来调整 PHP 中的这个简单脚本,它可以很好地适应 Silverstripe MVC 系统。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var refreshId = setInterval(function() {
jQuery('#load').fadeOut("fast", function () {
jQuery(this).load("reload.php", function () {
jQuery(this).fadeIn("fast");
});
});
}, 1000 * 15);
});
</script>
我的主要问题是如何将部分代码加载到模板而不是整页内容。在此示例中,reload.php 只是代码的一部分。但是在 Silverstripe 中,我需要将一部分代码渲染到模板并从控制器中检索它。
有人知道怎么做吗?
谢谢!
我不知道你有没有更好的主意来做这个,但有我的!它很适合我的使用。我是初级程序员 ;)
routes.yml
Director:
rules:
'ajax//$Action/$ID': 'AjaxController'
ajaxcontroller.php
class AjaxController extends Controller {
private static $allowed_actions = array(
'news'
);
public function news(){
$News = News::get()->sort('Sort',DESC);
if (Director::is_ajax()){
// Ajax!
return $this->renderWith('AjaxInclude', array('News' => $News) );
} else {
// Not ajax!
//return print_r($Pages);
}
}
}
includes/AjaxIncludes.ss
<% loop News %>
<p>$Title</p>
<% end_loop %>
HomePage.php 或其他页面
class HomePage_Controller extends Page_Controller {
public function init() {
parent::init();
Requirements::javascript($this->ThemeDir()."/javascript/ajax.js");
}
}
ajax.js
jQuery.noConflict();
(function($) {
$(document).ready(function() {
var refreshId = setInterval(function() {
$.ajax({
url: "ajax/news",
cache: false,
dataType: "html",
success: function(data) {
if(!data){
$("#responsecontainer").removeClass('online');
$("#responsecontainer").addClass('offline');
$("#responsecontainer").fadeOut();
//alert("offline");
$("#responsecontainer").html(data);
} else {
$("#responsecontainer").removeClass('offline');
$("#responsecontainer").addClass('online');
//alert("online");
$("#responsecontainer").html(data);
}
}
});
}, 1000 * 5);
});
}(jQuery));
最后,ID responsecontainer 必须在 HomePage.ss 中才能激活或停用
我需要一些帮助来调整 PHP 中的这个简单脚本,它可以很好地适应 Silverstripe MVC 系统。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var refreshId = setInterval(function() {
jQuery('#load').fadeOut("fast", function () {
jQuery(this).load("reload.php", function () {
jQuery(this).fadeIn("fast");
});
});
}, 1000 * 15);
});
</script>
我的主要问题是如何将部分代码加载到模板而不是整页内容。在此示例中,reload.php 只是代码的一部分。但是在 Silverstripe 中,我需要将一部分代码渲染到模板并从控制器中检索它。
有人知道怎么做吗? 谢谢!
我不知道你有没有更好的主意来做这个,但有我的!它很适合我的使用。我是初级程序员 ;)
routes.yml
Director:
rules:
'ajax//$Action/$ID': 'AjaxController'
ajaxcontroller.php
class AjaxController extends Controller {
private static $allowed_actions = array(
'news'
);
public function news(){
$News = News::get()->sort('Sort',DESC);
if (Director::is_ajax()){
// Ajax!
return $this->renderWith('AjaxInclude', array('News' => $News) );
} else {
// Not ajax!
//return print_r($Pages);
}
}
}
includes/AjaxIncludes.ss
<% loop News %>
<p>$Title</p>
<% end_loop %>
HomePage.php 或其他页面
class HomePage_Controller extends Page_Controller {
public function init() {
parent::init();
Requirements::javascript($this->ThemeDir()."/javascript/ajax.js");
}
}
ajax.js
jQuery.noConflict();
(function($) {
$(document).ready(function() {
var refreshId = setInterval(function() {
$.ajax({
url: "ajax/news",
cache: false,
dataType: "html",
success: function(data) {
if(!data){
$("#responsecontainer").removeClass('online');
$("#responsecontainer").addClass('offline');
$("#responsecontainer").fadeOut();
//alert("offline");
$("#responsecontainer").html(data);
} else {
$("#responsecontainer").removeClass('offline');
$("#responsecontainer").addClass('online');
//alert("online");
$("#responsecontainer").html(data);
}
}
});
}, 1000 * 5);
});
}(jQuery));
最后,ID responsecontainer 必须在 HomePage.ss 中才能激活或停用