Dynamically create HTML table in C#

前端 未结 4 1524
逝去的感伤
逝去的感伤 2020-12-30 06:40

Is there more efficient way to build HTML table than the one I\'m trying on right now?

I\'m getting an object and it has some list of entities in it. So I need to pa

4条回答
  •  执念已碎
    2020-12-30 07:30

    I would just like to supplement Steve Harris' answer with a class library that is a little more built out. His answer is a totally elegant solution that made a windows service I was creating not have to reference System.Web for no good reason!

    Classes Defined:

      public static class Html
      {
        public class Table : HtmlBase, IDisposable
        {
          public Table(StringBuilder sb, string classAttributes = "", string id = "") : base(sb)
          {
            Append("");
          }
    
          public void StartFoot(string classAttributes = "", string id = "")
          {
            Append("");
          }
    
          public void StartBody(string classAttributes = "", string id = "")
          {
            Append("");
          }
    
          public void Dispose()
          {
            Append("");
          }
    
          public Row AddRow(string classAttributes = "", string id = "")
          {
            return new Row(GetBuilder(), classAttributes, id);
          }
        }
    
        public class Row : HtmlBase, IDisposable
        {
          public Row(StringBuilder sb, string classAttributes = "", string id = "") : base(sb)
          {
            Append("");
          }
          public void AddCell(string innerText, string classAttributes = "", string id = "", string colSpan = "")
          {
            Append("");
          }
        }
    
        public abstract class HtmlBase
        {
          private StringBuilder _sb;
    
          protected HtmlBase(StringBuilder sb)
          {
            _sb = sb;
          }
    
          public StringBuilder GetBuilder()
          {
            return _sb;
          }
    
          protected void Append(string toAppend)
          {
            _sb.Append(toAppend);
          }
    
          protected void AddOptionalAttributes(string className = "", string id = "", string colSpan = "")
          {
    
            if (!id.IsNullOrEmpty())
            {
              _sb.Append($" id=\"{id}\"");
            }
            if (!className.IsNullOrEmpty())
            {
              _sb.Append($" class=\"{className}\"");
            }
            if (!colSpan.IsNullOrEmpty())
            {
              _sb.Append($" colspan=\"{colSpan}\"");
            }
            _sb.Append(">");
          }
        }
      }
    

    Usage:

    StringBuilder sb = new StringBuilder();
          using (Html.Table table = new Html.Table(sb, id: "some-id"))
          {
            table.StartHead();
            using (var thead = table.AddRow())
            {
              thead.AddCell("Category Description");
              thead.AddCell("Item Description");
              thead.AddCell("Due Date");
              thead.AddCell("Amount Budgeted");
              thead.AddCell("Amount Remaining");
            }
            table.EndHead();
            table.StartBody();
            foreach (var alert in alertsForUser)
            {
              using (var tr = table.AddRow(classAttributes: "someattributes"))
              {
               tr.AddCell(alert.ExtendedInfo.CategoryDescription);
                tr.AddCell(alert.ExtendedInfo.ItemDescription);
                tr.AddCell(alert.ExtendedInfo.DueDate.ToShortDateString());
                tr.AddCell(alert.ExtendedInfo.AmountBudgeted.ToString("C"));
                tr.AddCell(alert.ExtendedInfo.ItemRemaining.ToString("C"));
              }
            }
            table.EndBody();
          }
          return sb.ToString();
    

提交回复
热议问题