Funcionalidades
- Mini (Tamanho da DLL apenas 20KB) e fácil de usar.
- Suporta .NET Standard 2.0/.NET 4.6/.NET 4.5/.NET 4.0
- Sem qualquer biblioteca de terceiros
- Suporta Tipos Anônimos, Consulta Dinâmica Dapper, List/Array/Set/Enumrable, DataTable, Dictionary
Instalação
Você pode instalar o pacote a partir do NuGet usando o Gerenciador de Pacotes do Visual Studio ou a interface do NuGet:
PM> install-package HtmlTableHelperou pela linha de comando dotnet:
dotnet add package HtmlTableHelperDemonstração Fiddle:
Começando
##### Exemplo de List/Array/Set/Enumrable sem Tipo de Chave/Valor
``C#
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
Resultado:
*/
Name Age Gender ITWeiHan 25 M
C#
using (var cn = "Sua Conexão")
{
var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");
var tablehtml = sourceData.ToHtmlTable();
}
##### Exemplo Dapper
C#
var sourceData = new[] {new Dictionary
##### Exemplo Dictionary
Atributos Personalizados para Table/TR/TD/TH (Tipo Dinâmico) C# var data = /List/Array/Set/Enumrable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //este é um tipo dinâmico, suporta qualquer atributo ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* Resultado:
| .. |
|---|
| .. |
##### Anotação de Atributo###### 1. Display :
C#
public class ModelClassWithDisplayAttr
{
[TableColumn(DisplayName = "Coluna1")] //A propriedade MyProperty1 irá renderizar o innertext do thead-td: "Coluna1"
public string MyProperty1 { get; set; }
[TableColumn(DisplayName = "Coluna2")] //A propriedade MyProperty2 irá renderizar o innertext do thead-td: "Coluna2"
public string MyProperty2 { get; set; }
}
###### 2. Skip : C#
public class ModelClassWithSkipAttr
{
[TableColumn( Skip = true)]
public string MyProperty1 { get; set; } //MyProperty1 não irá renderizar html
public string MyProperty2 { get; set; }
}
##### HTMLTableBuilder###### HtmlCaption
C#
var soucreData = new []{ new {MyProperty1="teste",MyProperty2=123} };
var html = soucreData.CreateBuilder()
.SetCaption("Esta é a Legenda", new { id = "CaptionId" })
.ToHtmlTable();
//Resultado : | MyProperty1 | MyProperty2 |
|---|---|
| teste | 123 |
##### HTMLTableSettingCodificação InnerHtml configurável (Recomendado não fazer isso sem uma razão específica, devido a ataques XSS)
C#
var sourceData = new[] { new { Name = "ITWeiHan" } };//Codificação padrão var encodinghtml = sourceData.ToHtmlTable(); //Resultado:
var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //Resultado:
Extensão
ASP.NET Core MVC:
Crie um IHtmlHelperExtension.csC#
using System.Collections.Generic;
using HtmlTableHelper;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;public static class IHtmlHelperExtension
{
public static HtmlString ToHtmlTable
public static HtmlString ToHtmlTable
C#
@Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } })
/*
Resultado:
razor.cshtml
*/
Name Age Gender ITWeiHan 25 M
C#
using System.Collections.Generic;
using HtmlTableHelper;
using System.Web;
using System.Web.Mvc;
ASP.NET MVC 5:
Crie um HtmlHelperExtension.cs
public static class HtmlHelperExtension
{
public static HtmlString ToHtmlTable var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable
Demonstração
Demonstração do ASP.NET MVC 5 JQuery DataTable: `C#
using HtmlTableHelper;
//..
public class HomeController : Controller
{
public ActionResult Index()
{
var datas = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
ViewBag.Table = datas.ToHtmlTable();
return View();
}
}
C#
@{
Layout = null;
}
AspNetMvcDemo
@Html.Raw(ViewBag.Table)
Demonstração ASP.NET Core:
C#
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
await context.Response.WriteAsync(tablehtml);
});
}
}
---
Tranlated By Open Ai Tx | Last indexed: 2025-06-30
---