Web Analytics

HtmlTableHelper

⭐ 42 stars Polish by mini-software

NuGet


version version version version version version version version version version version version version version version version version


Funkcje

Instalacja

Możesz zainstalować pakiet z NuGet za pomocą Menedżera pakietów Visual Studio lub interfejsu NuGet:

PM> install-package HtmlTableHelper

lub za pomocą linii poleceń dotnet:

dotnet add package HtmlTableHelper

Fiddle Demo:

Pierwsze kroki

##### Przykład List/Array/Set/Enumerable bez typu klucz/wartość ``C# using HtmlTableHelper; .. var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } }; var tablehtml = sourceData.ToHtmlTable(); /* Wynik:

NameAgeGender
ITWeiHan25M
*/

##### Przykład Dapper
C# using (var cn = "Your Connection") { var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender"); var tablehtml = sourceData.ToHtmlTable(); }

##### Przykład Dictionary 
C# var sourceData = new[] {new Dictionary (){{"Name" , "ITWeiHan" },{"Age",25},{"Gender","M"}}}; var tablehtml = sourceData.ToHtmlTable();

Własne atrybuty Table/TR/TD/TH (typ dynamiczny)

C# var data = /List/Array/Set/Enumerable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //to jest typ dynamiczny, obsługuje wszystkie atrybuty ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* Wynik:
..
..
*/

##### Adnotacje atrybutów

###### 1. Display :

C# public class ModelClassWithDisplayAttr { [TableColumn(DisplayName = "Kolumna1")] //Właściwość MyProperty1 wyrenderuje thead-td z innertext: "Kolumna1" public string MyProperty1 { get; set; } [TableColumn(DisplayName = "Kolumna2")] //Właściwość MyProperty2 wyrenderuje thead-td z innertext: "Kolumna2" public string MyProperty2 { get; set; } }

###### 2. Pominięcie : 
C# public class ModelClassWithSkipAttr { [TableColumn( Skip = true)] public string MyProperty1 { get; set; } //MyProperty1 nie zostanie wyrenderowana w html public string MyProperty2 { get; set; } }

##### HTMLTableBuilder

###### HtmlCaption

C# var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} }; var html = soucreData.CreateBuilder() .SetCaption("To jest podpis", new { id = "CaptionId" }) .ToHtmlTable(); //Wynik :
To jest podpis
MyProperty1MyProperty2
test123

##### HTMLTableSetting

Konfigurowalne kodowanie InnerHtml (Zalecane, aby nie wyłączać bez konkretnego powodu, ze względu na ataki XSS)

C# var sourceData = new[] { new { Name = "ITWeiHan" } };

//Domyślne kodowanie var encodinghtml = sourceData.ToHtmlTable(); //Wynik:

..<b>ITWeiHan</b>..

var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //Wynik:

..ITWeiHan..

Rozszerzenie

ASP.NET Core MVC: Utwórz plik IHtmlHelperExtension.cs
C# using System.Collections.Generic; using HtmlTableHelper; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Html;

public static class IHtmlHelperExtension { public static HtmlString ToHtmlTable(this IHtmlHelper htmlHelper, IEnumerable enums , object tableAttributes = null, object trAttributes = null, object tdAttributes = null , HtmlTableSetting HTMLTableSetting = null) { var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting); return new HtmlString(html); }

public static HtmlString ToHtmlTable(this IHtmlHelper htmlHelper, System.Data.DataTable datatable , object tableAttributes = null, object trAttributes = null, object tdAttributes = null , HtmlTableSetting HTMLTableSetting = null) { var html = datatable.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting); return new HtmlString(html); } }


razor.cshtml 
C# @Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } }) /* Wynik:
NameAgeGender
ITWeiHan25M
*/

ASP.NET MVC 5:   
Utwórz plik HtmlHelperExtension.cs
C# using System.Collections.Generic; using HtmlTableHelper; using System.Web; using System.Web.Mvc;

public static class HtmlHelperExtension { public static HtmlString ToHtmlTable(this HtmlHelper htmlHelper, IEnumerable enums , object tableAttributes = null, object trAttributes = null, object tdAttributes = null , HtmlTableSetting HTMLTableSetting = null) {

        var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
        return new HtmlString(html);
    }

public static HtmlString ToHtmlTable(this HtmlHelper htmlHelper, System.Data.DataTable datatable , object tableAttributes = null, object trAttributes = null, object tdAttributes = null , HtmlTableSetting HTMLTableSetting = null) { var html = datatable.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting); return new HtmlString(html); } }

Demo

ASP.NET MVC 5 JQuery DataTable Demo:
`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)

ASP.NET Core Demo:

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 ---