欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

我用ASP写的m行n列的函数,动态输出创建TABLE行列

程序员文章站 2023-12-03 12:10:16
没有用正常的循环tr内循环td的方法,所以就没有计算最后一页的记录集数量。 但当记录集不足时也同样能够自动补空行空列,如有不足请指出,谢谢。 使用方法很简单: 复制代码 代...
没有用正常的循环tr内循环td的方法,所以就没有计算最后一页的记录集数量。
但当记录集不足时也同样能够自动补空行空列,如有不足请指出,谢谢。
使用方法很简单:

复制代码 代码如下:

<% 
showdata("查询语名") 
call pagehead(行数,列数) 
'------------------------------------------------------------------------------- 
'可以把下面的response.write()写到函数里面,看个人的爱好了。 
response.write(formatcrtable(表格宽度,表格高度)) 
response.write(pagefoot()) 
call closedata() 
%> 

函数如下: 

 

复制代码 代码如下:

<% 
'------------------------------------------------------------------------------- 
'copyright 2005 by jorkin.all rights reserved 
'------------------------------------------------------------------------------- 
public rs 
public pageno,cols,rows,maxpageno,maxredcount,maxpagesize 
'------------------------------------------------------------------------------- 
public function showdata(sqlstrings) 
  set rs=server.createobject("adodb.recordset") 
  rs.open sqlstrings,oconn,1,3 
end function 
public function closedata() 
  rs.close 
  set rs=nothing 
end function 
'初始化分页 
'maxrows =最大行数 
'maxcols =最大列数 
'------------------------------------------------------------------------------- 
public function pagehead(maxrows,maxcols) 
'------------------------------------------------------------------------------- 
  rows=maxrows 
  cols=maxcols 
  maxpagesize=1 
  maxredcount=0 
  maxpageno=0 
  pageno=0 
'------------------------------------------------------------------------------- 
  maxredcount = rs.recordcount 
  if maxredcount>0 then 
  maxpagesize = rows*cols 
  maxpageno = cint((maxredcount+maxpagesize-1)/maxpagesize) 
  rs.pagesize = maxpagesize 
  pageno=request("pageno") 
  if isempty(pageno) or not isnumeric(pageno) or cint(pageno)<1 or cint(pageno)>rs.pagecount then 
   pageno=1 
  end if 
  rs.absolutepage=pageno 
  end if 
  on error resume next 
end function 
'分页函数尾 
'无参数 
'------------------------------------------------------------------------------- 
public function pagefoot() 
  strr="<table width=""500"" border=""0"" cellpadding=""0"" cellspacing=""0"">" & vbcrlf 
  strr=strr&"  <tr>" & vbcrlf 
  strr=strr&"    <td valign=""top""><div align=""right"">" 
'大于1页才会显示首页和上一页(可选) 
' if pageno>1 then 
   strr=strr&"<a href=""?pageno=1"" title=""首页""><font face = webdings>9</font></a>" 
   strr=strr&" " 
   strr=strr&"<a href=""?pageno="&pageno-1&""" title=""上一页""><font face = webdings>7</font></a>" 
' end if 
  strr=strr&" "&pageno&" " 
'小于最大页数才会显示下一页和尾页(可选) 
' if pageno<maxpageno then 
   strr=strr&"<a href=""?pageno="&pageno+1&""" title=""下一页""><font face = webdings>8</font></a>" 
   strr=strr&" " 
   strr=strr&"<a href=""?pageno="&maxpageno&""" title=""""><font face = webdings>:</font></a>" 
' end if 
  strr=strr&"    " 
  strr=strr&(pageno-1)*maxpagesize+1&"/"&maxredcount&"条记录" 
  strr=strr&"    " 
  strr=strr&pageno&"/"&maxpageno&"页" 
  strr=strr&"</div></td>" & vbcrlf 
  strr=strr&"  </tr>" & vbcrlf 
  strr=strr&"</table>" 
  pagefoot=strr 
end function 
'进行行列格式化函数 
'tablewidth  =表格宽度 
'tableheight =表格高度(因浏览器不同可能无效) 
'------------------------------------------------------------------------------- 
public function formatcrtable(tablewidth,tableheight) 
  dim i,strr 
  i=0 
  strr="" 
  strr=strr&"<table width="""&tablewidth&"""  border=""0"" cellspacing=""0"" cellpadding=""0"">" & vbcrlf 
  strr=strr&"  <tr>" & vbcrlf 
  if maxredcount>0 then 
   do while i<maxpagesize 
    i=i+1 
    if not rs.eof then 
     strr=strr&"    <td width="""&tablewidth/cols&""" height="""&tableheight/rows&""">有记录则进行输出</td>" & vbcrlf 
     rs.movenext 
    else 
     strr=strr&"    <td width="""&tablewidth/cols&""" height="""&tableheight/rows&""">记录集不足时补余</td>" & vbcrlf 
    end if 
    if i mod cols = 0 then 
     strr=strr&"  </tr>" & vbcrlf 
     strr=strr&"  <tr>" & vbcrlf 
    end if 
   loop 
  else 
   strr=strr&"<td height="""&tablewidth&""" valign=""top"">目前没有记录集</td>" 
  end if 
  strr=strr&"  </tr>" & vbcrlf 
  strr=strr&"</table>" & vbcrlf 
  formatcrtable=strr 
end function 
%> 
代码还有很多不足,而且写的也不是很严谨,见笑了。
以后可以会改为class