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

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

Theory Popularization——User Experience

1. Concept Analysis 1: UE User Experience <br ...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

UDP simple server client code example

I won’t go into details about the theory of UDP. ...

MySQL calculates the number of days, months, and years between two dates

The MySQL built-in date function TIMESTAMPDIFF ca...

Example code of CSS responsive layout system

Responsive layout systems are already very common...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

Simple operation of installing vi command in docker container

When using a docker container, sometimes vim is n...

Analysis of Linux Zabbix custom monitoring and alarm implementation process

Target Display one of the data in the iostat comm...

How to count the number of specific characters in a file in Linux

Counting the number of a string in a file is actu...

Solution to the problem of data loss when using Replace operation in MySQL

Preface The company's developers used the rep...

Five ways to implement inheritance in js

Borrowing Constructors The basic idea of ​​this t...

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

Detailed explanation of nginx optimization in high concurrency scenarios

In daily operation and maintenance work, nginx se...