经典 ASP 添加按 TD 列排序的功能
Classic ASP Adding sort function by TD column
根据我下面的陈述,我该如何根据电影名称和日期进行排序?基本上,用户只会单击 table header,要求:我不想在存储过程中执行,只需单击 header,这可能吗,如果是,如何我应该去做吗?
<html>
<head>
<meta http-equiv="refresh" content="10" />
<title>STATUS</title>
<style>
body {
font: 100% "Courier New", Courier, monospace;
}
table {
/* The default setting is border-collapse: separate;. By changing separate to collapse as shown below, the space between each table cell is removed. */
border-collapse: collapse;
}
caption {
font-size: .9125em;
font-weight: bold;
margin-bottom: .5em;
}
th,
td {
font-size: .875em;
padding: .5em .75em;
}
td {
border: 1px solid #000;
}
</style>
</head>
<body>
<p>
<%
'declare the variables
Dim Connection
Dim ConnString
Dim RS4
Dim SQL4
'define the connection string, specify database driver
ConnString="Driver={SQL Server};Server=10.0.1.21;Database=VISTAT;Uid=sa;Pwd=C@xxxxx1;"
'declare the SQL statement that will query the database
SQL4 = "SELECT top 8 (OrderTH_strMovieName) as Top8HotFilms,Convert(char(8), OrderTH_dtmSessionDateTime, 112) as DayOfCount,count( OrderTH_strMovieName)as filmoccurence FROM [VISTAIT].[dbo].[tblOrderTicketHistory] where Convert(char(8), OrderTH_dtmSessionDateTime, 112) >= (SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))) GROuP BY OrderTH_strMovieName , Convert(char(8), OrderTH_dtmSessionDateTime, 112)ORDER BY filmoccurence desc"
'create an instance of the ADO connection and RS1 objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set RS4 = Server.CreateObject("ADODB.RecordSet")
'Open the connection to the database
Connection.Open ConnString
'Open the rs object executing the SQL statement and return records
RS4.Open SQL4, Connection
'query fire up
If RS4.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Dim HTML4, pHTML4
Dim field14, field15, field16
Set field14 = RS4("Top8HotFilms")
Set field15 = RS4("DayOfCount")
Set field16 = RS4("filmoccurence")
HTML4 = "<BR><BR><table CellPadding=0 CellSpacing=0 border=1><caption>Top 8 Movies booking for today</caption><TR><TD>Movies</td><TD>Day</td><TD>Occurence</td></TR>"&vbCrLf
Do While Not RS4.EOF
pHTML4 = "<TR>"
pHTML4 = pHTML4 & "<TD> " & field14 & "</TD>"
pHTML4 = pHTML4 & "<TD> " & field15 & "</TD>"
pHTML4 = pHTML4 & "<TD> " & field16 & "</TD>"
pHTML4 = pHTML4 & "</TR>" & vbCrLf
HTML4 = HTML4 & pHTML4
RS4.MoveNext
Loop
HTML4 = HTML4 & "</table><br><br>" & vbCrLf
Response.Write HTML4
END IF 'query end
'close the connection and rs objects to free up resources
RS4.Close
Set RS4=nothing
Connection.Close
Set Connection=nothing
%>
</body>
</html>
纯粹在经典 asp 中执行此操作的方法是将 sql 查询的末尾从 ORDER BY filmoccurence desc
修改为 ORDER BY " & Request.Querystring("sortparameter") & " desc
,然后将您的 table column headers 转化为可以将相关字段名称传递给数据库查询的链接 eg
<td><a href="thispage.asp?sortparameter=DayOfCount">Day</a></td>
有一些方法可以使用 javascript 对 table 的客户端进行排序 - 我将在问题中添加一个 javascript 标签,因为我认为这可能会提供更令人满意的结果解决方案,SO 上的 JS 人比 Classic ASP 用户
多得多
编辑
上述方法假定查询字符串值已传递到页面。为了让它在没有一个的情况下工作,你需要稍微调整一下 - 例如
dim sortparameter
If Request.Querystring("sortparameter") = "" Then
sortparameter = "filmoccurence"
Else
sortparameter = Request.Querystring("sortparameter")
End If
SQL4 = "SELECT top 8 (OrderTH_strMovieName) as Top8HotFilms,Convert(char(8), OrderTH_dtmSessionDateTime, 112) as DayOfCount,count( OrderTH_strMovieName)as filmoccurence FROM [VISTAIT].[dbo].[tblOrderTicketHistory] where Convert(char(8), OrderTH_dtmSessionDateTime, 112) >= (SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))) GROuP BY OrderTH_strMovieName , Convert(char(8), OrderTH_dtmSessionDateTime, 112)ORDER BY " & sortparameter & " desc"
在生产环境中,我强烈建议您为查询字符串输入的值添加范围保护,否则您将面临 SQL 注入攻击。
正如我之前建议的那样,我认为 javascript 会提供一种更令人满意的方式来做您想做的事情 - 它会在所有客户端完成,因此无需每次都重新加载页面想要 re-order 你的专栏。
根据我下面的陈述,我该如何根据电影名称和日期进行排序?基本上,用户只会单击 table header,要求:我不想在存储过程中执行,只需单击 header,这可能吗,如果是,如何我应该去做吗?
<html>
<head>
<meta http-equiv="refresh" content="10" />
<title>STATUS</title>
<style>
body {
font: 100% "Courier New", Courier, monospace;
}
table {
/* The default setting is border-collapse: separate;. By changing separate to collapse as shown below, the space between each table cell is removed. */
border-collapse: collapse;
}
caption {
font-size: .9125em;
font-weight: bold;
margin-bottom: .5em;
}
th,
td {
font-size: .875em;
padding: .5em .75em;
}
td {
border: 1px solid #000;
}
</style>
</head>
<body>
<p>
<%
'declare the variables
Dim Connection
Dim ConnString
Dim RS4
Dim SQL4
'define the connection string, specify database driver
ConnString="Driver={SQL Server};Server=10.0.1.21;Database=VISTAT;Uid=sa;Pwd=C@xxxxx1;"
'declare the SQL statement that will query the database
SQL4 = "SELECT top 8 (OrderTH_strMovieName) as Top8HotFilms,Convert(char(8), OrderTH_dtmSessionDateTime, 112) as DayOfCount,count( OrderTH_strMovieName)as filmoccurence FROM [VISTAIT].[dbo].[tblOrderTicketHistory] where Convert(char(8), OrderTH_dtmSessionDateTime, 112) >= (SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))) GROuP BY OrderTH_strMovieName , Convert(char(8), OrderTH_dtmSessionDateTime, 112)ORDER BY filmoccurence desc"
'create an instance of the ADO connection and RS1 objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set RS4 = Server.CreateObject("ADODB.RecordSet")
'Open the connection to the database
Connection.Open ConnString
'Open the rs object executing the SQL statement and return records
RS4.Open SQL4, Connection
'query fire up
If RS4.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Dim HTML4, pHTML4
Dim field14, field15, field16
Set field14 = RS4("Top8HotFilms")
Set field15 = RS4("DayOfCount")
Set field16 = RS4("filmoccurence")
HTML4 = "<BR><BR><table CellPadding=0 CellSpacing=0 border=1><caption>Top 8 Movies booking for today</caption><TR><TD>Movies</td><TD>Day</td><TD>Occurence</td></TR>"&vbCrLf
Do While Not RS4.EOF
pHTML4 = "<TR>"
pHTML4 = pHTML4 & "<TD> " & field14 & "</TD>"
pHTML4 = pHTML4 & "<TD> " & field15 & "</TD>"
pHTML4 = pHTML4 & "<TD> " & field16 & "</TD>"
pHTML4 = pHTML4 & "</TR>" & vbCrLf
HTML4 = HTML4 & pHTML4
RS4.MoveNext
Loop
HTML4 = HTML4 & "</table><br><br>" & vbCrLf
Response.Write HTML4
END IF 'query end
'close the connection and rs objects to free up resources
RS4.Close
Set RS4=nothing
Connection.Close
Set Connection=nothing
%>
</body>
</html>
纯粹在经典 asp 中执行此操作的方法是将 sql 查询的末尾从 ORDER BY filmoccurence desc
修改为 ORDER BY " & Request.Querystring("sortparameter") & " desc
,然后将您的 table column headers 转化为可以将相关字段名称传递给数据库查询的链接 eg
<td><a href="thispage.asp?sortparameter=DayOfCount">Day</a></td>
有一些方法可以使用 javascript 对 table 的客户端进行排序 - 我将在问题中添加一个 javascript 标签,因为我认为这可能会提供更令人满意的结果解决方案,SO 上的 JS 人比 Classic ASP 用户
多得多编辑
上述方法假定查询字符串值已传递到页面。为了让它在没有一个的情况下工作,你需要稍微调整一下 - 例如
dim sortparameter
If Request.Querystring("sortparameter") = "" Then
sortparameter = "filmoccurence"
Else
sortparameter = Request.Querystring("sortparameter")
End If
SQL4 = "SELECT top 8 (OrderTH_strMovieName) as Top8HotFilms,Convert(char(8), OrderTH_dtmSessionDateTime, 112) as DayOfCount,count( OrderTH_strMovieName)as filmoccurence FROM [VISTAIT].[dbo].[tblOrderTicketHistory] where Convert(char(8), OrderTH_dtmSessionDateTime, 112) >= (SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))) GROuP BY OrderTH_strMovieName , Convert(char(8), OrderTH_dtmSessionDateTime, 112)ORDER BY " & sortparameter & " desc"
在生产环境中,我强烈建议您为查询字符串输入的值添加范围保护,否则您将面临 SQL 注入攻击。
正如我之前建议的那样,我认为 javascript 会提供一种更令人满意的方式来做您想做的事情 - 它会在所有客户端完成,因此无需每次都重新加载页面想要 re-order 你的专栏。