超酷的通用分页显示控制 (四) 统一记录显示

发表于:2007-06-30来源:作者:点击数: 标签:
追求是永无止境的。 我们现在有时间来考虑一个更深层次的问题,就是记录的显示。 记录的显示是一个古老的话题,通常的做法是: 1、在需要显示记录的 ASP 文件中,用 Dreamweaver 或者 Frontpage 画出表格,以及列标题 2、在 ASP 文件中用循环显示记录 3、由于
追求是永无止境的。

我们现在有时间来考虑一个更深层次的问题,就是记录的显示。
记录的显示是一个古老的话题,通常的做法是:
1、在需要显示记录的 ASP 文件中,用 Dreamweaver 或者 Frontpage 画出表格,以及列标题
2、在 ASP 文件中用循环显示记录
3、由于网站风格调整,因此在 ASP 文件中需要作相应修改


假定我们有20个需要分页的 ASP 页面(对于一个较大的网站来说,这不算什么),都要这样做,实在麻烦,特别是遇到网站改版--而这种改版仅仅只是网站风格改变,内容并没有改变,这种情况大约 3-6 个月会有一次。

显然应该有更好的方法。


现在我们来作这样的考虑:
对于一个网站来说,它的风格是一定的,记录显示的页面也是一定的。我们可以先设置好页面的风格(用 Dreamweaver 或者 Frontpage 画出表格),然后放入函数中,用语句来生成表格。至于列表题,以及要显示的字段、字段的长度、对齐方式、是否显示链接等,全部用参数传入该函数,用该函数来生成数据表格。


请看 sample4.asp




<一> 需要分页的 ASP 文件

sample4.asp


<!--#include file="../inc/functions.inc"-->
<%
    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 定义表名
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theTableName= "addressbook"

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 查询条件
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theQueryField    = "fld" & theTableName & "_nickname"‘’ 查询字段,完整名字
    theQueryTitle    = "昵称"                            ‘’ 字段显示标题
    theQueryTable    = "vw" & theTableName                ‘’ 字段所在的表,完整名字

    theQueryClass    = theTableName & "_class"            ‘’ 类别表名,去掉 tbl、vw 前缀
    theClassId        = c2int(request("classid"))            ‘’ 当前类别号

    ‘’ 如果是查询模式,则构造模糊查询语句
    if request("mode") = "query" then
        %><!--#include file="../inc/control/query_result.inc"--><%
    else
    ‘’ 否则忽略
        theQueryCon = "1>0"
    end if

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 限制条件
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theLimitCon    = "fld" & theTableName & "_userid=" & Session("userid")

    if theClassId > 0 then
        theLimitCon = theLimitCon & " and fld" & theQueryClass & "id=" & theClassId
    end if

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 构造 SQL 语句
    ‘’
    ‘’//////////////////////////////////////////////////////////

    uSQL = "select * from " & theQueryTable & " where ( " & theQueryCon & " ) and ( " & theLimitCon & " ) "

%>
<!--#include file="../inc/control/navigator_init.inc"-->

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../default.css" type="text/css">
</head>

                      <!-- 你的 HTML 代码//-->

                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
<!--查询控制-->
<!--#include file="../inc/control/query.inc"-->
                          </td>
                          <td>
<!--导航控制-->
<!--#include file="../inc/control/navigator.inc"-->
                          </td>
                        </tr>
                      </table>

                      <!-- 你的 HTML 代码//-->

                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
    <%
    ‘’ 定义记录集显示方式

    theViewType    = "view"
    theViewASP    = "sample_view.asp"
    theFields    = "fld" & theTableName & "_id,fld" & theTableName & "_class_title,fldaddressbook_nickname"
    theTitles    = "序号,类别,昵称"
    theAligns    = ",,m,"                ‘’ 对齐方式                :m 中 / r 右 / 缺省:左
    theTypes    = ",,,"                    ‘’ 字段类型                :l 逻辑 / 缺省:字符串
    theWidths    = "6%,14%,80%"            ‘’ 字段宽度                :
    theLenths    = "0,8,0"                ‘’ 字段可容纳的字符长度    :0 不指定
    theLinkASP    = "0,0,1"                ‘’ 是否显示链接            :0 不显示 / 1 显示
    theEnterLine= "0,0,0"                ‘’ 是否换行显示            :0 不换行 / 1 换行
    theIsAdd    = 0                        ‘’ 记录集序号显示方式    :0 递减/ 1 递增
    listRecords theFields,theTitles,theAligns,theTypes,theWidths,theLenths,theLinkASP,theEnterLine,theIsAdd,theViewType
    %>
                          </td>
                        </tr>
                      </table>
<!--#include file="../inc/control/listrecordscon.inc"-->

                      <!-- 你的 HTML 代码//-->

</body>
</html>



<二> 函数集:

将以下函数加入到 function.inc 中

<%
‘’--------------------------------------------------------
‘’Name:            ListRecords
‘’Argument:        uFields                    列元素数组,第一个必须是 ID 字段
‘’                uTitles                    列标题
‘’                uWidth                    列宽度数组
‘’                uLenth                    每列可容纳的字符串数
‘’                uLinkasp                链接地址
‘’                uEnterLine                是否换行显示
‘’                uIsAdd                    序号显示方式:    0 递减 / 1 递增
‘’                uViewType                查看方式
‘’Return:
‘’Description:   列出记录集
‘’                rs、conn、theCurrentPageNum 为全局变量,在 navigator_begin.inc 中定义
‘’--------------------------------------------------------

Sub listRecords(byval uFields, byval uTitles, byval uAligns, byval uTypes, byval uWidths, byval uLenths, byval uLinkasp, byval uEnterLine,byval uIsAdd,byval uViewType)
    Dim uNum, uMod, i, j, uStrTemp, uStr, uHeight, uLen, uLeft, uCols
    Dim uTmp1, uTmp2, uRecName, uFirstLine

    uHeight            = 20        ‘’ 行高
    uWidth_Num        = 40        ‘’ 序号列宽度

    uFields            = split(uFields,",")
    uTitles            = split(uTitles,",")
    uAligns            = split(uAligns,",")
    uTypes            = split(uTypes,",")
    uWidths            = split(uWidths,",")
    uLenths            = split(uLenths,",")
    uLinkasp        = split(uLinkasp,",")
    uCols            = ubound(uFields)
    uEnterLine        = split(uEnterLine,",")

    uStr            = ""

    ‘’ 隐含域 theActionScript,保存当前页面的文件名,但不包括路径和扩展名 .asp
    uStr = uStr & "<input type=""hidden"" name=""theActionScript"" value=""" & theActionScript & """>" & vbCrLf
    ‘’ 隐含域 theScript,保存当前页面的完整文件名
    uStr = uStr & "<input type=""hidden"" name=""theScript"" value=""" & theScript & """>" & vbCrLf

    ‘’ 标题栏

    uStr = uStr & "<table width=‘’100%‘’ border=‘’0‘’ cellspacing=‘’1‘’ cellpadding=‘’3‘’ bgcolor=‘’#000000‘’>" & vbCrLf
    uStr = uStr & "  <tr bgcolor=‘’#FFFFFF‘’> " & vbCrLf
    uStr = uStr & "    <td>" & vbCrLf
    uStr = uStr & "      <table width=‘’100%‘’ border=‘’0‘’ cellspacing=‘’0‘’ cellpadding=‘’0‘’>" & vbCrLf
    uStr = uStr & "        <tr>" & vbCrLf

    for i = 0 to UBound(uFields)
        uStr = uStr & "          <td width=‘’" & uWidths(i) & "‘’>" & uTitles(i) & "</td>" & vbCrLf
    next

    uStr = uStr & "        </tr>" & vbCrLf
    uStr = uStr & "      </table>" & vbCrLf
    uStr = uStr & "    </td>" & vbCrLf
    uStr = uStr & "  </tr>" & vbCrLf
    uStr = uStr & "  <tr bgcolor=‘’#FFFFFF‘’> " & vbCrLf
    uStr = uStr & "    <td>" & vbCrLf
    uStr = uStr & "        <table width=100% border=0 cellspacing=0 cellpadding=0>" & vbCrLf

    if rs.recordcount = 0 then
        j = 1
        uStr = uStr & "<tr height=" & uHeight & "><td cols=" & uCols & ">没有记录。</td></tr>" & vbCrLf
    else
        ‘’ 内容列表
        uFirstLine = 1
        For j = 1 To rs.pagesize

            if ( j mod 2 ) = 0 then
                uBgColor = " bgcolor=#efefef"
            else
                uBgColor = ""
            end if

            ‘’ 计算序号
            if uIsAdd = 1 then
                ‘’升序排列
                uNum = ( theCurrentPageNum - 1 ) * rs.pagesize + j
            else
                ‘’倒序排列
                uMod = rs.recordcount Mod rs.pagesize
                If uMod = 0 Then
                    uMod = rs.pagesize
                End If

                uNum = (rs.pagecount - theCurrentPageNum) * rs.pagesize + uMod - j + 1
            end if

            ‘’ 行开始
            uStr = uStr & "<tr valign=top height=" & uHeight & uBgColor & ">" & vbCrLf

            if uFirstLine = 1 then
                uWidthStr    = " width=" & uWidth_Checkbox
            else
                uWidthStr    = ""
            end if

            if uFirstLine = 1 then
                uWidthStr    = " width=" & uWidths(0)
            else
                uWidthStr    = ""
            end if

            uStr = uStr & "<td " & uWidthStr & ">" & uNum & "</td>" & vbCrLf

            ‘’ 显示指定字段

            For i = 1 to ubound(uFields)
                ‘’ 根据字段类型,返回不同的处理结果
                uStrTemp = getValue(rs(uFields(i)),uTypes(i))
                uLen = cint(uLenths(i))

                if uFirstLine = 1 then
                    uWidthStr    = " width=" & uWidths(i)
                else
                    uWidthStr    = ""
                end if

                if uAligns(i) <> "" then
                    uAlignStr = " align=" & getAlign(uAligns(i))
                else
                    uAlignStr = ""
                end if
                ‘’ 只显示指定长度的内容
                if uLen > 0 then
                    if lenX(uStrTemp) > uLen then
                        ‘’ 是否折行显示
                        if uEnterLine(i) = 1 then
                            uStrTemp = paragraph(uStrTemp,uLen,"<br>")
                        else
                            uStrTemp = leftX(uStrTemp,uLen) & "..."
                        end if
                    end if
                end if

                ‘’ 添加链接
                ‘’ 使用 return false ,当用户单击链接时,当前页面光标将保持原状,否则将回到页面起始位置

                If cint(uLinkasp(i)) = 1 Then
                    uStrTemp = "<a href=""#"" onclick=""javascript:" & uViewType & "(" & rs(uFields(0)) & "," & theCurrentPageNum & ");return false;"" >" & uStrTemp & "</a>"
                End If

                uStr = uStr & "<td" & uWidthStr & uAlignStr & ">" & uStrTemp & "</td>" & vbCrLf
            Next

            ‘’ 行结束
            uStr = uStr & "</tr>" & vbCrLf

            uFirstLine = 0
            rs.MoveNext

            If rs.EOF Then
                Exit For
                rsClose rs
                cnClose conn
            End If
        Next

    end if

    ‘’ 剩余的行数用空行填充
    uLeft = rs.pagesize - j - 1

    ‘’ 填充
    for i = 0 to uLeft
        uStr = uStr & "<tr height=" & uHeight & "><td cols=" & uCols & "></td></tr>" & vbCrLf
    next
    
    ‘’ 表结束
    uStr = uStr & "        </table>" & vbCrLf
    uStr = uStr & "    </td>" & vbCrLf
    uStr = uStr & "  </tr>" & vbCrLf
    uStr = uStr & "</table>" & vbCrLf

    ‘’ 显示表格
    response.write uStr
End Sub

‘’--------------------------------------------------------
‘’Name:            lenX
‘’Argument:        uStr
‘’Return:
‘’Description:    返回字符串的长度,1个中文字符长度为2
‘’--------------------------------------------------------

function lenX(byval uStr)
    dim theLen,x,testuStr
    theLen = 0

    for x = 1 to len(uStr)
        testuStr = mid(uStr,x,1)
        if asc(testuStr) < 0 then
            theLen = theLen + 2
        else
            theLen = theLen + 1
        end if
    next
    lenX = theLen
end function

‘’--------------------------------------------------------
‘’Name:            leftX
‘’Argument:        uStr        待处理的字符串
‘’                uLen        要截取的长度
‘’Return:
‘’Description:    返回指定长度的字符串,1个中文字符长度为2
‘’--------------------------------------------------------

function leftX(byval uStr,byval uLen)
    dim i,j,uTestStr,theStr

    leftX = ""
    j = 0

    for i = 1 to len(uStr)
        uTestStr= mid(uStr,i,1)
        theStr    = theStr & uTestStr
        if asc(uTestStr) < 0 then
            j = j + 2
        else
            j = j + 1
        end if
        if j >= uLen then exit for
    next
    leftX = theStr
end function

‘’--------------------------------------------------------
‘’Name:            rightX
‘’Argument:        uStr        待处理的字符串
‘’                uLen        要截取的长度
‘’Return:
‘’Description:    返回指定长度的字符串,1个中文字符长度为2
‘’--------------------------------------------------------

function rightX(byval uStr,byval uLen)
    dim i,j,uTestStr

    rightX = ""
    j = 0

    for i = len(uStr) to 1 step -1
        uTestStr = mid(uStr,i,1)
        rightX = rightX & uTestStr
        if asc(uTestStr) < 0 then
            j = j + 2
        else
            j = j + 1
        end if
        if j >= uLen then exit for
    next
end function

‘’--------------------------------------------------------
‘’Name:            getValue
‘’Argument:
‘’Return:
‘’Description:    转换字段类型
‘’--------------------------------------------------------

Function getValue(byval uValue,byval uType)
    select case uType
    case "l"        ‘’ 逻辑字段
        if uValue = true then getValue = "是" else getValue = "否" end if
    case else
        getValue = uValue
    end select
End Function

‘’--------------------------------------------------------
‘’Name:            getAlign
‘’Argument:
‘’Return:
‘’Description:    对齐方式
‘’--------------------------------------------------------

Function getAlign(byval uType)
    select case uType
    case "m"
        getAlign = "middle"
    case "r"
        getAlign = "right"
    case else
        getAlign = "left"
    end select
End Function

‘’--------------------------------------------------------
‘’Name:            paragraph
‘’Argument:        uStr            字符串
‘’                uLen            长度
‘’                uSplit            换行字符
‘’Return:
‘’Description:    将字符串折行显示
‘’--------------------------------------------------------

Function paragraph(byval uStr, byval uLen,byval uSplit)

    dim uTemp,uTestStr

    uTemp    = ""
    j = 0

    for i = 1 to len(uStr)
        uTestStr= mid(uStr,i,1)
        uTemp    = uTemp & uTestStr
        if asc(uTestStr) < 0 then
            j = j + 2
        else
            j = j + 1
        end if
        if j >= uLen then
            uTemp    = uTemp & uSplit
            j        = 0
        end if
    next
    
    ‘’ 处理回车换行
    uTemp        = replace(uTemp,vbCrLf,"<br>")
    paragraph    = uTemp

End Function

%>



<三> 和记录显示有关的客户端代码:

listrecordscon.inc


<script language="javascript">

function edit(id,pn)
{
    window.open(‘’<%=theActionScript%>_edit.asp?id=‘’+id+"&pn="+pagenum, ‘’‘’, ‘’width=<%=theWinW%>,height=<%=theWinH%>,resizable=0,scrollbars=yes‘’);
}

function view(id,pn)
{
    window.open(‘’<%=theActionScript%>_view.asp?id=‘’+id+"&pn="+pagenum, ‘’‘’, ‘’width=<%=theWinW%>,height=<%=theWinH+12%>,resizable=0,scrollbars=yes‘’);
}

</script>

在下一篇中,我们将对记录进行添加、删除、修改等操作,更多的 javascript 代码将加入到 listrecordscon.inc 中。

原文转自:http://www.ltesting.net