• asp实用类库DataList


    DataList:

    <%
    '<class>
    '<name>DataList</name>
    '<description><![CDATA[数据列表;作者:圣诞菠萝包]]></description>
    '<attributes>
    ' <attribute name="ID" comment="只写;数据列表ID"/>
    ' <attribute name="col" comment="只写;数据列表列数;当列数为1时,将以ul方式显示,否则以table方式显示;"/>
    ' <attribute name="ShowPageStyle" comment="只写;设置显示分页,当为False时,分页栏将不显示;默认值为True;"/>
    ' <attribute name="PageArg" comment="只写;分页参数;默认为CurrentPage"/>
    ' <attribute name="RecordCount" comment="只写;记录总数,分页显示时必须指定;"/>
    ' <attribute name="ItemTemplate" comment="只写;行模板;"/>
    ' <attribute name="AlternatingItemTemplate" comment="只写;交换行模板;"/>
    ' <attribute name="Style" comment="只写;数据列表样式"/>
    ' <attribute name="PageStyle" comment="只写;数据列表分页样式"/>
    ' <attribute name="DataSource" comment="只写;数据源"/>
    '</attributes>
    '<methods>
    ' <method name="DataBind()" comment="数据绑定,显示数据列表;"/>
    '</methods>
    '</class>

    Class DataList
     
     Private id_       '数据列表ID
     Private pageSize_     '每页显示记录数
     Private colNumber_     '列数
     Private showPageStyle_    '是否允许分页
     Private pageArg_     '分页参数
     Private recordCount_    '记录总数
     Private itemTemplate_    '行模板
     Private alternatingItemTemplate_ '交换行模板
     Private style_      'DataList主体样式
     Private pageStyle_     '分页样式
     Private dataSource_     '数据源
     
     '设置DataList的ID
     Public Property Let ID(ByVal strID)
      id_=strID
     End Property
     
     '设置列数
     Public Property Let Col(ByVal intCol)
      On Error Resume Next
      intCol=Clng(intCol)
      If Err.Number<>0 Or intCol<1 Then
       Err.Clear()
       colNumber_=1
      Else
       colNumber_=intCol
      End If
     End Property
     
     '设置是否显示分页
     Public Property Let ShowPageStyle(ByVal boolAllow)
      If boolAllow<>False Then
       showPageStyle_=True
      Else
       showPageStyle_=False
      End If
     End Property
     
     '设置分页参数
     Public Property Let PageArg(ByVal strArg)
      If strArg<>"" Then
       pageArg_=strArg
      End If
     End Property
     
     '设置记录总数
     Public Property Let RecordCount(ByVal intRecordCount)
      recordCount_=intRecordCount
     End Property
     
     '设置行模板
     Public Property Let ItemTemplate(ByVal strItemTemplate)
      itemTemplate_=strItemTemplate
     End Property
     
     '设置交换行模板
     Public Property Let AlternatingItemTemplate(ByVal strItemTemplate)
      alternatingItemTemplate_=strItemTemplate
     End Property
     
     '设置样式
     Public Property Let Style(ByVal strStyle)
      style_=strStyle
     End Property
     
     '设置分页样式
     Public Property Let PageStyle(ByVal strStyle)
      pageStyle_=strStyle
     End Property
     
     '设置数据源
     Public Property Let DataSource(ByRef objRS)
      Set dataSource_=objRS
     End Property
     
     '数据绑定
     Public Function DataBind()
      Dim intRow
      Response.Write("<!--数据列表:" & id_ & "开始-->" & VBCrlf)
      If colNumber_=1 Then
       '单列,用ul展示
       Response.Write("<div id=""" & id_ & """ class=""" & style_ & """>" & VBCrlf)
       Response.Write("  <ul>" & VBCrlf)
       For intRow=0 To dataSource_.RecordCount-1
        If alternatingItemTemplate_<>"" Then
         If  intRow mod 2 =0 Then
          Response.Write("    " & ParserItemTemplate(itemTemplate_) & VBCrlf)
         Else
          Response.Write("    " & ParserAlternatingTemplate(alternatingItemTemplate_) & VBCrlf)
         End If
        Else
         Response.Write("    " & ParserItemTemplate(itemTemplate_) & VBCrlf)
        End If
        dataSource_.MoveNext()
       Next
       Response.Write("  </ul>" & VBCrlf)
       Response.Write("</div>" & VBCrlf)
      Else
       '多列,用表格展示
       Response.Write("<table name=""" & id_ & """ id=""" & id_ & """ cellspacing=""0"" cellpadding=""0"" class=""" & style_ & """>" & VBCrlf)
       Dim intCol
       For intRow=0 To dataSource_.RecordCount-1
        Response.Write("  <tr>" & VBCrlf)
        For intCol=1 To colNumber_
          Response.Write("    <td style=""" & int(100/colNumber_)& "%"">")
          If alternatingItemTemplate_<>"" Then
           If (intRow mod (colNumber_)*2 =0) Or Fix(intRow/colNumber_) mod 2 =0 Then
            Response.Write(ParserItemTemplate(itemTemplate_))
           Else
            Response.Write(ParserAlternatingTemplate(alternatingItemTemplate_))
           End If
          Else
           Response.Write(ParserItemTemplate(itemTemplate_))
          End If
          Response.Write("</td>" & VBCrlf)
          If intCol<colNumber_ Then
           intRow = intRow + 1
          End If
          dataSource_.MoveNext()
          If dataSource_.EOF Then Exit For
        Next
        Response.Write("  </tr>" & VBCrlf)
        If dataSource_.EOF Then Exit For
       Next
       Response.Write("</table>" & VBCrlf)
      End If
      Response.Write("<!--数据列表:" & id_ & "结束-->" & VBCrlf)
      '显示分页
      If showPageStyle_=True Then
       Call CreatePageStyle()
      End If
      dataSource_.Close()
      Set dataSource_=Nothing
     End Function
     
     Private arrItemTemplate,arrItemIndex '模板缓存数组,减少解析时间;
     Private Function ParserItemTemplate(ByVal strItemTemplate)
      ParserItemTemplate=ParseTemplate(strItemTemplate,arrItemTemplate,arrItemIndex)
     End Function
     
     Private arrAlternatingTemplate,arrAlternatingIndex '模板缓存数组,减少解析时间;
     Private Function ParserAlternatingTemplate(ByVal strItemTemplate)
      ParserAlternatingTemplate=ParseTemplate(strItemTemplate,arrAlternatingTemplate,arrAlternatingIndex)
     End Function
     
     '解析模板
     Private Function ParseTemplate(ByVal strItemTemplate,ByRef arrTemplate,ByRef arrIndex)
      If IsArray(arrTemplate) Then
       For i=0 To UBound(arrTemplate)
        If i<UBound(arrTemplate) Then
         ParseTemplate=ParseTemplate & arrTemplate(i) & Eval(arrIndex(i))
        Else
         ParseTemplate=ParseTemplate & arrTemplate(i)
        End If
       Next
       Exit Function
      End If
      Dim objRegExp,objMatches,strTemp,arrTemp,i,arrTempLen
      Set objRegExp=new RegExp
      objRegExp.Pattern = "{(\d+|(\d|,)+)}"
      objRegExp.IgnoreCase = True
      objRegExp.Global = True
      strTemp=objRegExp.Replace(strItemTemplate,"{CHCW_SEPARATOR}")
      arrTemp=Split(strTemp,"{CHCW_SEPARATOR}")
      Set objMatches=objRegExp.Execute(strItemTemplate)
      arrTempLen=UBound(arrTemp)
      ReDim arrTemplate(arrTempLen)
      ReDim arrIndex(arrTempLen-1)
      For i=0 To arrTempLen
       If i<arrTempLen Then
        Dim objRegExpSub
        Set objRegExpSub=new RegExp
        objRegExpSub.Pattern="^(\d+),(\d+)$"
        objRegExpSub.IgnoreCase = True
        objRegExpSub.Global = True
        If objRegExpSub.Test(objMatches(i).SubMatches(0)) Then
         Dim objMatchesSub
         Set objMatchesSub=objRegExpSub.Execute(objMatches(i).SubMatches(0))
         ParseTemplate = ParseTemplate & arrTemp(i) & Left(dataSource_(Cint(objMatchesSub(0).SubMatches(0))),objMatchesSub(0).SubMatches(1))
         arrIndex(i)="Left(dataSource_("&objMatchesSub(0).SubMatches(0)&"),"&objMatchesSub(0).SubMatches(1)&")"
        Else
         ParseTemplate = ParseTemplate & arrTemp(i) & dataSource_(Cint(objMatches(i).SubMatches(0)))
         arrIndex(i)="dataSource_(" & objMatches(i).SubMatches(0) & ")"
        End If
        Set objMatchesSub=Nothing
        Set objRegExpSub=Nothing
       Else
        ParseTemplate = ParseTemplate & arrTemp(i)
       End If
       arrTemplate(i) = arrTemp(i)
      Next
      Set objMatches=Nothing
      Set objRegExp=Nothing
     End Function

     '过滤url参数
     Private Function FilterUrlArgs()
      Dim aArg
      For each aArg In Request.QueryString
       If StrComp(aArg,pageArg_,1)<>0 Then
        FilterUrlArgs = FilterUrlArgs & "&" & aArg &"=" & Eval("Request.QueryString(aArg)")
       End If
      Next
     End Function
     
     '分页样式
     Private Function CreatePageStyle()
      On Error Resume Next
      Dim intCurrentPage
      intCurrentPage=Request.QueryString(pageArg_)
      If intCurrentPage="" Or Not IsNumeric(intCurrentPage) Then
       intCurrentPage=1
      End If
      intCurrentPage=Clng(intCurrentPage)
      If intCurrentPage<1 Then
       intCurrentPage=1
      End If
      If Err.Number<>0 Then
       Err.Clear()
       intCurrentPage=1
      End If
      If Application("CHCW_PageSize_" & id_ & "_" & Request.ServerVariables("SCRIPT_NAME"))="" Then
       intCurrentPage=1
      End If
      If intCurrentPage=1 Then
       Application.Lock()
       Application("CHCW_PageSize_" & id_ & "_" & Request.ServerVariables("SCRIPT_NAME"))=dataSource_.RecordCount
       Application.UnLock()
      End If
      Dim intPageCount
      If recordCount_ mod Application("CHCW_PageSize_" & id_ & "_" & Request.ServerVariables("SCRIPT_NAME")) =0 Then
       intPageCount=int(recordCount_/Application("CHCW_PageSize_" & id_ & "_" & Request.ServerVariables("SCRIPT_NAME")))
      Else
       intPageCount=int(recordCount_/Application("CHCW_PageSize_" & id_ & "_" & Request.ServerVariables("SCRIPT_NAME"))) + 1
      End If
      If intCurrentPage>intPageCount Then
       intCurrentPage=intPageCount
      End If
      Response.Write("<!--数据列表:" & id_ & "分页样式开始-->" & VBCrlf)
      Response.Write("<div id=""" & id_ & "_PageStyle"" class=""" & pageStyle_ & """><form action="""" method=""get"">")
      Response.Write("共" & intCurrentPage & "/" & intPageCount & "页," & recordCount_ & "条记录 ")
      If intCurrentPage>1 Then
       Response.Write("<a href=""?" & pageArg_ & "=1" &FilterUrlArgs & """>首 页</a> ")
       Response.Write("<a href=""?" & pageArg_ & "=" & intCurrentPage -1 & FilterUrlArgs & """>上一页</a> ")
      Else
       Response.Write("首 页 ")
       Response.Write("上一页 ")
      End If
      Dim intPageIndex,i,intBeginNum,intEndNum
      intBeginNum=1
      intEndNum=1
      intPageIndex=5
      If intPageCount<intPageIndex Then
       intBeginNum=1
       intEndNum=intPageCount
      Else
       If intCurrentPage=<int(intPageIndex/2) Then
        intBeginNum=1
        intEndNum=intPageIndex
       ElseIf intCurrentPage>intPageCount-int(intPageIndex/2) Then
        intBeginNum=intCurrentPage-int(intPageIndex/2)*2
        intEndNum=intPageCount
       Else
        intBeginNum=intCurrentPage-int(intPageIndex/2)
        intEndNum=intCurrentPage+int(intPageIndex/2)
       End If
      End If
      For i=intBeginNum To intEndNum
       If i=intCurrentPage Then
        Response.Write(" " & i & " ")
       Else
        Response.Write(" <a href=""?" & pageArg_ & "=" & i & FilterUrlArgs & """>" & i & "</a> ")
       End If
      Next
      If intCurrentPage<intPageCount Then
       Response.Write("<a href=""?" & pageArg_ & "=" & intCurrentPage+1 & FilterUrlArgs & """>下一页</a> ")
       Response.Write("<a href=""?" & pageArg_ & "=" & intPageCount & FilterUrlArgs & """>尾 页</a> ")
      Else
       Response.Write("下一页 ")
       Response.Write("尾 页 ")
      End If
      Response.Write("转到<input name=""" & pageArg_ & """ type=""text"" class=""inputText"" style=""30px;"" maxlength=""8"" />页" & VBCrlf)
      Response.Write("</form></div>" & VBCrlf)
      Response.Write("<!--数据列表:" & id_ & "分页样式结束-->")
     End Function
     
     '初始化
     Private Sub Class_Initialize()
      pageSize_=20
      colNumber_=1
      showPageStyle_=True
      pageArg_="CurrentPage"
      itemTemplate_="模板内容<br />"
      alternatingItemTemplate_=""
      id_="DataList1"
     End Sub
     
     '销毁
     Private Sub Class_Terminate()
      If Not dataSource_ Is Nothing Then
       dataSource_.Close()
       Set dataSource_=Nothing
      End If
     End Sub
     
    End Class
    %>

    还有很多不足地方,灵活性还不够!不过大多情况也是适用的!

    例子:
    <!--#Include Virtual="/ASPLib/Util/Configuration.asp"-->
    <!--#Include Virtual="/ASPLib/Util/DataAccess.asp"-->
    <!--#include Virtual="/ASPLib/Control/DataList.asp"-->
    <!--#include Virtual="/School/DAL/News.asp"-->
    <%
    Dim objDL
    Set objDL=new DataList
    objDL.ID="DataList1"
    'objDL.PageArg="CurrentPage"
    'objDL.ShowPageStyle=True
    'objDL.Col=1
    objDL.ItemTemplate="<li>·<a href=""news.asp?NewsID={0}"">{1,20}</a>({2})</li>" '{0}表示取得记录集Rs(0),{1,20}表示取得记录集Rs(1),并取得前20个字符;
    objDL.AlternatingItemTemplate="<li style=""background:#F4F4F4;"">·<a href=""news.asp?NewsID={0}"">{1,20}</a>({2})</li>"
    objDL.RecordCount=News.GetRecordNewsList(Keyword,NewsCategoryID)  '调用News类的一个方法取得记录总数
    objDL.DataSource=News.GetNewsList(PageSize,CurrentPage,Keyword,NewsCategoryID)   '调用News类的方法取得记录集
    objDL.DataBind()
    Set objDL=Nothing
    %>

  • 相关阅读:
    LeetCode 10 Regular Expression Matching(字符串匹配)
    LeetCode 9 Palindrome Number(回文数字判断)
    操作系统期末复习资料分享
    计算机网络那些事~(二)
    seL4之hello-3征途
    计算机网络那些事~(一)
    seL4之hello-2旅途(完成更新)
    博客声明
    seL4环境配置
    Canvas链式操作
  • 原文地址:https://www.cnblogs.com/MaxIE/p/451393.html
Copyright © 2020-2023  润新知