最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scrollable lazy loading table from server side with js - Stack Overflow

programmeradmin6浏览0评论

I need to display a table with a huge number of items. So I want to implement that with lazy loading from server side. Then when the user scroll down (or up), I call the server to get the next/previous defined number of items and display them instead of the old items (or add them to the old items). Is there a simple way to implement that ? Is there some JavaScript Library that can help me to implement this functionality ? Any help will be appreciated. Thanks

I need to display a table with a huge number of items. So I want to implement that with lazy loading from server side. Then when the user scroll down (or up), I call the server to get the next/previous defined number of items and display them instead of the old items (or add them to the old items). Is there a simple way to implement that ? Is there some JavaScript Library that can help me to implement this functionality ? Any help will be appreciated. Thanks

Share Improve this question edited Oct 20, 2015 at 12:42 Bouabane Mohamed Salah asked Oct 20, 2015 at 12:34 Bouabane Mohamed SalahBouabane Mohamed Salah 5271 gold badge3 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You can achieve this using scrollHeight, clientHeight and scrollTop, to detect when scroll bar is near the bottom area, then you fetch your new items:

This is an example (the demo):

HTML

<div id="container">  
    <div id="scrollbox" >  
        <div id="content" >  
            <p>Lorem ipsum dolor sit amet</p>  
            <p>Ipsum lorem dolor amet sit</p>  
            <p>Dolor lorem ipsum amet tis</p>  
            <p>Lorem ipsum dolor sit amet</p>  
            <p>Ipsum lorem dolor amet sit</p>  
            <p>Dolor lorem ipsum amet tis</p>  
            <p>Lorem ipsum dolor sit amet</p>  
            <p>Ipsum lorem dolor amet sit</p>  
            <p>Dolor lorem ipsum amet tis</p>  
        </div>  
    </div>  
    <p><span id="status" ></span></p>  
</div>  

CSS

#container{   
    width:400px;   
    margin:0px auto;   
    padding:40px 0;   
}  
#scrollbox{   
    width:400px;   
    height:300px;    
    overflow:auto; overflow-x:hidden;   
}  
#container > p{   
    background:#eee;   
    color:#666;   
    font-family:Arial, sans-serif; font-size:0.75em;   
    padding:5px; margin:0;   
    text-align:rightright;  
} 

JavaScript

$('document').ready(function(){  
    updatestatus();  
    scrollalert();  
});  
function updatestatus(){  
    //Show number of loaded items  
    var totalItems=$('#content p').length;  
    $('#status').text('Loaded '+totalItems+' Items');  
}  
function scrollalert(){  
    var scrolltop=$('#scrollbox').attr('scrollTop');  
    var scrollheight=$('#scrollbox').attr('scrollHeight');  
    var windowheight=$('#scrollbox').attr('clientHeight');  
    var scrolloffset=20;  
    if(scrolltop>=(scrollheight-(windowheight+scrolloffset)))  
    {  
        //fetch new items  
        $('#status').text('Loading more items...');  
        $.get('new-items.html', '', function(newitems){  
            $('#content').append(newitems);  
            updatestatus();  
        });  
    }  
    setTimeout('scrollalert();', 1500);  
}  

PS: I copy/past code source from here.

Check this template, follow the ments and you will be able to write your own code. Please remember, that it is only an example.

var $win = $(window),
    $table = $('.table'), // your table
    lazyPoint = 0, // point to call next ajax
    offset = 30, // number of last table row
    count = 30, // number of rows to load with one request
    loading = false; // flag to prevent more than 1 loading at a time

// this function will calc next Y coordinate
// then you reach it, use ajax to get some new table data
function calcLazyPoint () {
    var top = $table.offset().top;
    var height = $table.outerHeight();
    lazyPoint = top + height;
}

// add loaded data to table
function addToTable (data) {
    var html;

    // use some template engine here, like this: http://handlebarsjs./
    // in this function you should convert raw data
    // to HTML, which you will append to table

    $table.append(html); // append data to table
    offset += 30; // increase your offset
    loading = false; // allow to load next data portions

    calcLazyPoint(); // calc next lazy point
}

// Function with ajax request
// it will ask server for new data
// using your offset and count
function getTableData () {
    $.ajax({
        data: {
            offset: offset,
            count: count
        },
        success: addToTable
    });
}

$win.on("scroll", function () {
    var top = $win.scrollTop(); // get scrollTop
    var height = $win.innerHeight(); // viewport height
    var scrollPoint = top + height;

    if (scrollPoint > lazyPoint && !loading) {
        getTableData(); // ajax request
        loading = true; // prevent more than 1 request
    }
});

// fist start
calcLazyPoint();

You can use jQuery.isInView, a library of mine, and pretty much follow the usage example, which implements lazy loading.

发布评论

评论列表(0)

  1. 暂无评论