Skip to content

Commit 07c7a5a

Browse files
emrahkondurekondur
authored andcommitted
#138 Added Select All Checkbox
1 parent 1cb7457 commit 07c7a5a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

DatatableJS.Net/JSHelper.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ private static string RenderHtmlString<T>(this GridBuilder<T> grid)
114114
<table id=""{grid._name}"" class=""{grid._cssClass}"" style=""width:100%"">
115115
<thead>
116116
<tr>
117-
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Title)))}
117+
{string.Join(Environment.NewLine,
118+
grid._columns.Select(a => grid._selectEnable && grid._selectItems == SelectItems.Checkbox && a.ClassName == "select-checkbox"
119+
? $"<th style=\"text-align:center\"><input type=\"checkbox\" id=\"{grid._name}_SelectAll\"></th>"
120+
: string.Format("<th>{0}</th>", a.Title)))}
118121
</tr>
119122
</thead>
120123
{tfoot}
@@ -302,6 +305,7 @@ private static string RenderScriptString<T>(this GridBuilder<T> grid)
302305
}});
303306
{(string.IsNullOrEmpty(grid._captionTop) ? string.Empty : string.Format("$('#{0}').append('<caption style=\"caption-side:top\">{1}</caption>');", grid._name, grid._captionTop))}
304307
{(string.IsNullOrEmpty(grid._captionBottom) ? string.Empty : string.Format("$('#{0}').append('<caption style=\"caption-side:bottom\">{1}</caption>');", grid._name, grid._captionBottom))}
308+
{grid.GetSelectionScript()}
305309
</script>";
306310

307311
return script;
@@ -316,6 +320,20 @@ private static string GetDataStr<T>(this GridBuilder<T> grid)
316320
{(string.IsNullOrEmpty(grid._data) ? string.Empty : string.Format("d.data = {0}()", grid._data))}
317321
}}";
318322
}
323+
324+
private static string GetSelectionScript<T>(this GridBuilder<T> grid)
325+
{
326+
return grid._selectEnable && grid._selectItems == SelectItems.Checkbox ?
327+
$@"$(""#{grid._name}_SelectAll"").on( ""click"", function(e) {{
328+
var {grid._name}DT = $(""#{grid._name}"").DataTable();
329+
if ($(this).is( "":checked"" )) {{
330+
{grid._name}DT.rows().select();
331+
}}
332+
else {{
333+
{grid._name}DT.rows().deselect();
334+
}}
335+
}});" : string.Empty;
336+
}
319337
}
320338
}
321339

DatatableJS/JSHelper.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ public static IHtmlContent RenderHtml<T>(this GridBuilder<T> grid)
7979
<table id=""{grid._name}"" class=""{grid._cssClass}"" style=""width:100%"">
8080
<thead>
8181
<tr>
82-
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Title)))}
82+
{string.Join(Environment.NewLine,
83+
grid._columns.Select(a => grid._selectEnable && grid._selectItems == SelectItems.Checkbox && a.ClassName == "select-checkbox"
84+
? $"<th style=\"text-align:center\"><input type=\"checkbox\" id=\"{grid._name}_SelectAll\"></th>"
85+
: string.Format("<th>{0}</th>", a.Title)))}
8386
</tr>
8487
</thead>
8588
{tfoot}
@@ -271,6 +274,7 @@ public static IHtmlContent RenderScript<T>(this GridBuilder<T> grid)
271274
}});
272275
{(string.IsNullOrEmpty(grid._captionTop) ? string.Empty : string.Format("$('#{0}').append('<caption style=\"caption-side:top\">{1}</caption>');", grid._name, grid._captionTop))}
273276
{(string.IsNullOrEmpty(grid._captionBottom) ? string.Empty : string.Format("$('#{0}').append('<caption style=\"caption-side:bottom\">{1}</caption>');", grid._name, grid._captionBottom))}
277+
{grid.GetSelectionScript()}
274278
</script>";
275279

276280
return new HtmlString(script);
@@ -285,5 +289,19 @@ private static string GetDataStr<T>(this GridBuilder<T> grid)
285289
{(string.IsNullOrEmpty(grid._data) ? string.Empty : string.Format("d.data = {0}()", grid._data))}
286290
}}";
287291
}
292+
293+
private static string GetSelectionScript<T>(this GridBuilder<T> grid)
294+
{
295+
return grid._selectEnable && grid._selectItems == SelectItems.Checkbox ?
296+
$@"$(""#{grid._name}_SelectAll"").on( ""click"", function(e) {{
297+
var {grid._name}DT = $(""#{grid._name}"").DataTable();
298+
if ($(this).is( "":checked"" )) {{
299+
{grid._name}DT.rows().select();
300+
}}
301+
else {{
302+
{grid._name}DT.rows().deselect();
303+
}}
304+
}});" : string.Empty;
305+
}
288306
}
289307
}

0 commit comments

Comments
 (0)