|
|
Sortable Tables
At HTMLez.com I shared information
that I had learned at http://builder.cnet.com/webbuilding/pages/Programming/Scripter/080999/?tag=st.bl.7264cd3.plbl
about creating sortable tables on a web page. This javascript file you can
obtain from their site.
Here I will show how to make this sortable tables become generically useful on any asp enabled
web server. This example, available for
downloading in .txt format (simply
save as a .asp file as required on your server). Click here to see a sample showing .asp Sortable Tables.
We will automate by enabling this one asp file to process any .csv (comma delimited file). These
.csv files can easily be created in Excel and exported as a comma delimited file. Make the first row the Headers
for the table, being sure there is one header for each column in the table. Data fields in some columns may be
blank but be sure each column has a header value in the A row.
This asp generic program functions as follows:
- Define the values to be received from the invoking link:
Dim tableName
tableName = request("fname")
Dim tableWidth
tableWidth = request("twidth")
Dim tableAlign
tableAlign = request("talign")
Dim borderSize
borderSize = request("bsize")
Dim tableNumerics
tableNumerics = request("tnums")
- Then create javascript code from asp and start the data array.
response.write "<scr" & "ipt language=""javascript"">" & vbCrLf
response.write "var data = new Array(" & vbCrLf
- Open the .csv file and setup some values
Set fileFSO = CreateObject("Scripting.FileSystemObject")
dim strPathInfo, strPhysicalPath
strPathInfo = Request.ServerVariables("PATH_INFO")
strPhysicalPath = Server.MapPath(strPathInfo)
Dim objFSO, objFile, objFileItem, objFolder, objFolderContents
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.GetFile(strPhysicalPath)
set objFolder = objFile.ParentFolder
set objFolderContents = objFolder.Files
fName = objFolder & "\VIS_PRINTERS.csv"
fName = objFolder & "\" & tableName
Set fileCurFile = fileFSO.OpenTextFile(fName,1)
lineCurFile = fileCurFile.ReadLine 'future
needTitles = true
Dim Titles
dim rows
rows = 0
cols = 0
continue = false
if fileCurFile.AtEndOfStream = false then continue = true
- Start the loop for reading lines in the file.
do until continue = false
if fileCurFile.AtEndOfStream = true then continue = false
- On the first pass, read this line, counting the commas
if needTitles then
start = 1
pos = Instr(start,lineCurFile,",")
do until pos = 0
cols = cols + 1
start = pos + 1
pos = Instr(start,lineCurFile,",")
loop
cols = cols + 1
End if
- Start the row count and initialize the output line for javascript, and create the javascript array values.
rows = rows + 1
if Right(lineCurFile,1) <> "," then lineCurFile = lineCurFile & ","
theLine = ""
spos = 1
epos = Instr(1,lineCurFile,",")
'do while epos > 0
'for i = 1 to 5
for i = 1 to cols
if epos > spos then
temp = Mid(lineCurFile,spos,(epos - spos))
temp = Server.HTMLEncode(temp)
theLine = theLine & """" & temp & ""","
else
theLine = theLine & """"","
end if
spos = epos + 1
epos = Instr(spos,lineCurFile,",")
next
- Remove the trailing comma so that javascript won't fail, read the next line of the .csv file, and set the conditional
if Len(theLine) > 0 then theLine = Left(theLine,(Len(theLine) - 1))
if continue then lineCurFile = fileCurFile.ReadLine
- If processing the first line, setup the Titles. Otherwise, output the javascript array statement. Close the file.
if needTitles then
needTitles = false
Titles = theLine
else
'if fileCurFile.AtEndOfStream = True then
if continue = false then
response.write "new Array(" & theLine & ")" & vbCrLf
else
response.write "new Array(" & theLine & ")," & vbCrLf
end if
end if
loop
fileCurFile.close
This produces a file such as this:
var data = new Array(
new Array("George","Wade","676 781-5181","5"),
new Array("Rebecca","Wade","676 781-5181","9"),
new Array("Christia","Lewis","676 375-3775","21"),
new Array("Kalamazoo","SDA School","342-8983"),
new Array("Matthew","lewis","676 375-3991","15")
)
var buttons = new Array("First Name","Last Name","Phone Number"," Age")
where the first javascript array is the data array and the 2nd array is the Header/Button array
We allow javascript to create the buttons and the data table with the following assistance:
var buttons = new Array(<% =Titles %>)
var table = new SortableTable("<% =tableName %>",<% =rows %>,<% =cols %>,<% =borderSize %>,"<% =tableWidth %>%","<% =tableAlign %>") /*id,rows,columns,border,width,align*/
id,rows,columns,border,width,align are parms for dynamically defining the table.
And we are almost finished with our customization to the Sortable Tables. We still need to setup the
definitions for sorting a column numerically (since sortable tables sorts alphabetically by default).
We do this with the following asp code:
<%
Dim tableNumerics
Dim c
tableNumerics = request("tnums")
for i = 1 to Len(v) step 2
c = CInt(mid(v,i,2))
if c > 0 then
response.write "table.setNumeric(" & (c - 1) & ")" & vbCrLf
end if
next
%>
And our customization is complete.
Oh, you want to know how to call this generical Sortable Tables page? Create this link:
<a href="genSortTables.asp?fname=phonelist.csv&twidth=90&talign=right&bsize=1">MyLink</a>
or for numeric columns
<a href="genSortTables.asp?fname=phonelist.csv&twidth=90&talign=right&bsize=1&tnums=00000004">MyLink</a>
The &tnums=00000004 parameter contains 00 for each column to be sorted alphabetically and 04 for the
fourth column to be sorted numerically.
Enjoy using sortable Tables in the future. Just download this example as instructed above
and use this code without changing anything within the script tags. Just link to the page using the parms shown.
|
|