代码是从网上来的,有些地方改了。实现了根据用户信息的不固定搜索~
list.htm
<html>
<head>
<title></title>
<script language="javascript">...
function Ajax()
...{
var xhrObj=null;
if(window.XMLHttpRequest)
...{
xhrObj=new XMLHttpRequest();
}
else if(window.ActiveXObject)
...{
try...{
xhrObj=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e1)
...{
try...{
xhrObj=new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e2)...{
try...{
xhrObj=new ActiveXObject("MSXML3.XMLHTTP");
}
catch(e3)...{
alert("创建Ajax失败:"+e3)
}
}
}
}
else
...{
alert("未能识别的浏览器");
}
return xhrObj;
}
function $(id)
...{
return document.all[id];
}
</script>
<script language="javascript" defer>...
var xhr=new Ajax();
var page=0;
var icount=0; //当前页数
var pagecount; //总页数
var count=0;
var ipagecount=0;
var ipagesize=10;; //每页显示记录数
var pp=0;
var ipage=0;
var Dom=null;
function getList(curPage)
...{
if(ipagecount!=0)
...{
ipage=parseInt((curPage-1)/ipagecount)+1;
}else...{
ipage=1
};
page=curPage;
if (pp!=ipage)
...{;
Dom=null;
pp=ipage;
xhr.open("get","data.asp?curpage="+pp,true);
xhr.onreadystatechange=$CallBack;
xhr.send(null);
}
else
...{
$showList();
}
}
function $CallBack()
...{
if(xhr.readystate==1)
...{
$("newsList").innerHTML="正在连结服务器";
}
else if(xhr.readystate==2 || xhr.readystate==3)
...{
$("newsList").innerHTML="正在读取数据";
}
else if(xhr.readystate==4)
...{
if (xhr.status==200)
...{
Dom=xhr.responseXML;
$showList();
}
}
}

function $showList()
...{
var html="";
var items=Dom.selectNodes("//item");
icount=Dom.selectNodes("//item").length; //数量
count=Dom.selectSingleNode("//data").getAttribute("count"); //总数量
ipagecount=parseInt((icount-1)/ipagesize)+1;
pagecount=parseInt((count-1)/ipagesize)+1;
startPosition=(page-1)*ipagesize;
endPosition=(page*ipagesize)
if(endPosition>count)endPosition=count
for(var i=startPosition;i<endPosition;i++)
...{
html+="<div id="title"><a href=""+items[i].selectSingleNode("htmlurl").text+"">"+items[i].selectSingleNode("hits").text+"</a></div>";
}
$("newsList").innerHTML=html;

var statushtml="<a href="javascript:previousPage()">上一页</a>|<a href="javascript:nextPage()">下一页</a>";
if(pagecount<=10)
...{
for(var i=1;i<=pagecount;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
else if(page+10<pagecount && pagecount>10)
...{
if(page%10==0)
...{
for(var i=page-9;i<=page+1;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
else if(page % 10 ==1 && page!=1)
...{
for(var i=page-1;i<=page+10;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
else if(page<=11)
...{
for(var i=1;i<=11;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
else
...{
for(var i=(page-(page % 10));i<=page+(10-(page % 10))+1;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
}
else if(page+10>=pagecount && pagecount>10)
...{
if(page%10==0)
...{
for(var i=page-9;i<=page+1;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
else if(page % 10 ==1 && page!=1)
...{
for(var i=page-1;i<=page+9;i++)
...{
if (i==page)
...{
statushtml+="<b>"+i+"</b> "
}
else
...{
statushtml+="<a href="javascript:turnPage("+i+")">"+i+"</a> "
}
}
}
}
document.getElementById("pagestatus").innerHTML=statushtml;
}
function previousPage()
...{
if (page>1)
...{
getList(page-1);
}
else
...{
alert("已经是第一页了");
}
}
function nextPage()
...{
if(page<ipagecount)
...{
getList(page+1);
}
else
...{
alert("已经到最后一页了");
}
}
function turnPage(p)
...{
getList(p);
}
getList(1);
</script>
</head>
<body>
<p><BR>
</p>
<TABLE width="100%" align=center border=0>
<TBODY>
<TR>
<TD align=middle> </TD>
<TD><FONT color=red size=4>国际财经</FONT></TD>
<TD></TD>
</TR>
<TR>
<TD colspan="2"> </TD>
<TD align=right> </TD>
</TR>
</TBODY>
</TABLE>
<div id="newsList"></div>
<div id="pagestatus"></div>
</body>
</html>
t.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim ntype
ntype=request("ntype")
session("ntype")=ntype
response.Redirect("list.htm")
%>data.asp
<%@ Language="VBSCRIPT" codepage="936" %>
<%
response.cachecontrol="no-cache"
response.addHeader "pragma","no-cache"
response.expires=-1
response.expiresAbsolute=now-1
response.contentType="text/xml"
public conn
dim connstr
dim ntype
ntype2=session("ntype")

connstr="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security news=False;Initial Catalog=news;Data Source=DAVID"
set conn=server.createobject("ADODB.CONNECTION")
conn.open connstr


%>
<?xml version="1.0" encoding="gb2312" ?>
<%
dim iPagesize,rs,sSQL,iCurPage,ipresize,icount,ipagecount



icurpage=trim(request.QueryString("curpage"))
iPagesize=500 '页大小
set rs=server.createObject("adodb.recordset")
'rs.open "select count(id) from ca_news where status=1",conn,1,1
rs.open "select count(id) from news where ntype='"&ntype2&"' ",conn,1,1
icount=rs(0) '总记录数
rs.close
ipagecount=int((icount-1)/iPagesize)+1 '总页数
if icurpage="" then
icurpage=1
else
icurpage=cint(icurpage)
end if
if icurpage<1 then
icurpage=1
end if
if icurPage>ipagecount then icurpage=ipagecount
ipreSize=(iCurPage-1)*ipagesize
if ipresize=0 then
sSQL="select * from news where ntype='"&ntype2&"' order by id desc"
else
' sSQL="select top "&ipagesize&" id,title,postdate,hits,htmlurl from news where status=1 and id not in (select top "&ipresize&" id from news where status=1 order by id asc) order by id asc"
sSQL="select top "&ipagesize&" * from news id not in (select top "&ipresize&" id from news order by id desc) order by id desc"
end if
rs.open sSQL,conn,1,1
%>
<data count="<%=icount%>" pagesize="<%=ipagesize %>" pagecount="<%=ipagecount%>">
<%
while not rs.eof
%>
<item id="<%=rs("id")%>">
<title><![CDATA[<%=rs("id")%>]]></title>
<htmlurl> http://www.romaway.com/Finance/BShowNews.asp?ID=<%=rs("id")%></htmlurl>
<postdate><%=rs("rwdate") %></postdate>
<hits><%=trim(rs("title"))+left (trim(rs("rwdate")),10 )%></hits>
</item>
<%
rs.movenext
wend
%>
</data>
<%
rs.close
set rs=nothing
conn.close
set conn=nothing

%>

本文介绍了一种使用Ajax进行动态分页查询的技术实现方案。通过JavaScript与XMLHttpRequest对象完成前后端交互,实现实时更新页面内容而无需刷新整个页面。文章详细展示了如何构造请求、处理响应及动态展示数据的过程。
155

被折叠的 条评论
为什么被折叠?



