jquery data grid paging Edit

Murugan Andezuthu Dharmaratnam | 12 December 2022 | 108

Subtitle with Paragraph

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sagittis sapien sed diam pellentesque vestibulum. Etiam ultrices porta nulla, at dictum tellus euismod sed. Fusce mauris justo, imperdiet sit amet turpis ut, vestibulum dapibus dolor. Suspendisse vel ante eros. Etiam in viverra magna. Nunc pretium ipsum at nibh consequat condimentum. Nunc quis justo vel dui varius eleifend. Phasellus massa nibh, egestas eu erat quis, laoreet hendrerit quam. Fusce finibus lectus ut libero posuere, bibendum aliquet nisi convallis. Curabitur ultrices nisl ante, sed volutpat purus scelerisque ut. Etiam id ipsum ac justo convallis dapibus non nec sapien. Mauris vulputate sapien vel mi aliquet, ut ultricies lacus bibendum. Nam sodales, arcu et iaculis scelerisque, libero velit hendrerit elit, vitae accumsan nibh ex aliquet libero. Vestibulum nec malesuada elit, non rutrum est. Donec sagittis nisl odio, vitae iaculis eros bibendum a. Suspendisse ligula risus, tristique quis nisl id, bibendum scelerisque tellus. Nam eleifend finibus libero, nec tincidunt orci aliquam eget.

var paging = {
    container: null,
    data: null,
    pageSize: 0,
    pageCount: 0,
    pageIndex: 0,
    contentTemplate: null,
    pageNumberContainer: null,
    pageNumberTemplate: null,
    dataBindFn: null,
    init: function(container, data, pageSize, currentPage,  contentTemplate, pageNumberContainer, pageNumberTemplate, dataBindFn) {
        this.container = container;
        this.data = data;
        this.pageSize = pageSize;
        this.contentTemplate = contentTemplate;
        this.pageNumberContainer = pageNumberContainer;
        this.pageNumberTemplate = pageNumberTemplate;
        this.currentPage = currentPage||1,
        this.maxPagingNumber = 5,
        this.dataBindFn = dataBindFn,
        this.pageCount = function(_data, _pageSize) { 
            var pageCount = _data.length / _pageSize;
            console.log(pageCount);
            if(pageCount % 1 > 0)
            {
                pageCount = Math.trunc(pageCount)    1;
            }else
            {
                pageCount = Math.trunc(pageCount);
            }
            return pageCount;
        }(data, pageSize, currentPage,  contentTemplate, pageNumberContainer, pageNumberTemplate, dataBindFn)
    },
    first: function() {
        this.currentPage = 1;
        this.databind();    
    },
    last: function() {
        this.currentPage = this.pageCount;
        this.databind();    
    },
    next: function() {
        if(this.currentPage < this.pageCount)
        {
            this.currentPage = this.currentPage  1; 
            this.databind();    
        }
    },
    previous: function() {
        if(this.currentPage > 1)
        {
            this.currentPage = this.currentPage-1; 
            this.databind();    
        }
    },
    displaypaging: function() {
        console.log("paging");
        var startPage = this.currentPage - Math.trunc(this.maxPagingNumber / 2);
        if(startPage < 1) {
            startPage = 1;
        }
        var endPage = this.currentPage   Math.trunc(this.maxPagingNumber / 2);
        if(endPage > this.pageCount)
        {
            endPage =  this.pageCount;    
        }
        var container = $("#" this.pageNumberContainer);
        container.empty();
        for (let index = startPage; index <= endPage; index  ) {
            var pageno  = $($("#" this.pageNumberTemplate).html()).clone();
            pageno.click(function(ele, ele2){
                paging.currentPage = parseInt($(this).html());
                paging.displaypaging();
                paging.databind();  
            });
            pageno.html(index);
            container.append(pageno);
        }
        // Previous Page 
        var pageno  = $($("#" this.pageNumberTemplate).html()).clone();
        var pp = paging.currentPage - 1;
        if(pp < 1){ pp  = 1 };
        pageno.click(function(ele, ele2){
            paging.currentPage = pp;
            paging.displaypaging();
            paging.databind();  
        });
        pageno.html("<");
        container.prepend(pageno);  
        // First Page
        var pageno  = $($("#" this.pageNumberTemplate).html()).clone();
        pageno.click(function(ele, ele2){
            paging.currentPage = 1;
            paging.displaypaging();
            paging.databind();  
        });
        pageno.html("<<");
        container.prepend(pageno);
        // Next Page 
        var pageno  = $($("#" this.pageNumberTemplate).html()).clone();
        var lp = paging.currentPage   1;
        if(lp > this.pageCount){ lp  = this.pageCount };
        pageno.click(function(ele, ele2){
            paging.currentPage = lp;
            paging.displaypaging();
            paging.databind();  
        });
        pageno.html(">");
        container.append(pageno);
        // Last Page 
        var pageno  = $($("#" this.pageNumberTemplate).html()).clone();
        pageno.click(function(ele, ele2){
            paging.currentPage = paging.pageCount;
            paging.displaypaging();
            paging.databind();  
        });
        pageno.html(">>");
        container.append(pageno);              
        },
    databind: function() {
        this.pageIndex = (this.currentPage-1) * this.pageSize; 
        var container = $("#" this.container);
        container.empty();
        this.data.slice(this.pageIndex,this.pageIndex this.pageSize).forEach( (ele) => { 
            var content  = $($("#" this.contentTemplate).html()).clone();
            this.dataBindFn(content, ele);
            container.append(content); 
         });
    }
}

HTML Code

<head>
    <style>
        #newsitems span {
            padding: 5 5 5 5;
        }

        #pagenumber span {
            padding: 5 5 5 5;
        }

        #pagenumber {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div id="content">
        <div id="news">
            <div id="newsitems">
            </div>
            <div >
                <div id="pagenumber">
                </div>
            </div>
        </div>
    </div>
    <div id="pagenumbertemplate">
        <span></span>
    </div>
    <div id="contenttemplate">
        <div><span name="id"></span><span name="name"></span></div>
    </div>
    <script type="text/javascript">
        var data = [{ id: 1, name: "Murugan" }, { id: 2, name: "Anantu" }, { id: 3, name: "Joyal" }, { id: 3, name: "Manjunath" }, { id: 5, name: "Mallikarjun" }, { id: 6, name: "Dnyaneshwar" }, { id: 7, name: "Deepika" }, { id: 8, name: "Raghavendra" }, { id: 9, name: "Salu" }, { id: 10, name: "Sanketh" }];
    </script>
    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <script src="paging.js" type="text/javascript"></script>
    <script type="text/javascript">
        paging.init('newsitems', data, 3, 1,'contenttemplate','pagenumber','pagenumbertemplate', dataBind);

        function dataBind(content, obj)
        {
            $("[name='id']",content).html(obj.id);
            $("[name='name']",content).html(obj.name);
        }
    </script>