|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dynamic sorting header
I am trying to incorporate dynamic header with reverse sorting from this ASP 101 example sort script:
Code:
<!-- #include file="adovbs.inc" -->
<%
' ADO constants included above. Questions about adovbs.inc?
' See "What is Adovbs.inc and Why Do I Need It?"
' (URL address blocked: See forum rules)
Dim strConnString ' SQL Server connection string
Dim cnnDbSort ' ADO Connection object
Dim rstDbSort ' ADO Recordset object
Dim strSqlQuery ' Our SQL query
Dim strSortField ' Field to sort by
Dim strSortOrder ' "ASC" or "DESC"
Dim objField ' Used for table display
Dim blnColor ' Alternating color indicator
' Set our connection string
strConnString = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;"
' Retrieve sorting parameters:
' Get the field name and make sure the input is one of our field names.
strSortField = Request.QueryString("field")
Select Case LCase(strSortField)
Case "last_name", "first_name", "sales"
strSortField = strSortField
Case Else
strSortField = "id"
End Select
' Check for descending o/w we default to ascending
Select Case LCase(Request.QueryString("order"))
Case "desc"
strSortOrder = "desc"
Case Else
strSortOrder = "asc"
End Select
' Build our SQL query
strSqlQuery = "SELECT * FROM [sample] ORDER BY [" & strSortField & "] " & strSortOrder & ";"
' Open connection
Set cnnDbSort = Server.CreateObject("ADODB.Connection")
cnnDbSort.Open strConnString
' Get recordset
Set rstDbSort = Server.CreateObject("ADODB.Recordset")
rstDbSort.Open strSqlQuery, cnnDbSort
' Build our table:
' Start the table
Response.Write "<table border=""1"" cellspacing=""0"">" & vbCrLf
' Write titles and include links to sort the table by each field
Response.Write vbTab & "<tr>" & vbCrLf
For Each objField in rstDbSort.Fields
Response.Write vbTab & vbTab & "<td bgcolor=""#CCCCCC""><strong>"
If objField.Name = strSortField And strSortOrder = "asc" Then
Response.Write "<a href=""?field=" & objField.Name & "&order=desc"">" & objField.Name & "</a>"
Else
Response.Write "<a href=""?field=" & objField.Name & "&order=asc"">" & objField.Name & "</a>"
End If
Response.Write "</strong>"
If objField.Name = strSortField Then
If LCase(strSortOrder) = "asc" Then
Response.Write " <img src=""images/arr_up.gif"" width=""9"" height=""7"" border=""0"" alt=""Up Arrow"">"
Response.Write " ↑ "
Else
Response.Write " <img src=""images/arr_dn.gif"" width=""9"" height=""7"" border=""0"" alt=""Down Arrow"">"
Response.Write " ↓ "
End If
End If
Response.Write "</td>" & vbCrLf
Next 'objField
Response.Write vbTab & "</tr>" & vbCrLf
' Display the data
blnColor = False
rstDbSort.MoveFirst
Do While Not rstDbSort.EOF
'Response.Write rstDbSort.Fields(0).Value & "<br />" & vbCrLf
Response.Write vbTab & "<tr>" & vbCrLf
For Each objField in rstDbSort.Fields
Response.Write vbTab & vbTab & "<td bgcolor="""
' Decide what color to output
If blnColor Then
Response.Write "#CCCCFF" ' Light blueish
Else
Response.Write "#FFFFFF" ' White
End If
Response.Write """>" & Trim(objField.Value) & "</td>" & vbCrLf
Next 'objField
Response.Write vbTab & "</tr>" & vbCrLf
' Toggle our colors
blnColor = Not blnColor
rstDbSort.MoveNext
Loop
' End the table
Response.Write "</table>" & vbCrLf
' Close data access objects and free variables
rstDbSort.Close
Set rstDbSort = Nothing
cnnDbSort.Close
Set cnnDbSort = Nothing
%>
Anyone know how I can choose to not show some of the columns, right now it is returning all the fields in the sql select statement? example like id and sales but yet can select those fields in my sql query? Also instead of using the exact field name, can I replace those names with something more formatted? like Last Name for the last_name field. Help would be highly appreciated. Thank you! id ↑ last_name first_name sales 1 Fuller Andrew 5000 2 Leverling Janet 6250 3 Buchanan Steven 3245 4 Peacock Margaret 4685 5 King Robert 5290 6 Andersen David 6420 7 Edwards Frank 3840 8 Richards Kate 4260 9 Jones Edward 3450 10 Callahan Laura 3675 11 Miller Greg 5640 12 Steel Tammy 3570 13 Dodsworth Nancy 2750 14 Nelson Beth 4345 15 Hopkins Peter 6710 16 Graham Chris 4050 17 Thompson Vincent 4820 18 Ostrander Ian 3965 19 Quinn Howard 5865 20 Smith Fred 6370 21 Ingraham Owen 4750 22 Youngs Quincy 4420 23 Zimmerman Walter 4980 24 Valentino Linda 2965 25 Underwood Debra 3790 26 Xavier Simon 5580 27 White Mark 4500 |
![]() |
| Viewing: ASP Free Forums > Programming > ASP Development > Dynamic sorting header |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|