Class To Html Table
List<T> olan bir veriyi html çıktı olarak hazırlama…
Öncelik custom attribute yazmamız lazım.
[AttributeUsage(AttributeTargets.Property)]
public class ListAttribute : Attribute
{
public Enums.Color ListFieldColor { get; set; }
public string ListFieldEmptyName { get; set; }
public string ListFieldDisplayName { get; set; }
public bool ListVisibility { get; set; }
public ListAttribute()
{
ListVisibility = true;
}
public ListAttribute(string listFieldDisplayName)
: this(listFieldDisplayName, string.Empty)
{
}
public ListAttribute(string listFieldDisplayName, string listFieldEmptyName)
: this(listFieldDisplayName, listFieldEmptyName, Enums.Color.Empty)
{
}
public ListAttribute(string listFieldDisplayName, Enums.Color listFieldColor)
: this(listFieldDisplayName, string.Empty, listFieldColor)
{
}
public ListAttribute(string listFieldDisplayName, bool listVisibility)
: this(listFieldDisplayName, string.Empty, Enums.Color.Empty, listVisibility)
{
}
public ListAttribute(string listFieldDisplayName, string listFieldEmptyName = "", Enums.Color listFieldColor = Enums.Color.Empty, bool listVisibility = true)
{
ListFieldDisplayName = listFieldDisplayName;
ListFieldEmptyName = listFieldEmptyName;
ListFieldColor = listFieldColor;
ListVisibility = listVisibility;
}
}
Daha sonra class içindeki property üzerine tanımlama yapmalıyız.
[List(ListFieldDisplayName = "Deneme Kolon Adı")]
public int PatientName { get; set; }
Ve son olarak aşağıdaki kod ile işlemimizi tamamlıyoruz.
public static string ClassToHtmlTable<T>(IList<T> list)
{
if (!list.Any()) return "<br /><hr>";
StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<table class='table'>");
sb.AppendLine("<thead>");
sb.AppendLine("<tr>");
foreach (var prop in typeof(T).GetProperties())
{
var listAttribute = (ListAttribute[])prop.GetCustomAttributes(typeof(ListAttribute), false);
foreach (var attr in listAttribute)
{
if (!attr.ListVisibility) continue;
sb.Append("<th>");
sb.Append(!string.IsNullOrEmpty(attr.ListFieldDisplayName)
? attr.ListFieldDisplayName
: prop.Name);
sb.Append("</th>");
}
}
sb.Append("</tr>\n");
int nRow = 0;
foreach (var item in list)
{
sb.Append(nRow % 2 == 0 ? "<tr class='info'>\n" : "<tr>\n");
foreach (var prop in typeof(T).GetProperties())
{
var listAttribute = (ListAttribute[])prop.GetCustomAttributes(typeof(ListAttribute), false);
foreach (var attr in listAttribute)
{
if (!attr.ListVisibility) continue;
var color = GetListAttributeColor(prop);
sb.Append(color == string.Empty ? "<td>" : "<td style=\"color:" + color + ";\">");
if (prop.GetValue(item) is float)
{
sb.Append(Convert.ToDecimal(prop.GetValue(item)).ToString("#,##0.00", new System.Globalization.CultureInfo("tr-TR")));
}
else if (prop.GetValue(item) is DateTime)
{
sb.Append(DateTime.MinValue == Convert.ToDateTime(prop.GetValue(item))
? GetListAttributeEmptyName(prop)
: Convert.ToDateTime(prop.GetValue(item)).ToString("dd-MM-yyyy HH:mm", new System.Globalization.CultureInfo("tr-TR")));
}
else if (prop.GetValue(item) is bool)
{
var emptyName = GetListAttributeEmptyName(prop);
if (!Convert.ToBoolean(prop.GetValue(item)) && !string.IsNullOrEmpty(emptyName))
{
sb.Append(emptyName);
}
else
{
sb.Append(Convert.ToBoolean(prop.GetValue(item))
? "<i class=\"glyphicon glyphicon-ok\"></i>"
: "<i class=\"glyphicon glyphicon-remove\"></i>");
}
}
else
{
sb.Append(prop.GetValue(item));
}
sb.Append("</td>");
}
}
//
nRow++;
sb.Append("</tr>\n");
}
sb.AppendLine("</tbody>");
sb.AppendLine("</table>");
sb.AppendLine("<span class=\"badge pull-right\">Toplam Kayıt Sayısı : " + list.Count + "</span>");
sb.AppendLine("</html>");
return sb.ToString();
}
private static string GetListAttributeEmptyName(PropertyInfo propertInfo)
{
var listAttribute = propertInfo?.GetCustomAttribute(typeof(ListAttribute)) as ListAttribute;
return listAttribute != null ? listAttribute.ListFieldEmptyName : string.Empty;
}
private static string GetListAttributeColor(PropertyInfo propertInfo)
{
var listAttribute = propertInfo?.GetCustomAttribute(typeof(ListAttribute)) as ListAttribute;
return listAttribute?.ListFieldColor == Enums.Color.Red? "red" : string.Empty;
}