EJS receives back-end data to realize local paging

Time:2021-11-25

Back end data

var arr=[];
for(let i=0;i<77;i++){
    arr.push({
        index:i+1,
        head: '<h3>ccccccccc</h3>',
        content: '<div>qqqqqqqq,qqqqqqqqqvvvvvvvv<p>cccccc,,cccccc</p></div>'
    })
}
router.get('/', function(req, res, next) {
    res.render('index', {
        News: JSON. Stringify (ARR), // the key is to convert strings
    });
});

Front desk HTML

<div id="content">
    <% var newslist=JSON.parse(news);newslist.forEach(function(item,key){%>
        <%if(key<=9){%>
            <div style="margin-bottom: 10px;">
                <%- item.index%>
                <%-item.head%>
                <%- item.content%>
            </div>
        <%}%>
    <%})%>
</div>
<div id="pages"></div>

JS part

var newslist = JSON.parse('<%-news%>');
function getList(index){
    var reslut= newslist.slice((index-1)*10,index*10);    
    $('#content').empty();
    var dof=document.createDocumentFragment();
    reslut.forEach(element => {
        $(dof).append(`<div style="margin-bottom:10px">${element.index} ${element.head} ${element.content}</div>`)
    });
    $('#content').append($(dof))
};


new Pagination({
    Element: '#pages', // element        
    Type: 2, // style type, optional [1,2]
    PageIndex: 1, // initial page number       
    PageSize: 10, // number of pages       
    Pagecount: 9, // number of pages       
    Total: newslist.length, // total number of data        
    Jumper: false, // display input box jump        
    Singlepagehide: true, // paging is not displayed when there is only one page       
    Prevtext: 'prev', // previous page text        
    Nexttext: 'next', // next page text       
    Disabled: true, // whether to display disabled        
    currentChange: function (index) {
        //Callback when page number changes         
        getList(index);
    }
});

In the example, a paging plug-in is used. You can find similar ones by yourself. There are many, but you can’t find a claw