/// <summary> /// Makes grid from attribute /// </summary> /// <param name="t">Entity for reflection</param> /// <param name="mode">Template render mode</param> /// <returns>Grid class</returns> public Grid Reflect(Type t, TemplateMode mode) { var attribute = (GridPropertiesAttribute)t.GetCustomAttribute(typeof(GridPropertiesAttribute)); if (attribute != null) { var grid = new Grid(attribute); var rows = new List<GridRow>(); foreach (var property in t.GetProperties()) { var rowAttribute = (RowDisplayAttribute)property.GetCustomAttribute(typeof(RowDisplayAttribute)); if (rowAttribute != null) { if (mode == TemplateMode.ExtendedDisplay || !rowAttribute.ExtendedView) { if (mode == TemplateMode.Edit && rowAttribute.ColumnEditType == GridColumnTypes.DropDown) { rows.Add(new GridRow(t.GetProperty(rowAttribute.DropdownPropertyName), rowAttribute, mode)); } else { rows.Add(new GridRow(property, rowAttribute, mode)); } } } } grid.Rows = rows.ToList(); return grid; } return null; }
/// <summary> /// Set html properties for cell /// </summary> /// <param name="cellToConfigurate">Cell for setup</param> /// <param name="grid">Grid to render</param> /// <param name="row">Row to get properties</param> /// <returns>Configured header cell</returns> private HtmlTableHeaderCell ConfigureHeaderCell(HtmlTableHeaderCell cellToConfigurate, Grid grid, GridRow row) { if (row.NonBordered) { cellToConfigurate.Styles.Add("display:none"); } if (row.ColumnName != null) { cellToConfigurate.InnerHtml = row.ColumnName; } if (row.ColumnWidth != 0) { cellToConfigurate.Styles.Add(string.Format("max-width:{0}px", row.ColumnWidth)); } if (!string.IsNullOrEmpty(row.DataField)) { cellToConfigurate.Attributes.Add(new KeyValuePair<string, string>("data-field", row.DataField)); } if (!string.IsNullOrEmpty(row.ModelField)) { cellToConfigurate.Attributes.Add(new KeyValuePair<string, string>("data-modelfield", row.ModelField)); } if (row.IsSortable) { cellToConfigurate.Classes.Add("sort-row"); if (grid.Sorting != null && grid.Sorting.SortColumnName == row.DataField) { cellToConfigurate.Classes.Add("current-sort"); if (grid.Sorting.LowestToHight) { cellToConfigurate.Classes.Add("sorting_asc"); } else { cellToConfigurate.Classes.Add("sorting_desc"); } } else { cellToConfigurate.Classes.Add("sorting"); } } if (row.IsHidden) { cellToConfigurate.Styles.Add("display:none"); } return cellToConfigurate; }
/// <summary> /// Render grid body /// </summary> /// <param name="grid">Grid to render</param> /// <param name="entities">List of entities to render</param> /// <returns>Render grid body in string</returns> private string RenderBody(Grid grid, List<object> entities) { var bodyRender = string.Empty; foreach (var entity in entities) { var column = string.Empty; foreach (var row in grid.Rows) { var cell = new HtmlTableCell(); cell = this.ConfigureTableCell(cell, row, entity); column += cell.ToString(); } bodyRender += string.Format("<tr>{0}</tr>", column); } return string.Format("<tbody>{0}</tbody>", bodyRender); }
/// <summary> /// Renders grid header /// </summary> /// <param name="grid">Grid to render</param> /// <returns>Rendered in string grid</returns> private string RenderHeader(Grid grid) { var headerRender = string.Empty; foreach (var row in grid.Rows) { var headerCell = new HtmlTableHeaderCell(); headerCell = this.ConfigureHeaderCell(headerCell, grid, row); headerRender += headerCell.ToString(); } return string.Format("<thead><tr>{0}</tr></thead>", headerRender); }