JS realizes the front-end paging effect

JS realizes the front-end paging effect

This article example shares the specific code of JS to achieve the front-end paging effect for your reference. The specific content is as follows

1. HTML part

<!doctype html>
<html>
<head>
 <meta charset='utf-8'>
    <script src="js/jquery.js"></script>
 <style type="text/css">
  a{text-decoration: none;}
  table {border-collapse:collapse;width: 100%;font-size: 14px;}
  th{background-color: #ddd;}
  table, td, th {border:1px solid #e7e8ec;}
  th,tr{height: 40px;}
  td {text-align: center;}
  tr:hover{background-color: #f9f4f3;}
  .barcon {width: 1000px;margin: 0 auto;text-align: center;}
  .barcon2 {float: right;}
  .barcon2 ul {margin: 20px 0;padding-left: 0;list-style: none;text-align: center;}
  .barcon2 li {display: inline;}
  .barcon2 a {font-size: 16px;font-weight: normal;display: inline-block;padding: 5px;padding-top: 0;color: black;border: 1px solid #ddd;background-color: #fff;}
  .barcon2 a:hover{background-color: #eee;}
  .ban {opacity: .4;}
 </style>
</head>
<body>
<table width="950" cellpadding="0" cellspacing="0" class="table2" align="center">
 <thead>
 <tr align="center">
  <th width="150" height="33" class="td2">Serial number</th>
  <th width="300" class="td2">Username</th>
  <th width="250" class="td2">Permissions</th>
  <th width="250" class="td2">Operation</th>
 </tr>
 </thead>
 <tbody id="myTable">
 <tr align="center">
  <td class="td2" height="33" width="150">1</td>
  <td class="td2" >admin</td>
  <td class="td2" >Administrator</td>
  <td class="td2" ><a href="###" >Modify</a></td>
 </tr>
 </tbody>
</table>
<div id="barcon" class="barcon" >
 <div id="barcon2" class="barcon2">
  <ul>
   <li><a href="###" id="prePage">Previous page</a></li>
   <li id="barcon1"></li>
   <li><a href="###" id="nextPage">Next page</a></li>
   <li><input type="text" id="num" size="2" oninput="value=value.replace(/[^\d]/g,'')"></li>
   <li><a href="###" id="jumpPage" onclick="jumpPage()">Jump</a></li>
  </ul>
 </div>
</div>
</body>
</html>

2. JS Logic

<script>
     // Initialize data function dynamicAddUser(num){
        for(var i=1;i<=num;i++)
        {
            var trNode = document.createElement("tr");
            $(trNode).attr("align","center");
            //Serial number var tdNodeNum = document.createElement("td");
            $(tdNodeNum).html(i+1);
            tdNodeNum.style.width = "150px";
            tdNodeNum.style.height = "33px";
            tdNodeNum.className = "td2";
            //User name var tdNodeName = document.createElement("td");
            $(tdNodeName).html("lzj"+i);
            tdNodeName.style.width = "300px";
            tdNodeName.className = "td2";
            //Permission var tdNodePri=document.createElement("td");
            tdNodePri.style.width = "250px";
            tdNodePri.className = "td2";
            var priText = document.createElement("span");
            $(priText).css({"display":"inline-block","text-align":"center"});
            $(priText).text("Ordinary user");
            tdNodePri.appendChild(priText);
            //Operation var tdNodeOper = document.createElement("td");
            tdNodeOper.style.width = "170px";
            tdNodeOper.className = "td2";
            var editA = document.createElement("a");
            $(editA).attr("href", "###").text("Edit");
            $(editA).css({ display: "inline-block" });
            tdNodeOper.appendChild(editA);
 
            trNode.appendChild(tdNodeNum);
            trNode.appendChild(tdNodeName);
            trNode.appendChild(tdNodePri);
            trNode.appendChild(tdNodeOper);
            $("#myTable")[0].appendChild(trNode);
        }
    }
    $(function(){
        dynamicAddUser(80);
        goPage(1,10);
    })
 
    /**
     * Paging function * pno--page number * psize--number of records displayed per page * The paging part starts from the real data row, so there is a constant to add or subtract to determine the real number of records * Pure js paging is actually loading all data rows, and the paging function is completed by displaying attributes**/
    var pageSize=10;//Number of rows displayed per page var currentPage_=1;//Current page global variable, used to determine whether it is on the same page when jumping, if so, do not jump, otherwise jump.
    var totalPage; //Total number of pages function goPage(pno,psize){
        var itable = document.getElementById("myTable");
        var num = itable.rows.length; //Number of all rows in the table (number of all records)
 
        pageSize = psize; //Number of rows per page //Total number of pages if (num/pageSize > parseInt(num/pageSize)) {
            totalPage=parseInt(num/pageSize)+1;
        }else{
            totalPage=parseInt(num/pageSize);
        }
        var currentPage = pno; //Current page number currentPage_=currentPage;
        var startRow = (currentPage - 1) * pageSize+1;
        var endRow = currentPage * pageSize;
        endRow = (endRow > num)? num : endRow;
 
        $("#myTable tr").hide();
        for(var i=startRow-1;i<endRow;i++) {
            $("#myTable tr").eq(i).show();
        }
 
        var tempStr = currentPage+"/"+totalPage;
        document.getElementById("barcon1").innerHTML = tempStr;
 
        if(currentPage>1){
            $("#firstPage").on("click",function(){
                goPage(1,psize);
            }).removeClass("ban");
            $("#prePage").on("click",function(){
                goPage(currentPage-1,psize);
            }).removeClass("ban");
        }else{
            $("#firstPage").off("click").addClass("ban");
            $("#prePage").off("click").addClass("ban");
        }
 
        if (currentPage < totalPage) {
            $("#nextPage").on("click",function(){
                goPage(currentPage+1,psize);
            }).removeClass("ban")
            $("#lastPage").on("click",function(){
                goPage(totalPage,psize);
            }).removeClass("ban")
        }else{
            $("#nextPage").off("click").addClass("ban");
            $("#lastPage").off("click").addClass("ban");
        }
    }
 
    function jumpPage() {
        var num = parseInt($("#num").val());
        if(num != currentPage_ && !isNaN(num) && num <= totalPage && num > 0) {
            goPage(num,pageSize);
        }
    }
</script>

The effect is as shown below:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Native JS to implement paging click control
  • js to achieve simple front-end paging effect
  • Pure javascript to achieve paging (two methods)
  • A very good JS paging effect code, worth studying
  • Using js to create html table paging example (js to implement paging)
  • Pure js paging code (simple and practical)
  • JSP paging display implementation code
  • js paging to display div contents
  • Simple paging example implemented by JS
  • Native JS to achieve cool paging effect

<<:  Tutorial on installing the latest MySQL 8.0.18 using a compressed package on Win10 64-bit (with pictures and text)

>>:  Detailed tutorial on installing php-fpm service/extension/configuration in docker

Recommend

37 Tips for a Good User Interface Design (with Pictures)

1. Try to use single column instead of multi-colum...

Markup language - CSS layout

Click here to return to the 123WORDPRESS.COM HTML ...

isPrototypeOf Function in JavaScript

Table of contents 1. isPrototypeOf() Example 1, O...

Idea configures tomcat to start a web project graphic tutorial

Configure tomcat 1. Click run configuration 2. Se...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Detailed explanation of how to restore database data through MySQL binary log

Website administrators often accidentally delete ...

JavaScript to achieve fancy carousel effect

This article shares two methods of implementing t...

Small paging design

Let our users choose whether to move forward or ba...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

How to simply encapsulate axios in vue

Inject axios into Vue import axios from 'axio...

How to create, start, and stop a Docker container

1. A container is an independently running applic...