Fonctionnalités
- Mini (taille DLL seulement 20KB) et facile à utiliser.
- Supporte .NET Standard 2.0/.NET 4.6/.NET 4.5/.NET 4.0
- Sans aucune bibliothèque tierce
- Prend en charge les types anonymes, requête dynamique Dapper, List/Array/Set/Enumerable, DataTable, Dictionary
Installation
Vous pouvez installer le package depuis NuGet en utilisant le Visual Studio Package Manager ou l’interface NuGet :
PM> install-package HtmlTableHelperou la ligne de commande dotnet :
dotnet add package HtmlTableHelperDémo Fiddle :
Pour commencer
##### Exemple List/Array/Set/Enumerable de type non Clé/Valeur
``C#
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
Résultat :
*/
Name Age Gender ITWeiHan 25 M
C#
using (var cn = "Votre Connexion")
{
var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");
var tablehtml = sourceData.ToHtmlTable();
}
##### Exemple Dapper
C#
var sourceData = new[] {new Dictionary
##### Exemple Dictionary
Attributs personnalisés Table/TR/TD/TH (Type dynamique) C# var data = /List/Array/Set/Enumerable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} // ceci est un type dynamique, supporte tout attribut ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* Résultat :
| .. |
|---|
| .. |
##### Annotation d'attribut###### 1. Display :
C#
public class ModelClassWithDisplayAttr
{
[TableColumn(DisplayName = "Colonne1")] //La propriété MyProperty1 affichera dans thead-td : "Colonne1"
public string MyProperty1 { get; set; }
[TableColumn(DisplayName = "Colonne2")] //La propriété MyProperty2 affichera dans thead-td : "Colonne2"
public string MyProperty2 { get; set; }
}
###### 2. Sauter : C#
public class ModelClassWithSkipAttr
{
[TableColumn( Skip = true)]
public string MyProperty1 { get; set; } //MyProperty1 ne sera pas rendu en html
public string MyProperty2 { get; set; }
}
##### HTMLTableBuilder###### HtmlCaption
C#
var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} };
var html = soucreData.CreateBuilder()
.SetCaption("Ceci est la légende", new { id = "CaptionId" })
.ToHtmlTable();
//Résultat : | MyProperty1 | MyProperty2 |
|---|---|
| test | 123 |
##### HTMLTableSettingEncodage InnerHtml configurable (Non recommandé sauf raison spécifique, risque d'attaque XSS)
C#
var sourceData = new[] { new { Name = "ITWeiHan" } };//Encodage par défaut var encodinghtml = sourceData.ToHtmlTable(); //Résultat :
var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //Résultat :
Extension
ASP.NET Core MVC :
Créer un fichier 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" } })
/*
Résultat :
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 :
Créer un fichier HtmlHelperExtension.cs
public static class HtmlHelperExtension
{
public static HtmlString ToHtmlTablevar html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable
Démonstration
Démo 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)
Démo 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
---