[*] Hello Snoopy

.NET and Flash Blog
改成JS语法的ASP DataGrid

习惯用JScript写asp,所以改了一下

<%
function DataGrid()
{
    
// constructor
    this.DataSource;    //数据源
    this.Style = 'border=1 width=100% cellpadding=2 cellspaccing=0 borderColor=#000000 style="Border-collapse:collapse;font-size:9pt"';//表格总风格
    this.HeadStyle   = 'bgcolor="#CCCCCC"';//表头风格
    this.HeadItemStyle;    //表头单独风格
    this.ItemStyle;    //单元格独立风格
    this.Columns;    //需要显示的列元素
    this.Alternate        = true;//是否交替风格
    this.AlternateStyle = 'bgcolor="#EEEEEE"';//偶数行风格
    this.NormalStyle    = 'height="20"';//正常风格
    
    
this.PageSize;    //页大小
    this.CurPage = Apps.getQuery("page");    //当前页
    this.AllowPageing;    //是否分页
    this.PageingStyle;    //页数风格

    
var Templates;    //自定义单元项
    
    
this.ItemStyle        = new ActiveXObject("Scripting.Dictionary");
    
this.HeadItemStyle    = new ActiveXObject("Scripting.Dictionary");
    
this.Columns        = new ActiveXObject("Scripting.Dictionary");
    
this.Templates      = new ActiveXObject("Scripting.Dictionary");
    
}
DataGrid.prototype.clear 
= function()
{
    
delete this.Item;
    
delete this.HeadItemStyle;
    
delete this.Columns;
};
DataGrid.prototype.InitTable 
= function()
{
    
var FieldsNum = this.DataSource.Fields.Count;
    
    
if(this.Columns.Count == 0)
    {
        
for(var i=0;i<FieldsNum;i++)
        {
            
this.Columns.add(this.DataSource.Fields(i).Name,DataSource.Fields(i).Name);
            Apps.print(
this.DataSource.Fields(i).Name);
        }
    }
    
if(this.PageSize == undefined)    this.PageSize = 10;
};

DataGrid.prototype.AddTemplate 
= function(ColumnName,Template)
{
    
this.Columns.add(ColumnName,ColumnName);
    
this.Templates.add(ColumnName,Template);
};

DataGrid.prototype.CreateGrid 
= function()
{
    
this.InitTable();
    
var iRowsNum = this.DataSource.RecordCount;

    
var i,j;
    
var tableStr;
    
var tdStart,tdEnd,tbStyle,tbContent;
    
var curRow;
    
var clm;
    
var regEx,Match,Matches;

    
var aKeys = (new VBArray(this.Columns.Keys())).toArray();
    
var styleHeader,styleItem; 
    
var itemReplace;
       
    tableStr 
= "<table " + this.Style + ">";

    
//Draw Table Head
    Apps.print(tableStr);
    Apps.print(
"<tr>");
    

    
for(i in aKeys)
    {
        clm 
= aKeys[i];
        styleHeader 
= (typeof(this.HeadItemStyle(clm)) != "undefined")? this.HeadItemStyle(clm) : "";
        tbStyle 
= this.HeadStyle + " " + styleHeader;
        tdStart 
= "<td " + tbStyle + ">";
        tdEnd    
= "</td>";
        
        Apps.print(tdStart);
        Apps.print(
this.Columns(clm));
        Apps.print(tdEnd);
    }
    Apps.print(
"</tr>")

    
//Draw Table items
    curRow = 1
    
if(this.AllowPageing != undefined)
        
this.DataSource.PageSize = this.PageSize;
    
else
        
this.DataSource.PageSize = iRowsNum;
    
    
if(this.CurPage == "")
        
this.CurPage = 1;

    
if(this.CurPage < 1)
        
this.DataSource.AbsolutePage = 1;

    
if(this.CurPage >= this.DataSource.PageCount)
        
this.DataSource.AbsolutePage = this.DataSource.PageCount;

    
if(this.CurPage >= 1 && this.CurPage <= this.DataSource.PageCount)
        
this.DataSource.AbsolutePage = this.CurPage;

    
for(curRow = 1; curRow <= this.DataSource.PageSize; curRow++)
    {
        
if(this.DataSource.EOF) break;
                    
        Apps.print(
"<tr>");
        
for(i in aKeys)
        {
            clm 
= aKeys[i];
            styleItem 
= (typeof(this.ItemStyle(clm)) != "undefined")?this.ItemStyle(clm): "";
            
if(!this.Alternate)
                tbStyle 
= this.NormalStyle + " " + styleItem;
            
else
            {
                
if(curRow % 2 == 0)
                    tbStyle 
= this.AlternateStyle + " " + styleItem;
                
else
                    tbStyle 
= this.NormalStyle + " " + styleItem;
            }
            
            tdStart 
= "<td " + tbStyle + ">";
            tdEnd 
= "</td>";
            
    
            
if(typeof(this.Templates(clm)) == "undefined")
                tbContent 
= this.DataSource(clm);
            
else
            {
                tbContent 
= this.Templates(clm);
                regEx 
= /{[da-zA-Z_-]+}/gi;

                matches 
= this.Templates(clm).match(regEx);
                
var itemReplace;
                
for(j=0;j<matches.length;j++)
                {
                    match 
= new String(matches[j]);
                    itemReplace 
= match.replace(/{|}/g,"");
                    
//Apps.print(match);
                    //Apps.print(itemReplace);
                    tbContent = tbContent.replace(match,this.DataSource(itemReplace));
                }
            }
            Apps.print(tdStart);
            Apps.print(tbContent);
            Apps.print(tdEnd);
        }
        Apps.print(
"</tr>");
        
this.DataSource.MoveNext();
    }

    
//Draw Pageing Row
    if(this.DataSource.PageCount > 1)
    {
        Apps.print(
"<tr>");
        Apps.print('
<td colspan="' + this.Columns.Count + '" ' + this.PageingStyle + '>');
        
for(i=1;i<=this.DataSource.PageCount;i++)
        {
            
if(i != this.CurPage)
                Apps.print('
<a href="' + Request.ServerVariables("SCRIPT_NAME") + '?page=' + i + '">' );
            Apps.print(i);
            
if(i != this.CurPage)
                Apps.print(
"</a>&nbsp;");
            Apps.print(
" ")
        }
        Apps.print(
"</td></tr>");
    }

    
//Draw Table end
    Apps.print("</table>");
}
%>

使用:
<%
 var db = new DB();
 
 var rs = db.getRs("select product_id,class_id,name,describle,detail from product",1);  
 
 var grd = new DataGrid();
 
 grd.DataSource = rs;
 //grd.PageSize = 8 ;
 //grd.AllowPageing = true;

 grd.Columns.Add("name","用户名");
 grd.Columns.Add("describle","描述");
 grd.Columns.Add("detail","详细");

 grd.AddTemplate("修改",new Link("{name}","aaa.asp?id={product_id}&class={class_id}").toString());
 grd.PageingStyle = 'style="font-size:11pt;text-align:right"';

 
 grd.CreateGrid();
 
 grd.clear();
 db.clear();
 %>
DB是自定义数据访问对象,getRs()返回一个Recordset对象

posted on 2004-07-21 15:31  HelloSnoopy  阅读(619)  评论(0编辑  收藏  举报