Web Analytics

HtmlTableHelper

⭐ 42 stars Japanese by mini-software

NuGet


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


特徴

インストール

パッケージはNuGetからVisual StudioのパッケージマネージャまたはNuGet UIでインストールできます:

PM> install-package HtmlTableHelper

または dotnet コマンドライン:

dotnet add package HtmlTableHelper

Fiddle デモ:

はじめに

##### List/Array/Set/Enumerable 非Key/Value型の例 ``C# using HtmlTableHelper; .. var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } }; var tablehtml = sourceData.ToHtmlTable(); /* 結果:

NameAgeGender
ITWeiHan25M
*/

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

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

カスタム Table/TR/TD/TH 属性(動的型)

C# var data = /List/Array/Set/Enumerable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //これは動的型で、すべての属性をサポート ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* 結果:
..
..
*/

##### 属性アノテーション

###### 1. Display :

C# public class ModelClassWithDisplayAttr { [TableColumn(DisplayName = "Column1")] //MyProperty1 プロパティは thead-td の innertext を "Column1" としてレンダリングします public string MyProperty1 { get; set; } [TableColumn(DisplayName = "Column2")] //MyProperty2 プロパティは thead-td の innertext を "Column2" としてレンダリングします public string MyProperty2 { get; set; } }

###### 2. スキップ :
C# public class ModelClassWithSkipAttr { [TableColumn( Skip = true)] public string MyProperty1 { get; set; } //MyProperty1 は HTML をレンダリングしません public string MyProperty2 { get; set; } }

##### HTMLTableBuilder

###### HtmlCaption

C# var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} }; var html = soucreData.CreateBuilder() .SetCaption("This is Caption", new { id = "CaptionId" }) .ToHtmlTable(); //結果 :
This is Caption
MyProperty1MyProperty2
test123

##### HTMLTableSetting

設定可能な InnerHtml エンコーディング(特別な理由がない限り推奨しません。XSS攻撃の可能性があるため)

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

//デフォルトのエンコーディング var encodinghtml = sourceData.ToHtmlTable(); //結果:

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

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

..ITWeiHan..

拡張

ASP.NET Core MVC: 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" } }) /* 結果:
NameAgeGender
ITWeiHan25M
*/

ASP.NET MVC 5:   
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); } }

デモ

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)

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