ASP实现的一个DataGrid类α版

发表于:2007-05-25来源:作者:点击数: 标签:
VB SCRIPT'>%@LANGUAGE="VBSCRIPT " % %Option Explicit % \!--#include file="../includes/library.asp"-- \!--#include file="../includes/runtime.asp"-- \!--#include file="../Manager/char.asp"-- \%=lan_getText(session("userLanguage"),0)% script

<%@LANGUAGE="VBSCRIPT" %>
<%Option Explicit %>
<\!--#include file="../includes/library.asp"-->
<\!--#include file="../includes/runtime.asp"-->
<\!--#include file="../Manager/char.asp"-->
<\%=lan_getText(session("userLanguage"),0)%>
<script>
function selectAll(obj)
{
  var top=obj;
  while(true)
  {
     var top=top.parentElement;
  if(top==null)
  {
    break;
  }
  if(top=="undefined")
  {
    top=null;
    break;
  }
  if(top.tagName=="TABLE")
  {
   break;
  }
  }
  var i;
  var j;
  if(top!=null)
  {
    for(i=0;i<top.rows.length;i++)
 {
      for(j=0;j<top.rows[i].cells[0].children.length;j++)
   {
       var temp=top.rows[i].cells[0].children[j];
    if(temp.tagName=="INPUT")
    {
       if(temp.type=="checkbox")
    {
        temp.checked=obj.checked;
    }
    }
   }
 }
  }
}
</script>
<%
Dim nbdg, errLog
Set nbdg = new NewsBriefDataGrid
Dim pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1


pageStringTemplate = "<table width=""100%""><tr><td width=""80%"">{0}</td><td>{1}</td></tr></table>"
pageStringTemplate0         = "合计<font color=""red"">{RowCount}</font>条记录第<font color=""red"">{CurrentPage}</font>页/合计<font color=""red"">{PageCount}</font>页"
pageStringTemplatePrevious0  = "上一页&nbsp;"
pageStringTemplateNext0      = "下一页&nbsp;"
pageStringTemplatePrevious1  = "<a href=""?page={Previous}"">上一页</a>&nbsp;"
pageStringTemplateNext1      = "<a href=""?page={Next}"">下一页</a>&nbsp;"


nbdg.listNewsBriefs Response, pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, "page", -1, session("userLanguage"), errLog
%>


 


 


 



<%
Class NewsBriefDataGrid


'@param Res Response
'@param newsType 新闻类型
'@param languageId
'@errLog
Public Function listNewsBriefs(Res, pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, PAGE_PARAM_NAME, newsType, lang, errLog)
  Dim ret
  Dim currentPage '当前页码,从1开始计数
  Dim pageSize '页大小
  currentPage = Request.QueryString(PAGE_PARAM_NAME)
  If currentPage < 1 Then
    currentPage = 1
  End if
  pageSize = 10
 
  '定义新闻操作句柄
  dim objNews
  set objNews = Server.CreateObject("SanguineRecore.clsNews")
 
  dim count
  objNews.getNewsCount count, -1, "zh_CN", errLog
  ret = ret & (page(pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, count, pageSize, PAGE_PARAM_NAME, currentPage))
 
  '获取新闻并打印
  Dim rows
  Dim isOK
  'isOK = objNews.getNewsBriefs(CInt(startRow), CInt(pageSize), newsType, rows, "0",errLog)
  isOK = objNews.getNewsBriefs ((currentPage - 1) * pageSize, pageSize, newsType, rows, lang, errLog)
  'Res.Write(isOK)
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ' 一个使用数据网格的例子
  ' 下列属性为必选属性
  ' DataSource   数据源
  ' RowCount     行数
  ' ColumnCount  列数
  ' Columns      列绑定
  ' HeaderTexts  表头文本
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  dim dg
  '初始化数据网格
  set dg = new DataGrid
  '设置网格属性
  dg.tableAttribute = "cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"""
  '设置表格头属性
  dg.thAttribute = "bgcolor=""#EEEEEE"" style=""font:normal"""
  '设置表行属性
  dg.TrAttribute = "onmouseover=""this.style.backgroundColor='#EFEFEF'"" onmouseout=""this.style.backgroundColor='"""
  '设置数据源,为一二维数组
  dg.DataSource = rows
  '设置行数,默认为数据源的行数
  If IsArray(rows) Then
    dg.RowCount = UBound(rows)
  Else
    dg.RowCount = 0
  End if
  '列数,默认为Columns属性的长度
  dg.ColumnCount = 6
  '设置列绑定
  dg.Columns = array( _
                      "<input type=""checkbox"" name=""id"" value=""{TEXT[0]}"">", _
                      "{TEXT[0]}", _
                      "{HTML[2]}", _
                      "{TEXT[4]}", _
                      "{TEXT[3]}", _
                      "<a href=""edit.asp?id={TEXT[0]}"">编辑</a>"_
                    )
  'Response.Write(rows(0)(1))
  '设置列宽度
  dg.ColumnWidths = array("5%","5%", "20%", "30%", "20%", "")
  '设置列文字对齐方式
  dg.ColumnAligns = array("center", "center", "center", "center", "center", "center")
  '设置表头文字,支持HTML格式
  '请把下面的汉字修改成类似:lan_getText(languageId,32)
  dg.HeaderTexts = array("<input type=""checkbox"" onclick=""selectAll(this)"">", "ID", "新闻标题", "新闻摘要", "输入时间", "操作")
  '设置表头文字对齐属性,默认对齐方式为center
  dg.HeaderTextAligns = array("center", "center", "center", "center", "center", "center")
 
  ret = ret & dg.ToTable()
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ret = ret & "<input type=""submit"" value=""删除"">"
 
  ret = ret & "</form>"
  Res.Write(ret)
'''''''''''''''''''''''''''''' 
End Function


'
'@param rowCount 总行数
'@param pageSize 每页显示的行数
'@param PAGE_PARAM_NAME page的参数名,用在QueryString中
'@param currentPage 当前页码
'@return 分页字符串
Public Function page(pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, rowCount, pageSize, PAGE_PARAM_NAME, currentPage)
  rowCount = CInt(rowCount)
  pageSize = CInt(pageSize)
  currentPage = CInt(currentPage)
  Dim ret
  Dim pageCount, i, tmp
  tmp = rowCount / pageSize
  pageCount = Fix(tmp)
  If(tmp > pageCount) Then
    pageCount = pageCount + 1
  End If
  Dim p1, p2
  p1 = ""
  'p1 = p1 &  "合计<font color=""red"">" & rowCount & "</font>条记录" _
  '   & "第<font color=""red"">" & currentPage & "</font>页/" _
  '   & "合计<font color=""red"">" & pageCount & "</font>页"
  p1 = pageStringTemplate0
  p1 = Replace(p1, "{RowCount}", rowCount)
  p1 = Replace(p1, "{CurrentPage}", currentPage)
  p1 = Replace(p1, "{PageCount}", pageCount)
  ''''''''''
  ''''''''''
 
  if currentPage > 1 then
    'p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & (currentPage-1) & """>" & "上一页" & "</a>&nbsp;"
    p2 = p2 & Replace(pageStringTemplatePrevious1, "{Previous}", currentPage -1)
  else
    'p2 = p2 & "上一页&nbsp;"
    p2 = p2 & pageStringTemplatePrevious0
  end if
  for i = 1 to pageCount
    if i = currentPage then
      p2 = p2 & i & "&nbsp;"
    else
      p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & i & """>" & (i) & "</a>&nbsp;"
    end if
  next
  if currentPage < pageCount then
    'p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & (currentPage+1) & """>" & "下一页" & "</a>&nbsp;"
    p2 = p2 & Replace(pageStringTemplateNext1, "{Next}", currentPage + 1)
  else
    'p2 = p2 & "下一页&nbsp;"
    p2 = p2 & pageStringTemplateNext0
  end if
  'p = "<table width=""100%""><tr><td width=""80%"">" & p1 & "</td><td>" & p2 & "</td></tr></table>"
  ret = pageStringTemplate
  ret = Replace(ret, "{0}", p1)
  ret = Replace(ret, "{1}", p2)
  page = ret
End Function


End Class
%>


 



<%
'Class DataGrid
'根据给定的列模板和数据源(二维数组)来显示数据表格
'
Class DataGrid
Private m_dataSource
'行数
Private m_rowCount
'列数
Private m_columnCount
Private m_columns
'表格属性
Private m_tableAttribute
'表头公共属性
Private m_thAttribute
'表行公共属性
Private m_trAttribute
'单元格属性
Private m_tdAttribute
'列宽
Private m_columnWidths
'列对齐格式
Private m_columnAligns
'表头文字
Private m_headerTexts
'表头文字对齐属性
Private m_headerTextAligns
'Private m_rows
'Private m_r_columns


Public Property Let DataSource(ds)
  m_dataSource = ds
End Property


Public Property Let RowCount(n)
  m_rowCount = n
End Property


Public Property Let ColumnCount(n)
  m_columnCount = n
End Property


Public Property Let Columns(cols)
  m_columns = cols
End Property


Public Property Let TableAttribute(s)
  m_tableAttribute = s
End Property


Public Property Let ThAttribute(s)
  m_thAttribute = s
End Property


Public Property Let TrAttribute(s)
  m_trAttribute = s
End Property


Public Property Let TdAttribute(s)
  m_tdAttribute = s
End Property


Public Property Let ColumnWidths(arrayWidth)
  m_columnWidths = arrayWidth
End Property


Public Property Let ColumnAligns(aligns)
  m_columnAligns = aligns
End Property


Public Property Let HeaderTexts(arrayText)
  m_headerTexts = arrayText
End Property


Public Property Let HeaderTextAligns(aligns)
  m_headerTextAligns = aligns
End Property


'将二维数组转换成HTML表格
'@param rows 二维数组
'@return HTML表格源码
function ToTable()
  dim ret
  ret = ""
  dim row, i, j
  ret = ret & "<table " & m_tableAttribute & ">" & vblf
  ret = ret & "<tr>" & thead() & "</tr>" & vblf
  if IsArray(m_dataSource) then
    for i = 0 to m_rowCount - 1
      row = m_dataSource(i)
      ret = ret & "<tr " & m_trAttribute & ">" & vblf
      for j = 0 to m_columnCount - 1
        ret = ret & "<td " & m_tdAttribute
        If IsArray(m_columnAligns) Then
          ret = ret & " align=""" & m_columnAligns(j) & """"
        End If
         ret = ret & ">" & bind(i, j) & "</td>"
      next
    '
    'ret = ret & "<td " & m_tdAttribute & ">" & "<input type=""checkbox"" name=""id"" value=""" & Server.HTMLEncode(row(m_r_columns(0))) & """>" & "</td>" & vblf
    '
    'for j = 0 to UBound(m_r_columns)
    '  ret = ret & "<td " & m_tdAttribute & ">" & Server.HTMLEncode(row(m_r_columns(j))) & "&nbsp;</td>" & vblf
    'next
    '
    'ret = ret & "<td " & m_tdAttribute & ">" & "<a href=""edit.asp?id=" & Server.HTMLEncode(row(m_r_columns(0))) & """>Edit</a>" & "</td>" & vblf
    '
      ret = ret & "</tr>" & vblf
    next
  end if
  ret = ret & tfoot() & vblf
  ret = ret & "</table>" & vblf
  ToTable = ret
end function


 Private Function bind(row, col)
   dim format, find, replacement, index, myVarType
   dim openB, closeB
   format = m_columns(col)
   openB = InStr(format, "{")
   dim var
   if openB < 0 then
     bind = format
   else
     closeB = InStr(format, "}")
     var = Mid(format, openB + 1,closeB - openB - 1)
     openB = InStr(var, "[")
     closeB = InStr(var, "]")
     myVarType = Mid(var, 1, openB - 1)
     'Response.Write(myVarType)
     index = CInt(Mid(var, openB+1, closeB - openB -1))
     'Response.Write(index)
     find = "{" & myVarType & "[" & index & "]}"
     If "HTML" = myVarType Then
       replacement = m_dataSource(row)(index)
     ElseIf "TEXT" = myVarType Then
       replacement = Server.HTMLEncode(m_dataSource(row)(index))
     End If
     bind = Replace(format, find, replacement)
   end if
 End Function


'显示表头
'@param 表头数组
'@return 表头HTML源码
private function thead()
  dim ret, i
  ret = ""
  for i = 0 to UBound(m_headerTexts)
    ret = ret & "<th " & m_thAttribute
    If IsArray(m_columnWidths) Then
      ret = ret & " width=""" & m_columnWidths(i) & """"
    End If
    If IsArray(m_headerTextAligns) Then
      ret = ret & " align=""" & m_headerTextAligns(i) & """"
    End if
    ret = ret & ">" & m_headerTexts(i) & "</th>" & vblf
  next
  thead = ret
end function


private function tfoot()
end function


end Class
%>

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