개발/C# .NET

ASP.NET MVC Grid Table

xwing 2014. 7. 29. 16:20


ASP.NET MVC4 

@Html 확장메서드


Grid Table



 public static MvcHtmlString Table<T>(this HtmlHelper htmlHelper, string id, IEnumerable<T> sorece, bool isSubGridDisplay = false)  
     {  
       if (sorece == null || sorece.Count() < 1)  
       {  
         string table = "<table id='" + id + "' class='cssEmptyTable'><tr><td style='text-align:center;'>데이터가 없습니다.</td></tr></table>";  
         return MvcHtmlString.Create(table);  
       }  
       PropertyInfo[] propertyinfos;  
       propertyinfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
       List<KeyValuePair<string, Type>> tableHeader = new List<KeyValuePair<string, Type>>();  
       List<KeyValuePair<string, Type>> tableBody = new List<KeyValuePair<string, Type>>();  
       List<KeyValuePair<string, Type>> columnProperty = new List<KeyValuePair<string, Type>>();  
       foreach (PropertyInfo info in propertyinfos)  
       {  
         object[] attributes = info.GetCustomAttributes(typeof(biz.Data.TableGridAttribute), false);  
         if (attributes.Count() > 0)  
         {  
           if (((biz.Data.TableGridAttribute)attributes[0]).IsHideGridDisplay) continue;  
           if (((biz.Data.TableGridAttribute)attributes[0]).ColumnType == typeof(decimal))  
           {  
             columnProperty.Add(new KeyValuePair<string, Type>(info.Name, ((biz.Data.TableGridAttribute)attributes[0]).ColumnType));  
           }  
         }  
         else  
         {  
           columnProperty.Add(new KeyValuePair<string, Type>(info.Name, typeof(string)));  
         }  
         var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(  
         () => Activator.CreateInstance<T>(), typeof(T), info.Name);  
         var name = metadata.DisplayName;  
         if (string.IsNullOrEmpty(name)) name = info.Name;  
         tableHeader.Add(new KeyValuePair<string, Type>(name, info.PropertyType));  
         tableBody.Add(new KeyValuePair<string, Type>(info.Name, info.PropertyType));  
       }  
       StringBuilder html = new StringBuilder(256);  
       html.Append("<table id='" + id + "'>");  
       html.Append("<tr>");  
       foreach (var header in tableHeader)  
       {  
         if (header.Value.IsGenericType && !header.Value.IsValueType)  
         {  
           if(isSubGridDisplay)  
             html.Append("<th>" + header.Key + "</th>");  
         }  
         else  
         {  
           html.Append("<th>" + header.Key + "</th>");  
         }  
       }  
       html.Append("</tr>");  
       int i = 1;  
       foreach (var src in sorece)  
       {  
         PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(src);  
         string clss = ((i % 2) == 0) ? "clsEven" : "clsOdd";  
         html.Append("<tr class='" + clss + "'>");  
         int j = 0;  
         foreach (var body in tableBody)  
         {  
           if (body.Value.IsGenericType && !body.Value.IsValueType)  
           {  
             if (isSubGridDisplay)  
             {  
               html.Append("<td>");  
               dynamic list = coll[body.Key].GetValue(src);  
               html.Append(Table(htmlHelper, "_sub_" + id, list, isSubGridDisplay));  
               html.Append("</td>");  
             }    
           }  
           else  
           {  
             KeyValuePair<string, Type> clm =  columnProperty[j];  
             if (clm.Value == typeof(decimal))  
             {  
               var _value = coll[body.Key].GetValue(src);  
               if (_value != null)  
               {  
                 html.Append("<td class='clsNumberField'>" + decimal.Parse(_value.ToString()).ToString("###,###.#") + "</td>");  
               }  
               else  
               {  
                 html.Append("<td>" + coll[body.Key].GetValue(src) + "</td>");  
               }  
             }  
             else  
             {  
               html.Append("<td>" + coll[body.Key].GetValue(src) + "</td>");  
             }  
           }  
           j++;  
         }  
         i++;  
         html.Append("</tr>");  
       }  
       var footer = "</table>";  
       html.Append(footer);  
       return MvcHtmlString.Create(html.ToString());  
     }  


[AttributeUsage(AttributeTargets.Property)]
    public class TableGridAttribute : System.Attribute
    {
        public bool IsHideGridDisplay { get; set; }

        public Type ColumnType { get; set; } 
    }


'개발 > C# .NET' 카테고리의 다른 글

[MVC3]ImageActionLink  (0) 2011.11.10
[MVC] DropDownList에 Enum 바인딩  (0) 2011.09.21
C# machine key 생성하기  (0) 2011.07.05