<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'用于前台操作数据表排序字段的研究
'作者:周培公
'数据库:d.mdb
'数据表:t
'字段:ID,name,orderID
dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("d.mdb")
If Request.form("a")<>"" Then
dim arrTest
arrTest=Request.form("a")
response.Write(arrTest)
dim arrT
arrT=split(arrTest,"|")
dim j,arT1,arT2
arT1=split(arrT(0),",")'第一个数是ID,第二个数是orderID
arT2=split(arrT(1),",")
conn.exeCute("update t set orderID="&arT1(1)&" where id="&arT1(0))
conn.exeCute("update t set orderID="&arT2(1)&" where id="&arT2(0))
response.Redirect("index.asp")
else
dim sql,rs
sql="select * from t order by orderID"
Set rs= Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,3
if not rs.bof and not rs.eof then
%>
<!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=gb2312" />
<title>前台排序研究</title>
</head>
<body>
<%
dim intCounter
intCounter=0
dim lastCountIndex
lastCountIndex=rs.recordcount-1
do while not rs.eof
%>
<%= rs("id") %>、<%= rs("name") %>---<%= rs("orderID") %>
[
<%
If intCounter<>0 Then
%>
<a href="#" onclick="upOder(<%= intCounter %>);return false;">升</a>
<%
End If
If intCounter<>lastCountIndex Then
%>
<a href="#" onclick="downOder(<%= intCounter %>);return false;">降</a>
<%
End If
%>
]<br />
<%
intCounter=intCounter+1
rs.movenext
loop
%>
<form id="f" name="f" method="post" action="">
<input name="a" type="hidden" value="" />
</form>
<script language="javascript">
<!--
var t_ID;
var t_orderID;
t_ID=new Array();
t_orderID=new Array();
<%
dim t,i
rs.movefirst
dim f(1)
f(0)="ID"
f(1)="orderID"
'getrows方法,
'第一个参数是所取得记录的数量(默认-1指全部),第二个参数是开始处的标签,第二个是字段
'结果数组的第一维是取得的字段数量,第二维是取得的记录数量
t=rs.getrows(-1,1,f)
for i=0 to UBOUND(t,2)
%>
t_ID[<%= i %>]=<%= t(0,i) %>;
t_orderID[<%= i %>]=<%= t(1,i) %>;
<%
next
%>
function upOder(i)
{
document.all.a.value=t_ID[i];
document.all.a.value+=","+t_orderID[i-1];
document.all.a.value+="|"+t_ID[i-1];
document.all.a.value+=","+t_orderID[i];
document.all.f.submit();
}
function downOder(i)
{
document.all.a.value=t_ID[i];
document.all.a.value+=","+t_orderID[i+1];
document.all.a.value+="|"+t_ID[i+1];
document.all.a.value+=","+t_orderID[i];
document.all.f.submit();
}
//-->
</script>
</body>
</html>
<%
end if
End If
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
本文介绍了一种利用VBScript和JavaScript实现的前端数据表排序方法。通过用户界面操作,可以轻松调整数据库中记录的顺序。具体实现了记录上移和下移功能,并即时更新数据库中的排序字段。
1507

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



