thinkPHP+LayUI 懒加载实现_蓝瞳_前端开发者

html

<div class="layui-container" id="container"> </div>
<div class="layui-container" id="container"> </div>

<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>
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>

function() {
var layui.jquery;
var layui.flow;
flow.load({
elem:
//流加载容器//滚动条所在元素,一般不用填,此处只是演示需要。function//执行下一页的回调 console.log(page)
//模拟数据插入function(){
var [];
varpage
$.get(url,
function (res) {

layui.each(res.msg.data,
function(index, item) {
lis.push(
<div class=”layui-row list”>\
<a href=”newsDesc/id/
“>\
<div class=”layui-col-xs4 layui-col-sm4 “>\
<img src=”
“>\
</div>\
<div class=”layui-col-xs7 layui-col-sm7 right”>\
<div class=”title”>
</div>\
<div class=”intro”>
</div>\
</div>\
</a>\
</div>\
<hr/>
);
});
//组装html//pages为Ajax返回的总页数,只有当前页小于总页数的情况下,才会继续出现加载更多 res.msg.pages);

})
},
);
}
});
});

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 json($list);
    }
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 json($list);
    }

function ajaxNews()
{
$page
//页码;
$list[
getNewsList($page,$pagesize);
$count
getNewsCount();
$list[
return json($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;
    }
// 获取动态列表
    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;
    }

// 获取动态列表publicfunction$page$pagesize)
{
$list)
)
)
])
$page$pagesize)
select();
return$list;
}

//获取动态总条数publicfunction getNewsCount()
{
$countcount();
return$count;
}

 

» 本文来自:前端开发者 » 《thinkPHP+LayUI 懒加载实现_蓝瞳_前端开发者》
» 本文链接地址:https://www.rokub.com/73423.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!