日期:2013-04-16  浏览次数:21220 次

创建一个分页类

  要记住,这里举例的整个应用程序只包括一个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) { " &