经典ASP通过TD列添加排序功能

时间:2015-05-31 03:22:39

标签: javascript asp-classic

根据我在下面的陈述,我如何根据电影名称和日期进行排序?基本上用户只需单击表头,要求:我不想在存储过程中执行,只需单击该标题,这是否可行,如果是,我应该如何处理它?<​​/ p>

<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>

1 个答案:

答案 0 :(得分:0)

纯粹在经典asp中执行此操作的方法是将sql查询的结尾从ORDER BY filmoccurence desc修改为ORDER BY " & Request.Querystring("sortparameter") & " desc,然后将表列标题转换为可以传递相关字段的链接数据库查询的名称,例如

<td><a href="thispage.asp?sortparameter=DayOfCount">Day</a></td>

有一些方法可以使用javascript对表客户端进行排序 - 我将在问题中添加一个javascript标记,因为我认为这可能会提供一个更令人满意的解决方案,并且有更多的JS人员比经典ASP更多用户

修改

上述方法假定已将querystring值传递给页面。为了使其无需工作,您需要稍微调整一下 - 例如

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会提供一种更令人满意的方式来做你想做的事情 - 它会在客户端做到这一点所以每次你想重新订购时都不需要重新加载页面列。