Ext生成GridView的CodeSmith模板

<%-- 
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" %>

ExpandedBlockStart.gifContractedBlock.gif
/**//**
 * @author sunfish
 
*/


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

ExpandedBlockStart.gifContractedBlock.gifExt.onReady(function()
{
    Ext.BLANK_IMAGE_URL 
= 'http://images.cnblogs.com/s.gif';
    Ext.QuickTips.init();
ExpandedSubBlockStart.gifContractedSubBlock.gif    var grid 
= new Ext.grid.GridPanel({
        store:storeMain,
        columns:
        [
            
new Ext.grid.RowNumberer(),
            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
<% for (int i = 0; i < SourceTable.Columns.Count; i++%>
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{header:"<%=GetHeaderName(SourceTable.Columns[i])%>",align:'center',sortable: true,dataIndex:<%=GetMemberVariableName(SourceTable.Columns[i])%>}<% if (i < SourceTable.Columns.Count - 1%>,<% } %>                
            
<% }
 %>
        ],
        renderTo:
'grid',
ExpandedSubBlockStart.gifContractedSubBlock.gif        viewConfig:
{forceFit:true},        
        trackMouseOver:
true,
        loadMask: 
true,
        autoShow : 
true,
        autoScroll: 
true,
ExpandedSubBlockStart.gifContractedSubBlock.gif        loadMask:
{msg:"数据加载中,请稍等"},
ExpandedSubBlockStart.gifContractedSubBlock.gif        bbar:
new Ext.PagingToolbar({
            pageSize:
20,
            store:storeMain,
            displayInfo:
true,
            displayMsg:
'当前显示 {0} - {1}条记录 /共 {2}条记录',
            emptyMsg:
"没有数据"
        }
)
    }
);
ExpandedSubBlockStart.gifContractedSubBlock.gif    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)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(table==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return null;
    }

    
return table.Name + "Grid";
}

public string GetUrlName(TableSchema table)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(table==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return null;
    }

    
return table.Name + "Grid"+".aspx";
}


ContractedBlock.gifExpandedBlockStart.gif
script#region script
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
return GetMemberVariableDeclarationStatement("private", column);
}


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

    
    statement 
+= ";";
    
    
return statement;
}


public string GetConstructorParameters()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
string ret = "";
    
foreach(ColumnSchema column in SourceTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ret 
+= GetCSharpVariableType(column) + " " + GetCamelCaseName(GetPropertyName(column)) + ",\n\t\t\t";
    }

    
return ret.Substring(0, ret.Length - 5);
}


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

    
return ret;
}


public string GetReaderAssignmentStatement(ColumnSchema column, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
//return value.Substring(0, 1).ToLower() + value.Substring(1);
    return value;
}


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

public string GetHeaderName(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
//string propertyName = GetPropertyName(column);
    string propertyName = column.Name;
    
string memberVariableName = GetCamelCaseName(propertyName);
    
    
return memberVariableName;
}


public string GetPropertyName(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
return (GetPropertyName(column)).Length;
}

public int GetPropertyNameMaxLength()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
int ret = 0;
    
foreach(ColumnSchema column in SourceTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ret 
= ret < GetPropertyNameLength(column)?GetPropertyNameLength(column):ret;
    }

    
return ret;
}

        
public string GetMemberVariableDefaultValue(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
if(column.IsPrimaryKeyMember)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "";
    }

    
switch (column.DataType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
case DbType.Guid:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "Guid.Empty";
        }

        
case DbType.AnsiString:
        
case DbType.AnsiStringFixedLength:
        
case DbType.String:
        
case DbType.StringFixedLength:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "String.Empty";
        }

        
default:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "";
        }

    }

}


public string GetCSharpVariableType(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
if (column.Name.EndsWith("TypeCode")) return column.Name;
    
    
switch (column.DataType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
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:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "__UNKNOWN__" + column.NativeType;
        }

    }

}


public string GetReaderMethod(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
switch (column.DataType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
case DbType.Byte:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetByte";
        }

        
case DbType.Int16:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetInt16";
        }

        
case DbType.Int32:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetInt32";
        }

        
case DbType.Int64:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetInt64";
        }

        
case DbType.AnsiStringFixedLength:
        
case DbType.AnsiString:
        
case DbType.String:
        
case DbType.StringFixedLength:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetString";
        }

        
case DbType.Boolean:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetBoolean";
        }

        
case DbType.Guid:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetGuid";
        }

        
case DbType.Currency:
        
case DbType.Decimal:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetDecimal";
        }

        
case DbType.DateTime:
        
case DbType.Date:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetDateTime";
        }

        
case DbType.Binary:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "GetBytes";
        }

        
default:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "__SQL__" + column.DataType;
        }

    }

}

public string GetSqlDbType(ColumnSchema column)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
switch (column.NativeType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
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";
        
defaultreturn "__UNKNOWN__" + column.NativeType;
    }

}


public string GetPrimaryKeyType(TableSchema table)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
if (table.PrimaryKey != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (table.PrimaryKey.MemberColumns.Count == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
        }

    }

    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
throw new ApplicationException("This template will only work on tables with a primary key.");
    }

}


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


public string GetCamelCaseNameTrue(string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
    
return value.Substring(01).ToLower() + value.Substring(1);
}

#endregion
 script
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值