示例#1
0
 void InitBindingContext()
 {
     Model = new TableObject
     {
         Columns = 5,
         Rows = 3,
         Border = 1,
         Width = 100,
         Height = 100,
         WidthUnit = Unit.Percentage,
         HeightUnit = Unit.Pixel,
         SpacingUnit = Unit.Pixel,
         PaddingUnit = Unit.Pixel,
         HeaderOption = TableHeaderOption.Default,
         Alignment = TableAlignment.Default
     };
 }
        /// <summary>
        /// 执行插入表格命令
        /// </summary>
        public static void InsertTable(this HtmlDocument document, TableObject table)
        {
            int rows = table.Rows;
            int cols = table.Columns;
            string width = string.Format(" width=\"{0}{1}\"", table.Width, table.WidthUnit.Value);
            string height = string.Format(" height=\"{0}{1}\"", table.Height, table.HeightUnit.Value);
            string spacing = string.Format(" cellspacing=\"{0}{1}\"", table.Spacing, (table.Spacing > 0 ? table.SpacingUnit.Value : string.Empty));
            string padding = string.Format(" cellspacing=\"{0}{1}\"", table.Padding, (table.Padding > 0 ? table.PaddingUnit.Value : string.Empty));
            string border = string.Format(" border=\"{0}\"", table.Border);
            string title = (string.IsNullOrEmpty(table.Title) != false ? string.Format(" title=\"{0}\"", table.Title.HtmlEncoding()) : string.Empty);
            string align = (table.Alignment != TableAlignment.Default ? string.Format(" align=\"{0}\"", table.Alignment.Value) : string.Empty);

            StringBuilder bx = new StringBuilder();
            bx.AppendFormat("<table{0}{1}{2}{3}{4}{5}{6}>", width, height, spacing, padding, border, align, title);
            for (int i = 0; i < rows; i++)
            {
                bx.Append("<tr>");
                if (i == 0 && (table.HeaderOption == TableHeaderOption.FirstRow || table.HeaderOption == TableHeaderOption.FirstRowAndColumn))
                {
                    for (int j = 0; j < cols; j++) bx.Append("<th></th>");
                }
                else
                {
                    for (int j = 0; j < cols; j++)
                    {
                        if (i == 0 && (table.HeaderOption == TableHeaderOption.FirstColumn || table.HeaderOption == TableHeaderOption.FirstRowAndColumn))
                        {
                            bx.Append("<th></th>");
                        }
                        else bx.Append("<td></td>");
                    }
                }
                bx.Append("</tr>");
            }
            bx.Append("</table>");
            document.InsertHTML(bx.ToString());
        }