This time I will bring you AJAX to implement non-refresh data paging, and what are the precautions for AJAX to implement non-refresh data paging. The following is a practical case, let's take a look.
I have used the GridView control before when using Asp.Net. This control has its own paging function. Although it is ugly, it is still very powerful. Here, I will show you a more powerful way - using AJAX to directly get data from the server for paging without refreshing.1. Implementation process
Note: The following contents are all used within the server. First, create several files in the server path, for example, page1.txt, page2.txt, page3.txt. Put an array into each file, as follows:[{user:'blue',pass:'123'},{user:'aaa',pass:'dsfaa'},{user:'Ares',pass:'12346'}]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AJAX实现分页、</title> <script src="ajax.js"></script> <script> window.onload=function () { var oUl=document.getElementById('ul1'); var aBtn=document.getElementsByTagName('a'); var i=0; for(i=0;i<aBtn.length;i++) { //给每一个按钮附一个初值索引 aBtn[i].index=i; aBtn[i].onclick=function () { //调用AJAX函数 ajax('page'+(this.index+1)+'.txt', function (str){ //获得其中的数据 var aData=eval(str); oUl.innerHTML=''; for(i=0;i<aData.length;i++) { var oLi=document.createElement('li'); oLi.innerHTML='<strong>'+aData[i].user+'</strong><i>'+aData[i].pass+'</i>'; oUl.appendChild(oLi); } }); }; } }; </script> </head> <body> <ul id="ul1"> </ul> <a href="javascript:;">1</a> <a href="javascript:;">2</a> <a href="javascript:;">3</a> </body> </html>
/* AJAX封装函数 url:系统要读取文件的地址 fnSucc:一个函数,文件取过来,加载完会调用 */ function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.XMLHttpRequest) { oAjax=new XMLHttpRequest(); } else { oAjax=new ActiveXObject("Microsoft.XMLHTTP"); } //2.连接服务器 oAjax.open('GET', url, true); //3.发送请求 oAjax.send(); //4.接收服务器的返回 oAjax.onreadystatechange=function () { if(oAjax.readyState==4) //完成 { if(oAjax.status==200) //成功 { fnSucc(oAjax.responseText); } else { if(fnFaild) fnFaild(oAjax.status); } } }; }
Figure 2 Display effect
2. Summary
AJAX still requires more attempts and more practice. Pagination is a good idea, it can improve the speed of access, it is a good method, and it will bring better benefits to everyone in the future. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:How to avoid being intercepted by the browser when the ajax callback opens a new form
Discussion on Ajax and research
The above is the detailed content of AJAX implements data paging without refreshing. For more information, please follow other related articles on the PHP Chinese website!