ThinkPHP + layui stream loading implementation

Time:2019-11-14

html

JS, to introduce layui.js

layui.use('flow', function() {
    var $ = layui.jquery;
    var flow = layui.flow;
        flow.load({
        Elem: 'container' // stream load container
           //The element where the scroll bar is located is generally not required to be filled in. This is just for demonstration purposes.
        , done: function (page, next) {// execute the callback on the next page
          console.log(page)
          //Analog data insertion
          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('\
                            \
                                \
                                    \
                                \
                                \
                                    '+item.title+'\
                                    '+item.intro+'\
                                \
                            \
                        \
                         ');
                    }); // assemble HTML
                    //Pages is the total number of pages returned by Ajax. Only when the current page is less than the total number of pages, will more loading continue
                    next(lis.join(''), page <= res.msg.pages);
                
            })
          }, 300);
        }
      });
});

PHP controller

public function ajaxNews()
    {
        $page = input ('page '); // page number
       $pagesize = 6;
       $list['data'] = model('Index')->getNewsList($page,$pagesize);
       $count= model('Index')->getNewsCount();
       $list['pages'] = ceil($count/$pagesize);
     return $list;

    }

PHP model model

//Get dynamic list
    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;
    }

    //Get the total number of dynamic items
    public function getNewsCount()
    {
        $count = Db::name('news')->where(['status'=>0])->();
        return $count;
    }