ThinkPHP+jquery实现“加载更多”功能代码

在很多web端界面中都会用到点击按钮加载最新几条数据的demo,下例为使用thinkphp+jquery实现实例:

ThinkPHP+jquery实现“加载更多”功能代码

要实现的结果大致如下

第一步

模板文件

<!--软件--> <div> <ul> {volist} <li> <div> <img src="https://www.jb51.net/public/static/images/{$vo.Pic}" alt=""></div> <div> <div> <h2> <a href="#nogo" >{$vo.AppName}</a></h2> <span>8.59MB</span></div> <div> <div> <div> <p></p> </div> </div> <span>{$vo.DownloadCount}万次下载</span></div> </div> <div> <a href="https://shouji.360tpcdn.com/170214/5aeae868026625e95b389b357fbdd186/com.ss.android.article.video_116.apk" > <span></span>下载</a> </div> </li> {/volist} </ul> <if condition="count($apps) eq 5"> <div> <a href="javascript:;" data-type="1">加载更多</a> </div> </if> <div> </div> </div>

第二步

后台文件

class Index { //打印首页 public function index() { $total=db('apps')->count(); $apps=db('apps')->where('AppStatus',1)->limit(5)->order("AppID ASC")->select(); //var_dump($apps); $view = new View(); $view->assign('total',$total); $view->assign('apps',$apps); return $view->fetch('index'); } public function data() { $start = Input('post.start'); //echo($start); $list = db('apps')->limit($start, 5)->order('AppID asc')->select(); return (array( 'result'=>$list,'status'=>1, 'msg'=>'获取成功!')); } }

第三步

模板中的异步js

<script> //加载更多 var nStart = 5; $('#loadmore').click(function() { var _this = $(".xinhao"); if(nStart >= {$total}) { //alert('后面没有数据了!'); $("#loadmore").text('没有数据了亲...').css({"border-top":"1px solid #d4d5d6","height":"30px","line-height":"30px"}); return false; } else { $.post("{:url('Index/data')}", {start: nStart}, function(res) { $.each(res['result'], function(i, item) { _this.append('<li>\ <div>\ <img src="/public/static/images/'+item.Pic+'"alt=""/></div>\ <div>\ <div>\ <h2>\ <a href="#nogo" >'+item.AppName+'</a></h2>\ <span>8.59MB</span></div>\ <div>\ <div>\ <div>\ <p></p>\ </div>\ </div>\ <span>'+item.DownloadCount+'万次下载</span></div>\ </div>\ <div>\ <a href="#" >\ <span></span>下载</a>\ </div>\ </li>'); }); }); nStart += 5; } }); </script>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwwjgd.html