• 可排序的分页类


    把下面的代码COPY成两个文件再把SQL语句和连接串改了就可以了。。。。

    演示地址:http://www.w2000.com.cn/sweet/class.asp

    [CODE]<%
    Option Explicit

    ''''''''''''''''''''''''''''''''''''''''''''
    '属性
    'countPage    总页数
    'currentPage  当前页数
    'Size         总长度
    'fieldList    保存查询的列的集合
    'orderByQuery 保存消除了orderby和desc和curp了的GET串
    'con,rs,pagesize,。。。
    '方法
    'initObject   初始化成员
    'page         显示页信息
    ''''''''''''''''''''''''''''''''''''''''''''

    Class Cls_Kellynic_Page
        '私用成员
        Private countPage,currentPage,Size
        Private fieldList,orderByQuery,orderBy,desc
        '公共成员
        Public con,rs,pageSize
        
        '析构
        Private Sub Class_Terminate()
            On Error resume Next
            rs.Close
            Set rs        =    Nothing
            con.Close
            Set con        =    Nothing
            Err.Clear
        End Sub
        '构造
        Private Sub Class_Initialize()
            Set con        =    Server.CreateObject("ADODB.Connection")
            Set rs        =    Server.CreateObject("ADODB.RecordSet")
        End Sub
        '过滤不需要的GET串
        Private Function filterGET(ByVal filters)
            Dim url,i,obj,newGet,flag
            Set url        =    Request.QueryString()
            filters        =    Split(filters,",")        
            newGet        =    ""
            For Each obj In url
                flag    =    False
                For i=0 To UBound(filters)
                    If Trim(LCase(CStr(obj)))=Trim(LCase(CStr(filters(i)))) Then
                        flag=True
                        Exit For
                    End If
                Next
                If Not flag Then newGet    =    newGet    &    obj    &    "="    &    url(obj)    &    "&"
            Next
            If Not newGet="" Then newGet=Trim(Left(newGet,Len(newGet)-1))
            filterGET=newGet
        End Function
        '我的一个过滤函数,过滤掉不为数字的字符
        Function kingFilter(ByVal s)

            Dim regex
            Set regex=New RegExp
            regex.IgnoreCase=True
            regex.Global=True        
            regex.Pattern="[^\d]"
            s=regex.Replace(s,"")
            Set regex=Nothing
            If s="" Then s=0
            kingFilter=s
        End Function
        '初始化成员对象
        Public Function initObject(ByVal linkString,ByVal sql,ByVal defaultPageSize)
            On Error resume Next
            con.Open linkString
            If Err<>0 Then
                Err.Clear
                initObject    =    1
                Exit Function
            End If
            
            fieldList    =    Trim(Left(sql,InStr(sql," from ")-1))
            fieldList    =    Trim(Right(fieldList,Len(fieldList)-Len("select ")))
            
            Dim selectTemp,descTemp
            orderByQuery    =    filterGet("orderBy,desc,curp")
            If Not orderByQuery="" Then orderByQuery=orderByQuery & "&"
            
            orderBy        =    kingFilter(Request.QueryString("orderBy"))-1
            desc        =    Request.QueryString("desc")
            If desc="" Then desc=True
            desc        =    CBool(desc)
            
            selectTemp    =    Split(fieldList,",")
            If orderBy<0 Then
                orderBy    =    selectTemp(0)        
            elseif orderBy>Ubound(selectTemp) Then
                orderBy    =    selectTemp(Ubound(selectTemp))
            Else
                orderBy    =    selectTemp(orderBy)

            End If
            If desc Then
                descTemp="desc"
            Else
                descTemp=""
            End If
            sql            =    sql & " order by " & orderBy & " " & descTemp
            
            On Error resume Next
            rs.Open sql,con,3,3
            If Err<>0 Then
                Err.Clear
                initObject    =    2
                Exit Function
            Else
                initObject    =    0
            End If
            
            Size        =    CLng(con.Execute("select count(*) from (" & rs.Source & ")").fields(0))
            
            pageSize    =    kingFilter(Request.QueryString("pagesize"))
            If pageSize=0 Then
                pageSize    =    CLng(defaultPageSize)
            End If
            
            If Not Size Mod pageSize=0 Then
                countPage    =    Size\pageSize+1
            Else
                countPage    =    Size\pageSize
            End If
            
            currentPage    =    CLng(kingFilter(Request.Querystring("curp")))
            If currentPage=0 Then
                currentPage    =    1
            End If
            If currentPage>countPage Then
                currentPage    =    countPage
            End If
            
            If curp<>1 And Not rs.eof Then
                'rs.Move count_Elements*(curp-1)
                rs.AbsolutePosition=pageSize*(currentPage-1)
            End If
        End Function
        '显示上下页(当前页,总页数,前后加起来的页数,除去cp参数的GET串)
        Public Function page(ByVal showPageC)

              Dim i,l,starF,endF,reStr,getStr,queryStr
              queryStr        =    filterGET("curp")
            '*****************************************************
              If queryStr="" Then
                    getStr="curp=1"
              Else
                    getStr=queryStr & "&curp=1"
              End If
              If currentPage>1 Then
                    reStr=reStr & "<a href=""?" & getStr & """ target=_self class=page>首页</a> "
              Else
                    reStr=reStr & "<font color=#CCCCCC>首页</font> "
              End If
            '*****************************************************
              If queryStr="" Then
                    getStr="curp=" & (currentPage-1)
              Else
                    getStr=queryStr & "&curp=" & (currentPage-1)
              End If
              If currentPage>1 Then
                    reStr=reStr & "<a href=""?" & getStr & """ target=_self class=page>上页</a> "
              Else
                    reStr=reStr & "<font color=#CCCCCC>上页</font> "
              End If
            '*****************************************************
              If queryStr="" Then
                    getStr="curp=" & (currentPage+1)
              Else
                    getStr=queryStr & "&curp=" & (currentPage+1)
              End If
              If currentPage<countPage Then
                    reStr=reStr & "<a href=""?" & getStr & """ target=_self class=page>下页</a> "

              Else
                    reStr=reStr & "<font color=#CCCCCC>下页</font> "
              End If
            '*****************************************************
              If queryStr="" Then
                    getStr="curp=" & countPage
              Else
                    getStr=queryStr & "&curp=" & countPage
              End If
              If currentPage<countPage Then
                    reStr=reStr & "<a href=""?" & getStr & """ target=_self class=page>末页</a> "
              Else
                    reStr=reStr & "<font color=#CCCCCC>末页</font> "
              End If
            '*****************************************************
              If queryStr="" Then
                    getStr="curp="
              Else
                    getStr=queryStr & "&curp="
              End If
              reStr=reStr & "跳转:<input type=text value=" & currentPage & " size=4 onkeyup=""this.value=this.value.replace(/[^\d]/ig,'');if(this.value>" & countPage & ")this.value=" & countPage &";"" onblur=""this.onkeyup();"" onchange=""this.onkeyup();"" onkeydown=""if(event.keyCode==13)location.href='?" & getStr & "'+this.value;""> "
              reStr=reStr & "<span class=green> " & currentPage & "/" & countPage & " " & pageSize & "/页</span> 合计:" & Size & " "

              getStr=filterGET("pagesize")
              If getStr="" Then
                    getStr="pagesize="
              Else
                    getStr=getStr & "&pagesize="
              End If
              reStr=reStr & "页长:<input type=text value=" & pagesize & " size=4 onkeyup=""this.value=this.value.replace(/[^\d]/ig,'');"" onblur=""this.onkeyup();"" onchange=""this.onkeyup();"" onkeydown=""if(event.keyCode==13)location.href='?" & getStr & "'+this.value;"">" & "<br><br>"
            '*****************************************************
              Const DOT="<span style=cursor:hand;>..</span>"
              If showPageC=0 Then showPageC    =    5
              l=showPageC\2
              If currentPage-l<=1 Then
              starF=1
              Else
                    starF=currentPage-l
                    reStr=reStr & DOT
              End If
              endF=starF+showPageC
              For i=starF To endF
                    If i>countPage Then Exit For
                    If queryStr="" Then
                    getStr="curp=" & i
                    Else
                    getStr=queryStr & "&curp=" & i
                    End If
                    If i<>currentPage Then
                    reStr=reStr & "<a href=""?" & getStr & """ target=_self class=page>[" & i & "]</a> "
                    Else
                    reStr=reStr & "<b><font color=#CC9933>[" & i & "]</a></b> "

                    End If
              Next
              If endF<countPage Then reStr=reStr & DOT
            '*****************************************************
              page=reStr
        End Function
        '构造order by地址
        Public Function getOrderBy(ByVal Index)
            getOrderBy    =    "?" & orderByQuery & "orderby=" & Index & "&desc=" & CStr(Not desc)
        End Function
    End Class

    %>%>



    测试:

    将以上的代码保存成 Cls_Kellyni_Page.asp

    请看 class.asp 里的代码:

    [CODE]<%@LANGUAGE="VBSCRIPT" CODEPAGE="936" CODEPAGE=%>
    <!--#include file="Cls_Kellynic_Page.asp"-->
    <%
    Dim startimer,endtimer
    startimer=timer()
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <style type="text/css">
    <!--
    body,td,th
    {
        font-size: 12px;
    }
    -->
    </style>
    </head>

    <body>
    <%
        Const LINK_STR="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb"
        Const OPEN_SQL="select 编号,标题,作者,点击次数,日期 from 技术文章表"
        '排序不支持 * 查询
        Dim kellynic_page,backValue
        Set kellynic_page=New Cls_Kellynic_Page
        backValue=kellynic_page.initObject(LINK_STR,OPEN_SQL,10)
        If backValue=0 Then
    %>
    <table width="100%"  border="0" cellspacing="0" cellpadding="0">
     <tr>
      <td bgcolor="#E1E1E1"><table width="100%"  border="0" cellspacing="1" cellpadding="6">
       <tr align="center" bgcolor="#F0F0F0">
        <td width="11%"><a href="<%=kellynic_page.getOrderBy(1)%>">编号</a></td>

        <td width="43%"><a href="<%=kellynic_page.getOrderBy(2)%>">标题</a></td>
        <td width="14%"><a href="<%=kellynic_page.getOrderBy(3)%>">作者</a></td>
        <td width="11%"><a href="<%=kellynic_page.getOrderBy(4)%>">点击次数</a></td>
        <td width="21%"><a href="<%=kellynic_page.getOrderBy(5)%>">日期</a></td>
       </tr>
    <%
        Dim i
        For i=1 To kellynic_page.pageSize
            If kellynic_page.rs.eof Then Exit For
    %>
       <tr bgcolor="#FFFFFF">
        <td><%=kellynic_page.rs.fields(0)%></td>
        <td><%=kellynic_page.rs.fields(1)%></td>
        <td><%=kellynic_page.rs.fields(2)%></td>
        <td><%=kellynic_page.rs.fields(3)%></td>
        <td><%=kellynic_page.rs.fields(4)%></td>
       </tr>
    <%
            kellynic_page.rs.movenext()
        Next
    %>
       <tr bgcolor="#FFFFFF">
        <td colspan="5"><%=kellynic_page.page(9)%></td>
        </tr>
      </table></td>
     </tr>
    </table>
    <%
        End If
        Set kellynic_page=Nothing
    %>
    </body>
    </html>
    <%
    endtimer=timer()
    %>
    <%=Round((endtimer-startimer)*1000,3)%>%>



    感觉这个类还行,只不过速度??
    这个类可扩展性还是很强的,用来排序,不支持*查询
  • 相关阅读:
    github使用技巧
    转载---linux运维相关
    session 测试用例详解
    php中使用linux命令四大步骤
    Thinkphp常用的方法和技巧(转)
    转学步园:jquery offset
    jquery冒泡及阻止
    nginx搭建流媒体服务器的方法详解
    SetTimeOut jquery的作用
    再也不要说,jquery动画呆板了
  • 原文地址:https://www.cnblogs.com/studio313/p/358312.html
Copyright © 2020-2023  润新知