创建一个分页类
要记住,这里举例的整个应用程序只包括一个ASP页面。当这个ASP页面被访问时,它将创建执行记录分页需要的所有客户端JavaScript代码。为简化这个过程,我创建了一个VBScript类来处理这个功能。使用这个类的时候,开发人员只需将他希望在访问者的Web浏览器上进行分页的记录集传递进来即可。关于类的使用,请阅读在VBScript中使用类。
我将这个类命名为dhtmlGetRows,它包含两个属性和一个方法。两个属性是:
1、 RecsPerPage:确定每一页显示多少个记录。
2、 THString:通过一个HTML表格显示这个进行了分页的结果;这个属性允许你为表格的标题指定一个字串。
单一的方法是GenerateHTML(RecordsetObject),它为分页应用程序返回完整的HTML:客户端JavaScript代码和需要的DIV 标记。这个方法只需要一个参数RecordsetObject,它应该是一个记录集对象,其中填充了你希望在一个分页格式中显示的数据库数据。
这个类的代码相当长,并且大部分代码都只是返回客户端的JavaScript代码。下面是类的代码:
<%
Class dhtmlGetRows
'******* PRIVATE MEMBER VARIABLES **********
Private iRecsPerPage
Private strTHString
'*******************************************
'************ Initialize Event *************
Private Sub Class_Initialize()
iRecsPerPage = 10 'assign a default value
End Sub
'*******************************************
'************ PROPERTY LET/GET *************
Public Property Let THString(strValue)
'Replace all apostrophes with \'
strTHString = Replace(strValue, "'", "\'")
End Property
Public Property Get THString()
THString = strTHString
End Property
Public Property Let RecsPerPage(iValue)
If iValue > 0 and IsNumeric(iValue) then
iRecsPerPage = CInt(iValue)
End If
End Property
Public Property Get RecsPerPage()
RecsPerPage = iRecsPerPage
End Property
'*******************************************
'**************** METHODS ******************
Public Function GenerateHTML(objRS)
'Begin by getting an array of the data
Dim aValues
aValues = objRS.GetRows()
'Find the value of rows and columns
Dim iCols, iRows
iCols = UBound(aValues, 1)
iRows = UBound(aValues, 2)
Dim strOutput
'Display the initial script block
strOutput = "<script language=""javascript"">" & vbCrLf & _
"var tableRow = new Array(" & iRows & ");" & vbCrLf & vbCrLf
Dim iLoop, iColLoop, strTmp
For iLoop = 0 to iRows
strOutput = strOutput & "tableRow[" & iLoop & "] = '<tr>"
For iColLoop = 0 to iCols
'Fix apostrophes
strTmp = Replace(aValues(iColLoop, iLoop),"'", "\'")
'Remove carraige returns
strTmp = Replace(strTmp, vbCrLf, "")
strOutput = strOutput & "<td>" & strTmp & "</td>"
Next 'iColLoop
strOutput = strOutput & "</tr>';" & vbCrLf
Next 'iLoop
'Init global varaibles and find out what browser the user is using
strOutput = strOutput & vbCrLf & vbCrLf & "var first = 0;" & vbCrLf & _
"var last = " & iRecsPerPage & ";" & vbCrLf & _
"var mynav;" & vbCrLf & "if (navigator.appName == ""Netscape"")" & _
vbCrLf & vbTab & "mynav = ""NS"";" & vbCrLf & _
"if (navigator.appName == ""Microsoft Internet Explorer"")" & _
vbCrLf & vbTab & "mynav = ""IE"";" & vbCrLf & _
vbCrLf & "</script>" & vbCrLf & vbCrLf
'Now display the HTML table
strOutput = strOutput & vbCrLf & "<div id=""grid""> </div>" & vbCrLf & _
vbCrLf & vbCrLf & "<script language=""javascript"">" & vbCrLf
'Write the nav function
strOutput = strOutput & "function nav(iVal) {" & vbCrLf & _
"// do we want to move forward or backwards?" & vbCrLf & _
"if (iVal == 1) { " & vbCrLf & vbTab & "first += " & _
iRecsPerPage & ";" & vbCrLf & "last += " & iRecsPerPage & _
vbCrLf & "}" & vbCrLf & "else if (iVal == -1) { " & vbCrLf & vbTab & _
"first -= " & iRecsPerPage & ";" & vbCrLf & vbTab & "last -= " & _
iRecsPerPage & ";" & vbCrLf & "}" & vbCrLf & _
vbCrLf & vbCrLf & "var txt = '';" & vbCrLf & _
"txt += '<table border=""1"">';" & vbCrLf
'Do we need to add a TH string?
If Len(strTHString) > 0 then
strOutput = strOutput & "txt += '<tr>" & strTHString & "</tr>';" & vbCrLf
End If
strOutput = strOutput & "for (var iLoop = first; iLoop < last; iLoop++)" & vbCrLf & _
vbTab & "if (iLoop <= " & iRows & ") txt += tableRow[iLoop];" & vbCrLf & _
"txt += '</table>';" & vbCrLf & vbCrLf
'Now, show next/prev links if applicable
strOutput = strOutput & "if (first > 0) // show prev link" & vbCrLf & _
vbTab & "txt += '<a href=""
javascript:nav(-1);"">Prev " & _
iRecsPerPage & "</a> ';" & vbCrLf & vbCrLf & _
"if (last <= " & iRows & ") // show next link" & vbCrLf & vbTab & _
"txt += '<a href=""
javascript:nav(1);"">Next " & _
iRecsPerPage & "</a>';" & vbCrLf & vbCrLf
'Write out the new HTML content to the DIV tag
strOutput = strOutput & "// write out the the DIV tag depending on browser..." & vbCrLf & _
"if (mynav == ""NS"") {" & vbCrLf & vbTab & _
"document.layers['grid'].document.write(txt);" & vbCrLf & vbTab & _
"document.close();" & vbCrLf & "}" & vbCrLf & vbCrLf & _
"if (mynav == ""IE"")" & vbCrLf & vbTab & _
"document.all['grid'].innerHTML = txt;" & vbCrLf & vbCrLf & _
"}" & vbCrLf & vbCrLf
strOutput = strOutput & "nav(0);" & vbCrLf & "</script>"
GenerateHTML = strOutput
End Function
'*******************************************
End Class
%>
……