首页 > 编程语言 >thinkphp+ajax+jquery异步实现加载更多

thinkphp+ajax+jquery异步实现加载更多

时间:2022-08-20 20:55:44浏览次数:56  
标签:jquery load button click ajax thinkphp data page more

1.后台请求的方法(这里主要赋模板,再查出总页数)

public function _initialize(){
parent::_initialize();
$this->model['farm'] = D('farm');
$this->model['farmnums'] = M('FarmNums');
$this->pagesize = 8;
}
//菜园列表
public function flist(){

$t = D('FarmListView');
$data = $t->field(true)->order('id desc')->select();
$pagesize = $this->pagesize;
$totals= count($data);
$total_pages = ceil($totals/$pagesize); #计算总页面数
$this->total_pages = $total_pages;
$this->assign('fname',$fname);
$this->display();
}

2.模板中,将异步加载的内容全部赋在id为results的div里面,内容在php中拼接

<div class="cd-show" id="results">
</div>

<div class="pages wbox" id="more_contents">
<a href="javascript:;" class="load_more" id="load_more_button">加载更多</a>
<div class="animation_image" style="display:none;"><img src="ajax-loader.gif"> Loading...</div>
</div>

<script type="text/javascript">
$(document).ready(function() {
var track_click = 0; //track user click on "load more" button, righ now it is 0 click

var total_pages = {$total_pages};
var url = "__URL__/data";
$('#results').load(url, {'page':track_click}, function() {track_click++;}); //initial data to load

$(".load_more").click(function (e) { //user clicks on button

$(this).hide(); //hide load more button on click
$('.animation_image').show(); //show loading image

if(track_click <= total_pages) //make sure user clicks are still less than total pages
{
//post page number and load returned data into result element
$.post(url,{'page': track_click}, function(data) {
//alert(data);exit();

$(".load_more").show(); //bring back load more button

$("#results").append(data); //append data received from server

//scroll page to button element
$("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);

//hide loading image
$('.animation_image').hide(); //hide loading image once data is received

track_click++; //user click increment on load button

}).fail(function(xhr, ajaxOptions, thrownError) {
alert(thrownError); //alert any HTTP error
$(".load_more").show(); //bring back load more button
$('.animation_image').hide(); //hide loading image once data is received
});


if(track_click >= total_pages-1)
{
//reached end of the page yet? disable load button
$("#more_contents").css("display", "none");
}
}

});
});
</script>

3.ajax请求的方法

public function data(){
$t = D('FarmListView');
$item_per_page = $this->pagesize; //每页的条数
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}

//起始位置
$position = ($page_number * $item_per_page);


$data = $t->field(true)->order('id desc')->limit($position,$item_per_page)->select();
// p($data);exit();
echo '<ul>';
foreach($data as $k=>$v){
echo '<li id="item_'.$v["fid"].'">';
echo '<a href="'.U("Farm/detail/",array("id"=>$v[fid])).'">';
echo '<div class="pr">';
echo '<div class="cd-img"><img src="'.__ROOT__.C('UPDATE_PATH').$v["fimg_url"].'"0 /></div><div class="cd-name">'.$v[title].'</div></div>';
echo '<div class="cd-xx wbox"><span class="wfl"><em class="jg">'.$v[farm_price].'</em>元<em class="dw">(一茬)</em></span><span class="wfl syf">剩余:'. intval($v['farm_sarea']/10) .'份</span></div></a>';
echo '</li>';
}
echo '</ul>';

}
————————————————
版权声明:本文为CSDN博主「魁兄」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zuokuikui/article/details/45397329

标签:jquery,load,button,click,ajax,thinkphp,data,page,more
From: https://www.cnblogs.com/jmbt/p/16608582.html

相关文章

  • thinkPHP实现基于ajax的评论回复功能
    https://www.jb51.net/article/142412.htm本文实例讲述了thinkPHP实现基于ajax的评论回复功能。分享给大家供大家参考,具体如下:控制器代码:1234567891......
  • jQuery_ajax调用的几种方法
    一、$.ajax()的基础使用 <buttonid="btn">发送请求</button><scriptsrc="/js/jquery.min.js"></script><script>varparams={name:'wangwu',age:300}$('#bt......
  • jQuery实现简单弹窗遮罩效果
    https://www.jb51.net/article/106880.htm效果图:图(1)初始图图(2)点击按钮后代码如下:1234567891011121314151617181920212223242526......
  • JQuery案例
    JQuery案例广告的自动显示和隐藏案例需求:当页面加载完3秒后自动显示广告广告显示5秒后自动消失分析:使用定时器来完成setTimeout(执行一次定时器)分析完成JQu......
  • JQuery插件
    JQuery插件插件:增强JQuery的功能1.实现方式: 1.$.fn.extend(object)增强通过Jquery获取的对象的功能$("#id")HTML代码<!DOCTYPEhtml><html><head><me......
  • jquery中attr方法和val方法的区别
    这几天一直在看jquery,感觉attr方法和val方法没有什么区别,经过试验,有点明白了这两个方法的区别!分享一下,如果理解错误,请大家指正!以下是源码:<!DOCTYPEhtml><html><head><......
  • jQuery on()方法示例及jquery on()方法的优点
    https://www.jb51.net/article/71614.htm#jQueryon()方法是官方推荐的绑定事件的一个方法。1$(selector).on(event,childSelector,data,function,map)......
  • JQuery事件绑定
    JQuery事件绑定1.jquery标准的绑定方式jq对象.事件方法(回调函数)HTML代码<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>绑......
  • JQuery_DOM操作_属性操作_CRUD操作
    CRUD操作append():父元素将子元素追加到末尾对象1.append(对象2):将对象2添加到对象1元素内部,并且在末尾prepend():父元素将子元素追加到开头对象1.prepend(对象2):将......
  • AJAX概念和AJAX实现_原生JS方式
    AJAX概念:概念:ASynchronousJavaScriptAndXML异步的JavaScript和XMLAJAX是一种在无需重新加载整个网页的情况下能够更新部分网页的技术。通过在后台于服务器进行少量......