phpcms列表分页ajax加载更多
1.在phpcms\modules\content\index.php文件中添加以下函数:
/*列表分页ajax加载更多*/
public function homeajaxlist() {
if(isset($_GET['siteid'])) {
$siteid = intval($_GET['siteid']);
} else {
$siteid = 1;
}
$page = $_GET['page']?$_GET['page']:1;
$siteid = $GLOBALS['siteid'] = max($siteid,1);
define('SITEID', $siteid);
$_userid = $this->_userid;
$_username = $this->_username;
$_groupid = $this->_groupid;
//SEO
$SEO = seo($siteid);
$sitelist = getcache('sitelist','commons');
$default_style = $sitelist[$siteid]['default_style'];
$CATEGORYS = getcache('category_content_'.$siteid,'commons');
include template('content','list_ajax',$default_style);
}
2.在模板中添加list_ajax.html
{pc:content action="lists" catid="256" num="8" page="$page" order="id DESC" thumb="" moreinfo="1" }
{loop $data $n $r}
<div class="left_news">
<h2><a href="{$r['url']}" target="_blank">{$r['title']}</a></h2>
<h3><a href="{$r['url']}" target="_blank">{$r['description']}</a></h3>
<h4>{date('Y-m-d H:i:s', $r['inputtime'])}</h4>
</div>
{/loop}{/pc}
3.在首页模板预定位置添加以下代码:
<div id="ajax_list" > {template "content","list_ajax"}</div>
<div class="left_morenews">
<div id="showmore" style="cursor:pointer">显示更多新闻>></div>
</div>
添加以下js:
$(function(){
$('#showmore').click(function(){
var temp=$("#currPage").val();
$.ajax({
url:"/index.php?a=homeajaxlist&page="+temp,
type:"post",
async:true,
dataType:"html",
success: function(data){
//debugger;
$('#ajax_list').append(data);
temp++;
$("#currPage").val(temp);
if(data==""){
$('#ajax_list').append("<div class='left_morenews'>已显示全部!</div>");
$("#showmore").hide();
}
},
error: function(data){
//debugger;
alert("出错了!");
return false;
}
});
});
});