经常写代码,对于无聊的table、tr、td真是深恶痛绝,一遍又一遍编写着重复无聊的代码,昨天看《ASP3.0高级编程》时,看到作者的一个ASP Table组件,感觉实在很方便,组件需要环境,通常情况下,我们用虚拟主机的情况要多得多,所以就着这个思路,自己用类实现,测试了一下,感觉用着确实方便,也拿来分享,也欢迎网友改进,不过别忘记发我一份!
以下是实现后的效果预览:

生成的代码有适当的缩进,还比较简单,以下是源码:
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"-->
%>
相关文章: