/// <summary> /// Create an Image with innerHtml, buttonType, htmlAttributes. /// </summary> /// <param name="htmlHelper"></param> /// <param name="innerHtml">Display name</param> /// <param name="buttonType">Button type</param> /// <param name="htmlAttributes">Html Attributes</param> /// <returns></returns> public static MvcHtmlString Button(this HtmlHelper htmlHelper, string innerHtml, HtmlButtonTypes buttonType = null, object htmlAttributes = null ) { return(ButtonHtmlExtention.Button(htmlHelper, innerHtml, string.Empty, string.Empty, string.Empty, buttonType, htmlAttributes)); }
/// <summary> /// Create a Table Layout. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="htmlhelper"></param> /// <param name="viewModel">Model with List to Show and Properties to Show.</param> /// <param name="withEdit">A boolean type for create a Table with Edit button</param> /// <param name="withRemove">A boolean type for create a Table with Remove button</param> /// <param name="withDetail">A boolean type for create a Table with Detail button</param> /// <param name="htmlAttributes"></param> /// <returns></returns> public static MvcHtmlString TableListFor <TModel, TEntity>(this HtmlHelper <TModel> htmlhelper, ITableList <TEntity> viewModel, bool withEdit = false, bool withRemove = false, bool withDetail = false, object htmlAttributes = null) where TEntity : class { TagBuilder table = new TagBuilder("table"); table.AddCssClass("table table-bordered table-hover table-responsive"); TagBuilder tr = new TagBuilder("tr"); foreach (var propertyToShow in viewModel.PropertiesToShow) { TagBuilder th = new TagBuilder("th"); th.MergeAttribute("style", "cursor: default; "); th.InnerHtml = propertyToShow.DisplayProperty != null ? propertyToShow.DisplayProperty : propertyToShow.PropertyName; tr.InnerHtml += th.ToString(); } if (withEdit) { tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Editar", new { style = "width: 5%; cursor: default;" }).ToString(); } if (withDetail) { tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Detalhes", new { style = "width: 5%; cursor: default" }).ToString(); } if (withRemove) { tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Remover", new { style = "width: 5%; cursor: default" }).ToString(); } TagBuilder tHead = HtmlExtentionsCommon.CreateTagBuilder("thead", tr.ToString()); TagBuilder tbody = new TagBuilder("tbody"); foreach (var item in viewModel.List) { TagBuilder trItem = new TagBuilder("tr"); var propertiesFromTheItem = item.GetType().GetProperties(); foreach (var property in propertiesFromTheItem) { PropertyToShow showThisProperty = HtmlExtentionsCommon.ShowThisProperty(property, viewModel.PropertiesToShow, item); if (showThisProperty != null) { string display = showThisProperty.DisplayProperty != null ? showThisProperty.DisplayProperty : showThisProperty.PropertyName; TagBuilder td = new TagBuilder("td"); td.InnerHtml = item.GetType().GetProperty(property.Name).GetValue(item).ToString(); td.MergeAttribute("style", "padding-top: 15px"); trItem.InnerHtml += td.ToString(); } } if (withEdit) { var button = HtmlExtentionsCommon.CreateTagBuilder("td", ButtonHtmlExtention.Button( htmlhelper, string.Empty, "btn-info col-lg-12", "glyphicon glyphicon-edit", string.Empty, HtmlButtonTypes.Button, new { data_action = "editar", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Editar" } ).ToString()); trItem.InnerHtml += button.ToString(); } if (withDetail) { var button = HtmlExtentionsCommon.CreateTagBuilder("td", ButtonHtmlExtention.Button( htmlhelper, string.Empty, "btn-success col-lg-10 col-lg-offset-1", "glyphicon glyphicon-search", string.Empty, HtmlButtonTypes.Button, new { data_action = "editar", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Detalhe" } ).ToString()); trItem.InnerHtml += button.ToString(); } if (withRemove) { var button = HtmlExtentionsCommon.CreateTagBuilder("td", ButtonHtmlExtention.Button( htmlhelper, string.Empty, "btn-danger col-lg-10 col-lg-offset-1", "glyphicon glyphicon-trash", string.Empty, HtmlButtonTypes.Button, new { data_action = "remover", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Remover" } ).ToString()); trItem.InnerHtml += button.ToString(); } tbody.InnerHtml += trItem.ToString(); } table.InnerHtml = tHead.ToString(); table.InnerHtml += tbody.ToString(); if (htmlAttributes != null) { var anonymousObject = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); if (anonymousObject.Select(x => x.Value.ToString().Contains("margin")).FirstOrDefault()) { table.MergeAttributes(anonymousObject); } else { anonymousObject.Add("style", "margin: 10px 0px"); } } else if (htmlAttributes != null) { table.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { style = "margin: 10px 0px; form-group" })); } return(MvcHtmlString.Create(table.ToString())); }