让操作更便捷 ASPTable类


经常写代码,对于无聊的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"-->
<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
%>


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3