codeSmith实现ExtJS中Grid代码生成

news/2024/7/8 13:41:30 标签: extjs, string, image, float, null, table

新的项目中。要使用ExtJs,为了简单,写了这么一个简单的CodeSmith模板来根据数据库中的表生成Grid。

代码如下:

 

<%--  
Name:
<%=  GetClassName(SourceTable)  %>
Author: sunfishlu
Description: 
--%>
<% @ CodeTemplate Language = " C# "  TargetLanguage = " JScript "  Description = " Generates a very simple business object. "  ResponseEncoding = " UTF-8 "   %>
<% @ Property Name = " NameSpace "  Type = " String "  Category = " Text "  Default = " Sunfishlu. "  Description = " The namespace to use for this class "   %>
<% @ Property Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "  Category = " Context "  Description = " Table that the object is based on. "   %>
<% @ Assembly Name = " SchemaExplorer "   %>
<% @ Assembly Name = " System.Data "   %>
<% @ Import Namespace = " SchemaExplorer "   %>
<% @ Import Namespace = " System.Data "   %>

/* *
 * @author sunfish
 
*/

var  storeMain  =   new  Ext.data.Store({
    proxy:
new  Ext.data.HttpProxy({
        url:
' <%= GetUrlName(SourceTable) %> '
    }),
    reader:
new  Ext.data.JsonReader({
        root:
' data ' ,
        id:
' <%= SourceTable.PrimaryKey.MemberColumns[0].Name %> ' ,
        totalProperty:
' totalCount ' ,
        fields:
        [
            
<%   for  ( int  i = 0 ; i  <  SourceTable.Columns.Count;  ++ i) {  %>   <%= GetMemberVariableName(SourceTable.Columns[i]) %><%   if  (i  <  SourceTable.Columns.Count  -   1 ) {  %> , <%  } %><% } %>
        ]
    }),
    remoteSort:
true
});

Ext.onReady(
function (){
    Ext.BLANK_IMAGE_URL 
=   ' http://images.cnblogs.com/s.gif ' ;
    Ext.QuickTips.init();
    
var  grid  =   new  Ext.grid.GridPanel({
        store:storeMain,
        columns:
        [
            
new  Ext.grid.RowNumberer(),
            
            
<%   for  ( int  i  =   0 ; i  <  SourceTable.Columns.Count; i ++ ) {  %>
            {header:
" <%=GetHeaderName(SourceTable.Columns[i])%> " ,align: ' center ' ,sortable true ,dataIndex: <%= GetMemberVariableName(SourceTable.Columns[i]) %> } <%   if  (i  <  SourceTable.Columns.Count  -   1 ) {  %> , <%  }  %>                 
            
<%  }  %>
        ],
        renderTo:
' grid ' ,
        viewConfig:{forceFit:
true },        
        trackMouseOver:
true ,
        loadMask: 
true ,
        autoShow : 
true ,
        autoScroll: 
true ,
        loadMask:{msg:
" 数据加载中,请稍等 " },
        bbar:
new  Ext.PagingToolbar({
            pageSize:
20 ,
            store:storeMain,
            displayInfo:
true ,
            displayMsg:
' 当前显示 {0} - {1}条记录 /共 {2}条记录 ' ,
            emptyMsg:
" 没有数据 "
        })
    });
    storeMain.load({params:{start:
0 , limit: 20 ,sort: ' <%= SourceTable.PrimaryKey.MemberColumns[0].Name %> ' ,dir: ' desc ' }});
});
< script runat = " template " >
//  My functions here.
public string GetClassName(TableSchema table)
{
    
if (table == null )
    {
        
return   null ;
    }
    
return  table.Name  +   " Grid " ;
}
public string GetUrlName(TableSchema table)
{
    
if (table == null )
    {
        
return   null ;
    }
    
return  table.Name  +   " Grid " + " .aspx " ;
}

#region script
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
{
    
return  GetMemberVariableDeclarationStatement( " private " , column);
}

public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column)
{
    string statement 
=  protectionLevel  +   "   " ;
    statement 
+=  GetCSharpVariableType(column)  +   "   "   +  GetMemberVariableName(column);
    
    string defaultValue 
=  GetMemberVariableDefaultValue(column);
    
if  (defaultValue  !=   "" )
    {
        statement 
+=   "  =  "   +  defaultValue;
    }
    
    statement 
+=   " ; " ;
    
    
return  statement;
}

public string GetConstructorParameters()
{
    string ret 
=   "" ;
    foreach(ColumnSchema column 
in  SourceTable.Columns)
    {
        ret 
+=  GetCSharpVariableType(column)  +   "   "   +  GetCamelCaseName(GetPropertyName(column))  +   " ,/n/t/t/t " ;
    }
    
return  ret.Substring( 0 , ret.Length  -   5 );
}

public string GetAssignValue()
{
    string ret 
=   "" ;
    foreach(ColumnSchema column 
in  SourceTable.Columns)
    {
        ret 
+=  GetMemberVariableName(column)  +   ( new  String( '   ' , GetPropertyNameMaxLength()  -  GetPropertyNameLength(column)))  +   "  =  "   +  GetCamelCaseName(GetPropertyName(column))  +   " ;/n/t/t/t " ;
    }
    
return  ret;
}

public string GetReaderAssignmentStatement(ColumnSchema column, 
int  index)
{
    string statement 
=   " if (!reader.IsDBNull( "   +  index.ToString()  +   " ))  " ;
    statement 
+=  GetMemberVariableName(column)  +   "  =  " ;
    
    
if  (column.Name.EndsWith( " TypeCode " )) statement  +=   " ( "   +  column.Name  +   " ) " ;
    
    statement 
+=   " reader. "   +  GetReaderMethod(column)  +   " ( "   +  index.ToString()  +   " ); " ;
    
    
return  statement;
}

public string GetCamelCaseName(string value)
{
    
// return value.Substring(0, 1).ToLower() + value.Substring(1);
     return  value;
}

public string GetMemberVariableName(ColumnSchema column)
{
    
// string propertyName = GetPropertyName(column);
    string propertyName  =  column.Name;
    string memberVariableName 
=   " ' "   + GetCamelCaseName(propertyName) + " ' " ;
    
    
return  memberVariableName;
}
public string GetHeaderName(ColumnSchema column)
{
    
// string propertyName = GetPropertyName(column);
    string propertyName  =  column.Name;
    string memberVariableName 
=  GetCamelCaseName(propertyName);
    
    
return  memberVariableName;
}

public string GetPropertyName(ColumnSchema column)
{
    string propertyName 
=  column.Name;
    
    
if  (propertyName  ==  column.Table.Name  +   " Name " return   " Name " ;
    
if  (propertyName  ==  column.Table.Name  +   " Description " return   " Description " ;
    
    
if  (propertyName.EndsWith( " TypeCode " )) propertyName  =  propertyName.Substring( 0 , propertyName.Length  -   4 );
    
    
return  propertyName;
}

public 
int  GetPropertyNameLength(ColumnSchema column)
{
    
return  (GetPropertyName(column)).Length;
}
public 
int  GetPropertyNameMaxLength()
{
    
int  ret  =   0 ;
    foreach(ColumnSchema column 
in  SourceTable.Columns)
    {
        ret 
=  ret  <  GetPropertyNameLength(column) ? GetPropertyNameLength(column):ret;
    }
    
return  ret;
}
        
public string GetMemberVariableDefaultValue(ColumnSchema column)
{
    
if (column.IsPrimaryKeyMember)
    {
        
return   "" ;
    }
    
switch  (column.DataType)
    {
        
case  DbType.Guid:
        {
            
return   " Guid.Empty " ;
        }
        
case  DbType.AnsiString:
        
case  DbType.AnsiStringFixedLength:
        
case  DbType.String:
        
case  DbType.StringFixedLength:
        {
            
return   " String.Empty " ;
        }
        
default :
        {
            
return   "" ;
        }
    }
}

public string GetCSharpVariableType(ColumnSchema column)
{
    
if  (column.Name.EndsWith( " TypeCode " ))  return  column.Name;
    
    
switch  (column.DataType)
    {
        
case  DbType.AnsiString:  return   " string " ;
        
case  DbType.AnsiStringFixedLength:  return   " string " ;
        
case  DbType.Binary:  return   " byte[] " ;
        
case  DbType.Boolean:  return   " bool " ;
        
case  DbType.Byte:  return   " byte " ;
        
case  DbType.Currency:  return   " decimal " ;
        
case  DbType.Date:  return   " DateTime " ;
        
case  DbType.DateTime:  return   " DateTime " ;
        
case  DbType.Decimal:  return   " decimal " ;
        
case  DbType.Double:  return   " double " ;
        
case  DbType.Guid:  return   " Guid " ;
        
case  DbType.Int16:  return   " short " ;
        
case  DbType.Int32:  return   " int " ;
        
case  DbType.Int64:  return   " long " ;
        
case  DbType.Object:  return   " object " ;
        
case  DbType.SByte:  return   " sbyte " ;
        
case  DbType.Single:  return   " float " ;
        
case  DbType.String:  return   " string " ;
        
case  DbType.StringFixedLength:  return   " string " ;
        
case  DbType.Time:  return   " TimeSpan " ;
        
case  DbType.UInt16:  return   " ushort " ;
        
case  DbType.UInt32:  return   " uint " ;
        
case  DbType.UInt64:  return   " ulong " ;
        
case  DbType.VarNumeric:  return   " decimal " ;
        
default :
        {
            
return   " __UNKNOWN__ "   +  column.NativeType;
        }
    }
}

public string GetReaderMethod(ColumnSchema column)
{
    
switch  (column.DataType)
    {
        
case  DbType.Byte:
        {
            
return   " GetByte " ;
        }
        
case  DbType.Int16:
        {
            
return   " GetInt16 " ;
        }
        
case  DbType.Int32:
        {
            
return   " GetInt32 " ;
        }
        
case  DbType.Int64:
        {
            
return   " GetInt64 " ;
        }
        
case  DbType.AnsiStringFixedLength:
        
case  DbType.AnsiString:
        
case  DbType.String:
        
case  DbType.StringFixedLength:
        {
            
return   " GetString " ;
        }
        
case  DbType.Boolean:
        {
            
return   " GetBoolean " ;
        }
        
case  DbType.Guid:
        {
            
return   " GetGuid " ;
        }
        
case  DbType.Currency:
        
case  DbType.Decimal:
        {
            
return   " GetDecimal " ;
        }
        
case  DbType.DateTime:
        
case  DbType.Date:
        {
            
return   " GetDateTime " ;
        }
        
case  DbType.Binary:
        {
            
return   " GetBytes " ;
        }
        
default :
        {
            
return   " __SQL__ "   +  column.DataType;
        }
    }
}
public string GetSqlDbType(ColumnSchema column)
{
    
switch  (column.NativeType)
    {
        
case   " bigint " return   " BigInt " ;
        
case   " binary " return   " Binary " ;
        
case   " bit " return   " Bit " ;
        
case   " char " return   " Char " ;
        
case   " datetime " return   " DateTime " ;
        
case   " decimal " return   " Decimal " ;
        
case   " float " return   " Float " ;
        
case   " image " return   " Image " ;
        
case   " int " return   " Int " ;
        
case   " money " return   " Money " ;
        
case   " nchar " return   " NChar " ;
        
case   " ntext " return   " NText " ;
        
case   " numeric " return   " Decimal " ;
        
case   " nvarchar " return   " NVarChar " ;
        
case   " real " return   " Real " ;
        
case   " smalldatetime " return   " SmallDateTime " ;
        
case   " smallint " return   " SmallInt " ;
        
case   " smallmoney " return   " SmallMoney " ;
        
case   " sql_variant " return   " Variant " ;
        
case   " sysname " return   " NChar " ;
        
case   " text " return   " Text " ;
        
case   " timestamp " return   " Timestamp " ;
        
case   " tinyint " return   " TinyInt " ;
        
case   " uniqueidentifier " return   " UniqueIdentifier " ;
        
case   " varbinary " return   " VarBinary " ;
        
case   " varchar " return   " VarChar " ;
        
default return   " __UNKNOWN__ "   +  column.NativeType;
    }
}

public string GetPrimaryKeyType(TableSchema table)
{
    
if  (table.PrimaryKey  !=   null )
    {
        
if  (table.PrimaryKey.MemberColumns.Count  ==   1 )
        {
            
return  GetCSharpVariableType(table.PrimaryKey.MemberColumns[ 0 ]);
        }
        
else
        {
            
throw   new  ApplicationException( " This template will not work on primary keys with more than one member column. " );
        }
    }
    
else
    {
        
throw   new  ApplicationException( " This template will only work on tables with a primary key. " );
    }
}

public override string GetFileName()
{
    
return   this .GetClassName( this .SourceTable)  +   " .cs " ;
}

public string GetCamelCaseNameTrue(string value)
{
    
return  value.Substring( 0 1 ).ToLower()  +  value.Substring( 1 );
}
#endregion script
< / script>

 


http://www.niftyadmin.cn/n/1412897.html

相关文章

【spring boot 学习笔记】日志相关

1. 如何启用日志&#xff1f; maven依赖中添加&#xff1a;spring-boot-starter-logging <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId> </dependency>实际开发中我们不…

Httpd-2.4实现2个虚拟主机

Httpd-2.4实现2个虚拟主机&#xff1a;要求如下a.1.提供两个基于名称的虚拟主机www1,www2;有单独的错误日志和访问日志&#xff1b;1.通过www1的/server-status提供httpd状态信息&#xff0c;且仅允许tom用户访问&#xff1b;2.www2不允许192.168.0.0/24网段的任意主机访问&…

ArrayList 的实现原理

ArrayList 概述 ArrayList 可以理解为动态数组&#xff0c;用 MSDN 中的说法&#xff0c;就是 Array 的复杂版本。与 Java 中的数组相比&#xff0c;它的容量能动态增长。ArrayList 是 List 接口的可变数组的实现。实现了所有可选列表操作&#xff0c;并允许包括 null 在内的所…

Prometheus + Grafna监控Redis

# 下载redis_exporter wget https://github.com/oliver006/redis_exporter/releases/download/v1.3.3/redis_exporter-v1.3.3.linux-amd64.tar.gztar zxf redis_exporter-v1.3.3.linux-amd64.tar.gz cd redis_exporter-v1.3.3.linux-amd64# 启动&#xff0c;有密码则要加 -redi…

input只能输入数字,格式化数值的显示

2019独角兽企业重金招聘Python工程师标准>>> 在使用input域输入的时候&#xff0c;有时候要求只能输入数字&#xff0c; 1.比较简单的验证方法 <input typetext οnblur"if(isNaN(this.value))alert(不是数字)"> 2.使用正则来验证 正则: <script…

《小狗钱钱》读书笔记

关于成功 确立最重要的目标&#xff08;愿望&#xff09;。 会有一些人想要阻止我们实现自己的目标。阻力甚至经常来自我们的亲戚朋友。但还是有一些不让自己迷失的方法。 一个人挣钱多少是和他的自信心联系在一起的。还有就是取决于他的精力是集中在自己的能力范围之内&…

单元格里用“...”替换超长字符

关键代码如上图&#xff0c;然后在GridView中设置一个模板列调用上面的函数即可。<asp:TemplateField><ItemTemplate><asp:Label ID"add" runat"server" Text<%#GetString((DataBinder.Eval(Container.DataItem,"UserName")).…

PJzhang:python基础入门的7个疗程-two

猫宁&#xff01;&#xff01;&#xff01; 参考链接&#xff1a;易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 https://blog.csdn.net/SuperCreators/article/details/81393977 第四天&#xff1a;高级数据…