以下是实现后的效果预览:
生成的代码有适当的缩进,还比较简单,以下是源码:
cls_table.asp
<%
'shaoyun table类
'time:2008/8/20 14:04
'author:shaoyun
'site:shaoyun.cnblogs.com www.devjs.com
'email:shaoyun at yeah.net
Class cls_Table
Private m_RS,m_Version,m_ClassName,m_ColCount,m_TableStyle,m_HeaderStyle
Dim m_TitleCols(),m_TextCols(),m_StyleCols()
Private Sub Class_Initialize()
m_ClassName="Shaoyun Table类"
m_Version="1.0"
m_RS= ""
m_ColCount=0
m_HeaderStyle=""
End Sub
Private Sub Class_Terminate()
Set m_RS=Nothing
End Sub
Public Property Get ClassName
ClassName = m_ClassName
End Property
Public Property Get Version
Version = m_Version
End Property
Public Property Let HeaderStyle(className)
m_HeaderStyle = className
End Property
Public Property Let TableStyle(className)
m_TableStyle = className
End Property
Public Function SetRS(adors)
Set m_RS=adors
End Function
'addcol(显示标题,显示文本格式,单元格的样式)
Public Function AddCol(displayName,colFormat,className)
m_ColCount = m_ColCount + 1
Redim Preserve m_TitleCols(m_ColCount)
Redim Preserve m_TextCols(m_ColCount)
Redim Preserve m_StyleCols(m_ColCount)
m_TitleCols(m_ColCount)=displayName
m_TextCols(m_ColCount)=colFormat
m_StyleCols(m_ColCount)=className
End Function
'获取生成的格式代码
Public Function GetHTML()
Dim strHTML
strHTML=""
strHTML=strHTML & "<TABLE class=""" & m_TableStyle & """>" & vbCrLf
strHTML=strHTML & "<TR>" & vbCrLf
For i=1 to m_ColCount
strHTML=strHTML & Chr(9) & "<TH class=""" & m_HeaderStyle & """>"
strHTML=strHTML & m_TitleCols(i) & "</TH>" & vbCrLf
Next
strHTML=strHTML & "</TR>" & vbCrLf
Dim strRowText
Do While Not m_RS.EOF
strRowText=""
For i=1 to m_ColCount
strRowText=strRowText & Chr(9) & "<TD class=""" & m_StyleCols(i) & """>"
strRowText=strRowText & ExpandString(m_TextCols(i)) & "</TD>" & vbCrLf
Next
strHTML=strHTML & "<TR>" & vbCrLf & strRowText & "</TR>" & vbCrLf
m_RS.movenext
Loop
strHTML=strHTML & "</TABLE>"
GetHTML=strHTML
End Function
'分析格式字符串,替换标签
Private Function ExpandString(sourceString)
If sourceString="" Then Exit Function
Dim tokens,newText,cnt
newText=sourceString
tokens=GetTokens(sourceString)
'Join(tokens,"")==""用于判断数组是否为空
If Join(tokens,"")<>"" Then
cnt=1
do while cnt<UBound(tokens)+1
newText=replace(newText,"{$" & tokens(cnt) & "$}",m_RS(tokens(cnt)))
cnt=cnt+1
Loop
End if
ExpandString=newText
End Function
'获取字符串中的所有标签,以数组返回
Private Function GetTokens(tokenString)
Dim regEx, Match, Matches,curIndex
Dim arrTokens()
curIndex=0
Set regEx = New RegExp
regEx.Pattern = "{$w+($}){1}"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(tokenString)
For Each Match in Matches
curIndex=curIndex+1
Redim Preserve arrTokens(curIndex)
arrTokens(curIndex) = Mid(Match.Value,3,len(Match.Value)-4)
Next
GetTokens = arrTokens
End Function
End Class
%>
用法如下:
<!--#include file="conn.asp"-->
<!--#include file="cls_table.asp"-->
<style type="text/css">
.tableStyle{}{width:600px;border:1px solid #369;}
.thStyle{}{background:#369;color:#FFF;}
</style>
<%
Dim art_tab
set art_tab=new cls_table
With art_tab
.TableStyle="tableStyle"
.HeaderStyle="thStyle"
End With
openconn("db.mdb")
sqlstr="select top 10 * from [article]"
call openrs(sqlstr,1)
if not(rs.bof and rs.eof) then
With art_tab
.setrs(rs)
.addcol "ID","{$art_id$}","tdid"
.addcol "标题","<a href=""View.asp?id={$art_id$}"">{$art_title$}</a>","td1"
.addcol "作者","{$art_author$}","td2"
.addcol "操作","编辑","td2"
End With
end if
Response.Write art_tab.gethtml()
closers()
closeconn()
set art_tab=nothing
%>