ASP数据岛操作类
<%
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'            programming by smartpig                              '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class tbgrid
    public datasource                '数据源
    public style                     '表格总风格
    public headstyle                 '表头风格
    public headitemstyle             '表头单独风格
    public itemstyle                 '单元格独立网络
    public headsort      '表头是否显示排序功能
    public columns                   '需要显示的列元素
    public alternate                 '是否交替风格
    public alternatestyle            '偶数行风格
    public normalstyle               '正常风格
    public defaultstyle              '默认风格簇
    public pagesize                  '页大小
    public allowpageing              '是否分页
    public pageingstyle              '页数风格

    private templates                '自定义单元项
    private curpage                   '当前页
    private pagestart     '页面开始运行时间

    '内容之间的关系
    'columns.add "field","headtext"
    'addtemplate("headtext",template)
    'itemstyle.add "field","style:adsasd"
    'headsort.add "field",true
    'datasource(columns.keys(i))

    private sub class_initialize   ' 设置 initialize 事件。
        set itemstyle  = createobject("scripting.dictionary")
        set headsort  = createobject("scripting.dictionary")
        set headitemstyle = createobject("scripting.dictionary")
        set columns   = createobject("scripting.dictionary")
        set templates  = createobject("scripting.dictionary")
        set datasource  = createobject("adodb.recordset")
        alternate   = 0
        pagestart = timer
    end sub

    private sub class_terminate   ' 设置 terminate 事件。
        set itemstyle  = nothing
        set headsort  = nothing
        set headitemstyle = nothing
        set columns   = nothing
        set datasource  = nothing
    end sub

    private sub inittable()
        'set fieldsnum    = datasource.fields.count
        'set rowsnum     = datasource.recordcount
        if columns.count = 0 then
            for i = 0 to datasource.fields.count -1
                columns.add datasource.fields(i).name,datasource.fields(i).name
                response.write(datasource.fields(i).name)
            next
        end if

        if isempty(style) and isempty(normalstyle) then
            defaultstyle = 1
        else
            defaultstyle = style
        end if

        curpage = cint(request.querystring("page"))
        if curpage = "" then
            curpage = 1
        end if

        if pagesize = empty then
            pagesize = 10
        end if

        select case defaultstyle
        case 1
            style     ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"
            alternate    = 1
            headstyle    = "height=25 style=""background-color:#006699;color:#ffffff"""
            alternatestyle   = "bgcolor=#ffffff height=25"
            normalstyle    = "height=25 bgcolor=#f5f5f5"
            allowpageing   = true
            tbgrid1.pageingstyle = "bgcolor='#f5f5f5' align='right'"
        case 2
            style     ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"
            alternate    = 0
            headstyle    = "height=25 style=""background-color:#ffffff"""
            alternatestyle   = "bgcolor=#ffffff height=25"
            normalstyle    = "height=25 bgcolor=#ffffff"
        case else
        end select
    end sub

    public sub addtemplate(byval columnname,byval template)
        columns.add columnname,columnname
        templates.add columnname,template
    end sub

    public sub show()
        inittable()
        dim tablestr
        dim tdstart,tdend,tbstyle,tbcontent
        dim currow
        dim clm
        dim regex,match,matches
        tablestr = "" & vbcrlf
        'draw table head
        response.write(tablestr)
        response.write("")
        for each clm in columns.keys()
            tbstyle = headstyle & " " & headitemstyle(clm)
            tdstart = ""
            tdend = "" & vbcrlf

            response.write(tdstart)
            '加入表头排序功能
            'code by redsun
            'date:2005-1-17
            if headsort(clm) then
                response.write sort(clm,columns(clm))
            else
                response.write(columns(clm))
            end if
            response.write(tdend)
        next
        response.write("" & vbcrlf)

        'draw table items
        currow = 1
        if allowpageing <> empty then
            datasource.pagesize = pagesize
        else
            datasource.pagesize = datasource.recordcount
        end if

        if curpage < 1 then
            datasource.absolutepage = 1
        end if

        if curpage >= datasource.pagecount then
            datasource.absolutepage = datasource.pagecount
        end if

        if curpage >= 1 and curpage <= datasource.pagecount then
            datasource.absolutepage = curpage
        end if

        for currow = 1 to datasource.pagesize
            if datasource.eof then
                exit for
            end if

            response.write("")
            for each clm in columns.keys()
                if alternate = 0 then
                    tbstyle = normalstyle & " " & itemstyle(clm)
                else
                    if currow mod 2 = 0 then
                        tbstyle = alternatestyle & " " & itemstyle(clm)
                    else
                        tbstyle = normalstyle & " " & itemstyle(clm)
                    end if
                end if

                tdstart = ""
                tdend = "" & vbcrlf

                if templates(clm) = empty then
                    tbcontent = datasource(clm)
                else
                    tbcontent = templates(clm)
                    set regex = new regexp
                    regex.pattern= "{[a-za-z0-9_-]+}"
                    regex.ignorecase = true
                    regex.global = true
                    set matches=regex.execute(templates(clm))
                    for each match in matches
                        on error resume next
                        tbcontent = replace(tbcontent,match.value,datasource(mid(match.value,2,len(match.value)-2)),1) 
                    next

                end if

                response.write(tdstart)
                response.write(tbcontent)
                response.write(tdend)
            next
            response.write("" & vbcrlf)

            datasource.movenext
        next

        'draw pageing row
        if datasource.pagecount > 1 and lcase(pageingstyle) <> "none" then
            dim i,endpage,startpage
            response.write("")
            response.write("")
            '改进分页功能
            'code by redsun
            'date:2005-1-17
            if curpage>4 then
                if curpage+2
相关文章