thinkPHP+LayUI 流加载实现
程序员文章站
2022-05-23 11:14:29
html js,要引入layui.js php Controller控制器 php model模型 ......
html
<div class="layui-container" id="container"> </div>
js,要引入layui.js
layui.use('flow', function() { var $ = layui.jquery; var flow = layui.flow; flow.load({ elem: '#container' //流加载容器 //滚动条所在元素,一般不用填,此处只是演示需要。 ,done: function(page, next){ //执行下一页的回调 console.log(page) //模拟数据插入 settimeout(function(){ var lis = []; var url = "/index/index/ajaxnews/?page="+page $.get(url,function (res) { layui.each(res.msg.data, function(index, item) { lis.push('<div class="layui-row list">\ <a href="newsdesc/id/'+item.id+'">\ <div class="layui-col-xs4 layui-col-sm4 ">\ <img src="'+item.cover_img+'">\ </div>\ <div class="layui-col-xs7 layui-col-sm7 right">\ <div class="title">'+item.title+'</div>\ <div class="intro">'+item.intro+'</div>\ </div>\ </a>\ </div>\ <hr/> '); });//组装html //pages为ajax返回的总页数,只有当前页小于总页数的情况下,才会继续出现加载更多 next(lis.join(''), page <= res.msg.pages); }) }, 300); } }); }); </script>
php controller控制器
public function ajaxnews() { $page = input('page'); //页码 $pagesize = 6; $list['data'] = model('index')->getnewslist($page,$pagesize); $count= model('index')->getnewscount(); $list['pages'] = ceil($count/$pagesize); return $list; }
php model模型
// 获取动态列表 public function getnewslist($page,$pagesize) { $list = db::name('news') ->field('id,title,intro,cover_img') ->order('create_time desc') ->where(['status'=>0]) ->page($page,$pagesize) ->select(); return $list; } //获取动态总条数 public function getnewscount() { $count = db::name('news')->where(['status'=>0])->count(); return $count; }