How to use pagination on HTML tables?

前端 未结 8 609
长发绾君心
长发绾君心 2020-12-07 21:41

I am trying to use this Pagination library in my HTML table page (specifically light theme) but somehow I am not able to understand how to plugin this library in such a way

8条回答
  •  鱼传尺愫
    2020-12-07 22:01

    Pure js. Can apply it to multiple tables at once. Aborts if only one page is required. I used anushree as my starting point.

    Sorry to the asker, obviously this is not a simplePagignation.js solution. However, it's the top google result when you type "javascript table paging", and it's a reasonable solution to many who may be considering a library but unsure whether to go that route or not.

    Use like this:

    addPagerToTables('#someTable', 8);
    

    Requires no css, though it may be wise to initially hide table tBody rows in css anyway to prevent the effect of rows showing then quicky being hidden (not happening with me right now, but it's something I've seen before).

    The code:

    function addPagerToTables(tables, rowsPerPage = 10) {
    
        tables = 
            typeof tables == "string"
          ? document.querySelectorAll(tables)
          : tables;
    
        for (let table of tables) 
            addPagerToTable(table, rowsPerPage);
    
    }
    
    function addPagerToTable(table, rowsPerPage = 10) {
    
        let tBodyRows = table.querySelectorAll('tBody tr');
        let numPages = Math.ceil(tBodyRows.length/rowsPerPage);
    
        let colCount = 
        [].slice.call(
            table.querySelector('tr').cells
        )
        .reduce((a,b) => a + parseInt(b.colSpan), 0);
    
        table
        .createTFoot()
        .insertRow()
        .innerHTML = ``;
    
        if(numPages == 1)
            return;
    
        for(i = 0;i < numPages;i++) {
    
            let pageNum = i + 1;
    
            table.querySelector('.nav')
            .insertAdjacentHTML(
                'beforeend',
                `${pageNum} `        
            );
    
        }
    
        changeToPage(table, 1, rowsPerPage);
    
        for (let navA of table.querySelectorAll('.nav a'))
            navA.addEventListener(
                'click', 
                e => changeToPage(
                    table, 
                    parseInt(e.target.innerHTML), 
                    rowsPerPage
                )
            );
    
    }
    
    function changeToPage(table, page, rowsPerPage) {
    
        let startItem = (page - 1) * rowsPerPage;
        let endItem = startItem + rowsPerPage;
        let navAs = table.querySelectorAll('.nav a');
        let tBodyRows = table.querySelectorAll('tBody tr');
    
        for (let nix = 0; nix < navAs.length; nix++) {
    
            if (nix == page - 1)
                navAs[nix].classList.add('active');
            else 
                navAs[nix].classList.remove('active');
    
            for (let trix = 0; trix < tBodyRows.length; trix++) 
                tBodyRows[trix].style.display = 
                    (trix >= startItem && trix < endItem)
                    ? 'table-row'
                    : 'none';  
    
        }
    
    }
    

提交回复
热议问题