zoukankan      html  css  js  c++  java
  • asp实用类库DataGrid

    DataGrid:

    <%
    '<class>
    '<name>DataGrid</name>
    '<description><![CDATA[数据表格version2.0;增加缓存模板功能,大大减少了模板解析时间;作者:圣诞菠萝包]]></description>
    '<attributes>
    ' <attribute name="ID" comment="只写;数据列表ID"/>
    ' <attribute name="PageArg" comment="分页参数;默认为CurrentPage"/>>
    ' <attribute name="RecordCount" comment="只写;记录总数;与Row一起计算分页数;"/>
    ' <attribute name="ShowSelectCol" comment="只写;是否显示checkbox列;"/>
    ' <atrribute name="OperationTemplate" comment="只写;操作列模板;"/>
    ' <attribute name="ToolTemplate" comment="只写;附加工具模板;"/>
    ' <attribute name="Alternating" comment="只写;是否显示交换行;"/>
    ' <attribute name="DataSource" comment="数据源(断开的记录集)"/>
    ' <attribute name="ShowPageStyle" comment="是否分页"/>
    ' <attribute name="FormatTemplate" comment="格式化数据"/>
    ' <attribute name="HeaderTemplate" comment="格式化表头"/>
    '</attributes>
    '<methods>
    ' <method name="DataBind()" comment="数据绑定,显示数据表格;数据源为断开的记录集;"/>
    '</methods>
    '</class>

    Class DataGrid
     
     Private id_
     Private pagesize_
     Private recordCount_
     Private showSelectCol_
     Private operationTemplate_
     Private alternating_
     Private toolTemplate_
     Private pageArg_
     Private dataSource_
     Private showPageStyle_
     Private formatTemplate_
     Private headerTemplate_
     
     Public Property Let ID(ByVal strID)
      id_=strID
     End Property
     
     Public Property Let RecordCount(ByVal intRecordCount)
      If IsNumeric(intRecordCount) Then
       recordCount_=intRecordCount
      End If
     End Property
     
     Public Property Let ShowSelectCol(ByVal boolValue)
      showSelectCol_=boolValue
     End Property
     
     Public Property Let OperationTemplate(ByVal strTemplate)
      operationTemplate_=strTemplate
     End Property
     
     Public Property Let Alternating(ByVal boolValue)
      If boolValue=False Then
       alternating_=False
      Else
       alternating_=True
      End If
     End Property
     
     Public Property Let ToolTemplate(ByVal strTemplate)
      toolTemplate_=strTemplate
     End Property
     
     '设置分页参数
     Public Property Let PageArg(ByVal strArg)
      If strArg<>"" Then
       pageArg_=strArg
      End If
     End Property
     
     Public Property Let ShowPageStyle(ByVal boolValue)
      If boolValue=False Then
       showPageStyle_=False
      Else
       showPageStyle_=True
      End If
     End Property
     
     Public Property Let FormatTemplate(ByVal strFormatTemplate)
      formatTemplate_=strFormatTemplate
     End Property
     
     Public Property Let HeaderTemplate(ByVal strHeaderTemplate)
      headerTemplate_=strHeaderTemplate
     End Property

     Public Property Let DataSource(ByRef objRS)
      Set dataSource_=objRS
     End Property
     
     Public Function DataBind()
      Response.Write("<!--数据表格" & id_ & "开始-->" & VBCrlf)
      Response.Write("<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"" id=""" & id_ & """>" & VBCrlf)
      '表头
      Response.Write("  <tr id=""" & id_ & "_header"">" & VBCrlf)
      Response.Write("    <td width=""1%""><div align=""center"">&nbsp;</div></td>" & VBCrlf)
      Dim intCol,intFieldCount
      intFieldCount=dataSource_.Fields.Count-1
      For intCol=0 To intFieldCount
       If showSelectCol_=True And intCol=0 Then
        Response.Write("    <td><div align=""center""><input name=""SelectAll"" type=""checkbox"" id=""SelectAll"" value=""1"" onclick=""selectAll('" & id_ & "',this);"" /></td>" & VBCrlf)
       Else
        Response.Write("    <td><div align=""center"">" & ParseHeader(intCol) & "</div></td>" & VBCrlf)
       End If
      Next
      Response.Write("    <td><div align=""center"">相关操作</div></td>" & VBCrlf)
      Response.Write("  </tr>" & VBCrlf)
      '数据行
      Dim intRow,intAlternating
      For intRow=0 To dataSource_.RecordCount-1
       If intAlternating mod 2 =0 Then
        Response.Write("  <tr class=""" & id_ & "_item"">" & VBCrlf)
       Else
        Response.Write("  <tr class=""" & id_ & "_item_alternating"">" & VBCrlf)
       End If
       Response.Write("    <td width=""1%"" class=""col_head""><div align=""center"">&nbsp;</div></td>" & VBCrlf)
       For intCol=0 To intFieldCount
        Response.Write("    <td>")
        If showSelectCol_=True And intCol=0 Then
         Response.Write("<div align=""center""><input name=""" & id_ & "_checkbox"" type=""checkbox"" id=""" & id_ & "_checkbox" & ParseFormatTemplate(intCol) & """ value=""" & ParseFormatTemplate(intCol) & """ /></div>")
        Else
         Response.Write("<div>" & ParseFormatTemplate(intCol) & "</div>")
        End If
        Response.Write("</td>" & VBCrlf)
       Next
       '解析并输出操作模板
       Response.Write("    <td><div align=""center"">")
       Response.Write(ParseOperationTemplate())
       Response.Write("</div></td>" & VBCrlf)
       Response.Write("  </tr>" & VBCrlf)
       intAlternating = intAlternating  + 1
       dataSource_.MoveNext()
      Next
      '表尾
         Response.Write("  <tr id=""" & id_ & "_footer"">" & VBCrlf)
           Response.Write("    <td><div align=""center"">&nbsp;</div></td>" & VBCrlf)
           Response.Write("    <td colspan=""" & intFieldCount+2 & """>")
      Response.Write("<div style=""float:left;padding-left:6px;padding-right:6px;margin-top:4px;"">")
      Response.Write("<form action="""" method=""post"" id=""" & id_ & "_toolfrm"">" & toolTemplate_ & "</form>")
      Response.Write("</div>")
      If showPageStyle_=True Then
       Call CreatePageStyle()
      End If
      Response.Write("</td>" & VBCrlf)
         Response.Write("  </tr>" & VBCrlf)
      Response.Write("</table>" & VBCrlf)
      Response.Write("<!--数据表格" & id_ & "结束-->" & VBCrlf)
      dataSource_.Close()
      Set dataSource_=Nothing
     End Function
     
     '解板表格头
     Private Function ParseHeader(ByVal intCol)
      If headerTemplate_="" Then
       ParseHeader=dataSource_(intCol).Name
       Exit Function
      End If
      Dim objRegExp,objMatches,strTemp,arrTemp,objMatch
      Set objRegExp=new RegExp
      objRegExp.Pattern = "{(\d+):([^}]+)}+?"
      objRegExp.IgnoreCase = True
      objRegExp.Global = True
      Set objMatches=objRegExp.Execute(headerTemplate_)
      For Each objMatch In objMatches
       If Cint(objMatch.SubMatches(0))=intCol Then
        ParseHeader = objMatch.SubMatches(1)
        Exit Function
       End If
      Next
      ParseHeader=dataSource_(intCol).Name
      Set objMatches=Nothing
      Set objRegExp=Nothing
     End Function
     
     Private arrFormatTemp '模板缓存数组,减少解析时间;
     
     '解析格式化模板
     Private Function ParseFormatTemplate(ByVal intCol)
      If formatTemplate_="" Then
       ParseFormatTemplate=dataSource_(intCol)
       Exit Function
      End If
      If IsArray(arrFormatTemp) Then
       If arrFormatTemp(intCol,0)=intCol Then
        If IsArray(arrFormatTemp(intCol,1)) Then
         For i=0 To Ubound(arrFormatTemp(intCol,1)(0))
          If i<Ubound(arrFormatTemp(intCol,1)(0)) Then
           ParseFormatTemplate = ParseFormatTemplate & arrFormatTemp(intCol,1)(0)(i) & Eval(arrFormatTemp(intCol,1)(1)(i))
          Else
           ParseFormatTemplate = ParseFormatTemplate & arrFormatTemp(intCol,1)(0)(i)
          End If
         Next
        Else
         ParseFormatTemplate = Eval(arrFormatTemp(intCol,1))
        End If
        Exit Function
       End If
      End If
      Dim objRegExp,objMatches,strTemp,arrTemp,objMatch
      Set objRegExp=new RegExp
      objRegExp.Pattern = "{(\d+):([^}]+)}+?"
      objRegExp.IgnoreCase = True
      objRegExp.Global = True
      Set objMatches=objRegExp.Execute(formatTemplate_)
      If Not IsArray(arrFormatTemp) Then
       ReDim arrFormatTemp(dataSource_.Fields.Count-1,1)
      End If
      Dim Flag
      For Each objMatch In objMatches
       If Cint(objMatch.SubMatches(0))=intCol Then
        If Instr(objMatch.SubMatches(1),"#")>0 Then
         Dim objSubRegExp,objSubMatches,objSubMatch,arrTempLen,i
         Set objSubRegExp=new RegExp
         objSubRegExp.Pattern = "#(\d+)?"
         objSubRegExp.IgnoreCase = True
         objSubRegExp.Global = True
         strTemp=objSubRegExp.Replace(objMatch.SubMatches(1),"{CHCW_SEPARATOR}")
         arrTemp=Split(strTemp,"{CHCW_SEPARATOR}")
         arrTempLen=UBound(arrTemp)
         Dim arrFormatTemplate,arrFormatIndex
         ReDim arrFormatTemplate(arrTempLen)
         ReDim arrFormatIndex(arrTempLen-1)
         Set objSubMatches=objSubRegExp.Execute(objMatch.SubMatches(1))
         For i=0 To arrTempLen
          If i<arrTempLen Then
           ParseFormatTemplate = ParseFormatTemplate & arrTemp(i) & dataSource_(Cint(objSubMatches(i).SubMatches(0)))
           arrFormatIndex(i) = "dataSource_(" & objSubMatches(i).SubMatches(0) & ")"
          Else
           ParseFormatTemplate = ParseFormatTemplate & arrTemp(i)
          End If
          arrFormatTemplate(i) = arrTemp(i)
         Next
         Set objSubMatches=Nothing
         Set objSubRegExp=Nothing
         arrFormatTemp(intCol,0)=intCol
         arrFormatTemp(intCol,1)=Array(arrFormatTemplate,arrFormatIndex)
         Exit Function
        ElseIf Instr(objMatch.SubMatches(1),"==")>0 Then
         Dim arrReTemp
         arrReTemp=Split(objMatch.SubMatches(1),"==")
         If StrComp(arrReTemp(0),dataSource_(intCol))=0 Then
          ParseFormatTemplate = arrReTemp(1)
          Exit Function
         Else
          ParseFormatTemplate=dataSource_(intCol)
          Flag=True
         End If 
        Else
         ParseFormatTemplate=dataSource_(intCol)
         arrFormatTemp(intCol,0)=intCol
         arrFormatTemp(intCol,1)="dataSource_(" & intCol & ")"
         Exit Function
        End If
       End If
      Next
      If Flag=True Then Exit Function
      ParseFormatTemplate=dataSource_(intCol)
      arrFormatTemp(intCol,0)=intCol
      arrFormatTemp(intCol,1)="dataSource_(" & intCol & ")"
      Set objMatches=Nothing
      Set objRegExp=Nothing
     End Function
     
     Private arrOperationTemp,arrOperationIndex '模板缓存数组,减少解析时间;
     
     '解析操作模板
     Private Function ParseOperationTemplate()
      If operationTemplate_="" Then Exit Function
      If IsArray(arrOperationTemp) Then
       For i=0 To UBound(arrOperationTemp)
        If i<UBound(arrOperationTemp) Then
         ParseOperationTemplate=ParseOperationTemplate & arrOperationTemp(i) & Eval(arrOperationIndex(i))
        Else
         ParseOperationTemplate=ParseOperationTemplate & arrOperationTemp(i)
        End If
       Next
       Exit Function
      End If
      Dim objRegExp,objMatches,strTemp,arrTemp,i,arrTempLen
      Set objRegExp=new RegExp
      objRegExp.Pattern = "{(\d+)}"
      objRegExp.IgnoreCase = True
      objRegExp.Global = True
      strTemp=objRegExp.Replace(operationTemplate_,"{CHCW_SEPARATOR}")
      arrTemp=Split(strTemp,"{CHCW_SEPARATOR}")
      Set objMatches=objRegExp.Execute(operationTemplate_)
      arrTempLen=UBound(arrTemp)
      ReDim arrOperationTemp(arrTempLen)
      ReDim arrOperationIndex(arrTempLen-1)
      For i=0 To arrTempLen
       If i<UBound(arrTemp) Then
        ParseOperationTemplate = ParseOperationTemplate & arrTemp(i) & dataSource_(Cint(objMatches(i).SubMatches(0)))
        arrOperationIndex(i)="dataSource_(" & objMatches(i).SubMatches(0) & ")"
       Else
        ParseOperationTemplate = ParseOperationTemplate & arrTemp(i)
       End If
       arrOperationTemp(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()
      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 dataSource_.RecordCount=0 Then
       intPageCount=0
      Else
       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
      End If
      If intCurrentPage>intPageCount Then
       intCurrentPage=intPageCount
      End If
      Response.Write("<div style=""float:right;text-align:right;""><form action="""" method=""get"">共" & intCurrentPage & "/" & intPageCount & "页," & recordCount_ & "个记录 " & VBCrlf)
      If intCurrentPage>1 Then
       Response.Write("<a href=""?" & pageArg_ & "=1" &FilterUrlArgs & """><img src=""Images/DataGrid/First.gif"" alt=""首页"" width=""9"" height=""8"" border=""0"" /></a> ")
       Response.Write("<a href=""?" & pageArg_ & "=" & intCurrentPage -1 & FilterUrlArgs & """><img src=""Images/DataGrid/Previous.gif"" alt=""上一页"" width=""8"" height=""8"" border=""0"" /></a> ")
      Else
       Response.Write("<img src=""Images/DataGrid/dis_First.gif"" alt=""首页"" width=""9"" height=""8"" /> <img src=""Images/DataGrid/dis_Previous.gif"" alt=""上一页"" width=""8"" height=""8"" /> ")
      End If
      Dim intPageNum,intBeginNum,intEndNum,i
      intBeginNum=1
      intEndNum=1
      intPageNum=5
      If intPageCount<intPageNum Then
       intBeginNum=1
       intEndNum=intPageCount
      Else
       If intCurrentPage=<int(intPageNum/2) Then
        intBeginNum=1
        intEndNum=intPageNum
       ElseIf intCurrentPage>intPageCount-int(intPageNum/2) Then
        intBeginNum=intCurrentPage-int(intPageNum/2)*2
        intEndNum=intPageCount
       Else 
        intBeginNum=intCurrentPage-int(intPageNum/2)
        intEndNum=intCurrentPage+int(intPageNum/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 & """><img src=""Images/DataGrid/Next.gif"" alt=""下一页"" width=""8"" height=""8"" border=""0"" /></a> ")
       Response.Write("<a href=""?" & pageArg_ & "=" & intPageCount & FilterUrlArgs & """> <img src=""Images/DataGrid/Last.gif"" alt=""尾页"" width=""9"" height=""8"" border=""0"" /></a> ")
      Else
       Response.Write("<img src=""Images/DataGrid/dis_Next.gif"" alt=""下一页"" width=""8"" height=""8"" /> <img src=""Images/DataGrid/dis_Last.gif"" alt=""尾页"" width=""9"" height=""8"" /> ")
      End If
      Response.Write("转到<input name=""" & pageArg_ & """ type=""text"" class=""inputText"" maxlength=""8"" style=""30px;"" />页" & VBCrlf)
      Response.Write("</form></div>")
     End Function
     
     '初始化
     Private Sub Class_Initialize()
      id_="datagrid"
      pagesize_=20
      showSelectCol_=True
      operationTemplate_=""
      alternating_=True
      pageArg_="CurrentPage"
      showPageStyle=True
      formatTemplate_=""
     End Sub
     
     '销毁
     Private Sub Class_Terminate()
      If Not dataSource_ Is Nothing Then
       dataSource_.Close()
       Set dataSource_=Nothing
      End If
     End Sub
     
    End Class
    %>

    同样,灵活性不够,因为一般用于后台。

    例子:
    <%
    Dim objDataGrid
    Set objDataGrid=new DataGrid
    objDataGrid.ID="datagrid"
    objDataGrid.OperationTemplate="<a href=""News_Delete.asp?NewsID={0}"" onclick=""return confirm('是否真的删除该记录?');""><img src=""Images/DataGrid/Delete.gif"" border=""0"" alt=""删除"" /></a> <a href=""News_Modify.asp?NewsID={0}""><img src=""Images/DataGrid/Update.gif"" border=""0"" alt=""修改"" /></a>"
    objDataGrid.ToolTemplate="<a href=""javascript:Operate('datagrid','News_Delete.asp?NewsID=');"">删除所选</a>"
    objDataGrid.FormatTemplate="{2:<a href=""/School/News_Detail.asp?NewsID=#0"" target=""_blank"">#2</a>}"
    objDataGrid.HeaderTemplate="{1:所属栏目}{2:新闻标题}{3:录入时间}{4:来源}{5:编辑}{6:点击}"
    objDataGrid.RecordCount=intRecordCount '由News类的一个方法提供
    objDataGrid.DataSource=objRS  '由News类的一个方法提供
    objDataGrid.DataBind()
    Set objDataGrid=Nothing
    %>

    图片:

    希望大家多多交流! 时间伧促,如有错误,请指出!
    等我忙完这阵子,我再重新整理,修改(如,把DataGrid修改得更加灵活,还有多皮肤....)

  • 相关阅读:
    第8月第15天 app支持后台播放
    第8月第12天 python json.dumps danmu
    第7月第27天 c++11 boost
    第7月第26天 iOS httpserver
    第7月第25天 xcode bundle calayer动画
    我曾七次鄙视自己的灵魂
    learning shell display alert function(5)
    learning armbian steps(6) ----- armbian 源码分析(一)
    learning scala 数组和容器
    learning scala ide tools install
  • 原文地址:https://www.cnblogs.com/MaxIE/p/451396.html
Copyright © 2011-2022 走看看