Skip to content

Commit ca739e1

Browse files
committed
#84 Added Selecting Behaviour
1 parent fcb31ad commit ca739e1

File tree

8 files changed

+322
-3
lines changed

8 files changed

+322
-3
lines changed

DatatableJS.Data/DatatableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static DataResult<T> ToDataResult<T>(this IQueryable<T> query, DataReques
7171
{
7272
if (!request.order.Any())
7373
{
74-
query = query.OrderBy(request.columns[0].data);
74+
query = query.OrderBy(request.columns[0].data ?? request.columns[1].data);
7575
}
7676
else
7777
{

DatatableJS.Net/Builders/GridBuilder.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public class GridBuilder<T>
3232
internal bool _processing { get; private set; } = true;
3333
internal bool _scrollX { get; private set; }
3434

35+
internal bool _selectEnable { get; private set; }
36+
internal SelectStyle _selectStyle { get; private set; }
37+
internal SelectItems _selectItems { get; private set; }
38+
internal bool _selectInfo { get; private set; }
39+
internal bool _selectToggleable { get; private set; }
40+
3541
internal List<ColumnDefinition> _columns = new List<ColumnDefinition>();
3642
internal List<FilterDefinition> _filters = new List<FilterDefinition>();
3743
internal List<OrderDefinition> _orders = new List<OrderDefinition>();
@@ -342,5 +348,84 @@ public GridBuilder<T> ScrollX(bool scrollX)
342348
_scrollX = scrollX;
343349
return this;
344350
}
351+
352+
/// <summary>
353+
/// Enable selection and set properties
354+
/// </summary>
355+
/// <param name="enable">Enable the selectable grid, default is false.</param>
356+
/// <param name="items">Set the selection behaviour.</param>
357+
/// <param name="style">Set the selection style.</param>
358+
/// <param name="info">Disable the visibility of selected row info on grid, default is true.</param>
359+
/// <param name="toggleable">Disable the toggleable selection, default is true.</param>
360+
/// <returns></returns>
361+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style, bool info, bool toggleable)
362+
{
363+
_selectEnable = enable;
364+
_selectItems = items;
365+
_selectStyle = style;
366+
_selectInfo = info;
367+
_selectToggleable = toggleable;
368+
369+
if (items == SelectItems.Checkbox)
370+
{
371+
var column = new ColumnDefinition
372+
{
373+
ClassName = "select-checkbox",
374+
Orderable = false,
375+
Searchable = false,
376+
Width = 5,
377+
Render = null
378+
};
379+
_columns.Insert(0, column);
380+
}
381+
382+
return this;
383+
}
384+
385+
/// <summary>
386+
/// Enable selection and set properties
387+
/// </summary>
388+
/// <param name="enable"></param>
389+
/// <param name="items"></param>
390+
/// <param name="style"></param>
391+
/// <param name="info"></param>
392+
/// <returns></returns>
393+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style, bool info)
394+
{
395+
return Selecting(enable, items, style, info, true);
396+
}
397+
398+
/// <summary>
399+
/// Enable selection and set properties
400+
/// </summary>
401+
/// <param name="enable"></param>
402+
/// <param name="items"></param>
403+
/// <param name="style"></param>
404+
/// <returns></returns>
405+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style)
406+
{
407+
return Selecting(enable, items, style, true, true);
408+
}
409+
410+
/// <summary>
411+
/// Enable selection and set properties
412+
/// </summary>
413+
/// <param name="enable"></param>
414+
/// <param name="items"></param>
415+
/// <returns></returns>
416+
public GridBuilder<T> Selecting(bool enable, SelectItems items)
417+
{
418+
return Selecting(enable, items, SelectStyle.Default, true, true);
419+
}
420+
421+
/// <summary>
422+
/// Enable selection and set properties
423+
/// </summary>
424+
/// <param name="enable"></param>
425+
/// <returns></returns>
426+
public GridBuilder<T> Selecting(bool enable)
427+
{
428+
return Selecting(enable, SelectItems.Row, SelectStyle.Default, true, true);
429+
}
345430
}
346431
}

DatatableJS.Net/JSHelper.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json;
22
using System;
3+
using System.ComponentModel;
34
using System.Globalization;
45
using System.Linq;
56
using System.Web.Mvc;
@@ -53,6 +54,15 @@ private static string ToLowString(this bool b)
5354
return b.ToString().ToLower(CultureInfo.InvariantCulture);
5455
}
5556

57+
private static string GetEnumDescription(this Enum enumValue)
58+
{
59+
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
60+
61+
var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
62+
63+
return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString();
64+
}
65+
5666
/// <summary>
5767
/// Render datatable script for prepared grid builder
5868
/// </summary>
@@ -84,6 +94,17 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> gridBuilder)
8494
}},"
8595
: string.Empty;
8696

97+
var selectInit = gridBuilder._selectEnable ?
98+
$@"select: {{
99+
info: {gridBuilder._selectInfo.ToLowString()},
100+
style: '{gridBuilder._selectStyle.GetEnumDescription()}',
101+
items: '{gridBuilder._selectItems.GetEnumDescription()}',
102+
toggleable: {gridBuilder._selectToggleable.ToLowString()},
103+
selector: '{(gridBuilder._selectItems == SelectItems.Checkbox ? "td:first-child" : string.Empty)}',
104+
blurable: true
105+
}},"
106+
: string.Empty;
107+
87108
var lengthMenu = (
88109
gridBuilder._lengthMenuValues.Count == 0
89110
) ? string.Empty :
@@ -106,6 +127,7 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> gridBuilder)
106127
processing:{gridBuilder._processing.ToLowString()},
107128
scrollX:{gridBuilder._scrollX.ToLowString()},
108129
serverSide:{gridBuilder._serverSide.ToLowString()},
130+
{selectInit}
109131
fixedColumns: {{
110132
leftColumns: {gridBuilder._leftColumns},
111133
rightColumns: {gridBuilder._rightColumns}
@@ -132,7 +154,7 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> gridBuilder)
132154
'searchable': {a.Searchable.ToLowString()},
133155
'className': '{a.ClassName}',
134156
'visible': {a.Visible.ToLowString()},
135-
'width': '{a.Width}%',
157+
'width': '{(a.Width > 0 ? $"{a.Width}%" : string.Empty)}',
136158
{(string.IsNullOrEmpty(a.Render) ? string.Empty : $"'render': function(data, type, row, meta) {{ if (data == null) {{ return ''; }} else {{ return {a.Render}; }} }}")}
137159
}}"))}]
138160
}});

DatatableJS.Net/Models/Enums.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.ComponentModel;
2+
3+
namespace DatatableJS.Net
4+
{
5+
/// <summary>
6+
/// Set which items are selectable (row, cell, checkbox). Default is row.
7+
/// </summary>
8+
public enum SelectItems
9+
{
10+
/// <summary>
11+
/// Enable row selection.
12+
/// </summary>
13+
[Description("row")]
14+
Row,
15+
16+
/// <summary>
17+
/// Enable cell selection.
18+
/// </summary>
19+
[Description("cell")]
20+
Cell,
21+
22+
/// <summary>
23+
/// Enable checkbox selection.
24+
/// </summary>
25+
[Description("row")]
26+
Checkbox
27+
}
28+
29+
/// <summary>
30+
/// Set select style functionalities.
31+
/// </summary>
32+
public enum SelectStyle
33+
{
34+
/// <summary>
35+
/// Select just for single item, to select multiple just click with CTRL button.
36+
/// </summary>
37+
[Description("os")]
38+
Default,
39+
40+
/// <summary>
41+
/// Only single item can be selected.
42+
/// </summary>
43+
[Description("single")]
44+
Single,
45+
46+
/// <summary>
47+
/// Multiple selectable with one click
48+
/// </summary>
49+
[Description("multi")]
50+
Multi
51+
}
52+
}

DatatableJS/Builders/GridBuilder.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public class GridBuilder<T>
3232
internal bool _processing { get; private set; } = true;
3333
internal bool _scrollX { get; private set; }
3434

35+
internal bool _selectEnable { get; private set; }
36+
internal SelectStyle _selectStyle { get; private set; }
37+
internal SelectItems _selectItems { get; private set; }
38+
internal bool _selectInfo { get; private set; }
39+
internal bool _selectToggleable { get; private set; }
40+
3541
internal List<ColumnDefinition> _columns = new List<ColumnDefinition>();
3642
internal List<FilterModel> _filters = new List<FilterModel>();
3743
internal List<OrderModel> _orders = new List<OrderModel>();
@@ -342,5 +348,84 @@ public GridBuilder<T> ScrollX(bool scrollX)
342348
_scrollX = scrollX;
343349
return this;
344350
}
351+
352+
/// <summary>
353+
/// Enable selection and set properties
354+
/// </summary>
355+
/// <param name="enable">Enable the selectable grid, default is false.</param>
356+
/// <param name="items">Set the selection behaviour.</param>
357+
/// <param name="style">Set the selection style.</param>
358+
/// <param name="info">Disable the visibility of selected row info on grid, default is true.</param>
359+
/// <param name="toggleable">Disable the toggleable selection, default is true.</param>
360+
/// <returns></returns>
361+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style, bool info, bool toggleable)
362+
{
363+
_selectEnable = enable;
364+
_selectItems = items;
365+
_selectStyle = style;
366+
_selectInfo = info;
367+
_selectToggleable = toggleable;
368+
369+
if (items == SelectItems.Checkbox)
370+
{
371+
var column = new ColumnDefinition
372+
{
373+
ClassName = "select-checkbox",
374+
Orderable = false,
375+
Searchable = false,
376+
Width = 5,
377+
Render = null
378+
};
379+
_columns.Insert(0, column);
380+
}
381+
382+
return this;
383+
}
384+
385+
/// <summary>
386+
/// Enable selection and set properties
387+
/// </summary>
388+
/// <param name="enable"></param>
389+
/// <param name="items"></param>
390+
/// <param name="style"></param>
391+
/// <param name="info"></param>
392+
/// <returns></returns>
393+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style, bool info)
394+
{
395+
return Selecting(enable, items, style, info, true);
396+
}
397+
398+
/// <summary>
399+
/// Enable selection and set properties
400+
/// </summary>
401+
/// <param name="enable"></param>
402+
/// <param name="items"></param>
403+
/// <param name="style"></param>
404+
/// <returns></returns>
405+
public GridBuilder<T> Selecting(bool enable, SelectItems items, SelectStyle style)
406+
{
407+
return Selecting(enable, items, style, true, true);
408+
}
409+
410+
/// <summary>
411+
/// Enable selection and set properties
412+
/// </summary>
413+
/// <param name="enable"></param>
414+
/// <param name="items"></param>
415+
/// <returns></returns>
416+
public GridBuilder<T> Selecting(bool enable, SelectItems items)
417+
{
418+
return Selecting(enable, items, SelectStyle.Default, true, true);
419+
}
420+
421+
/// <summary>
422+
/// Enable selection and set properties
423+
/// </summary>
424+
/// <param name="enable"></param>
425+
/// <returns></returns>
426+
public GridBuilder<T> Selecting(bool enable)
427+
{
428+
return Selecting(enable, SelectItems.Row, SelectStyle.Default, true, true);
429+
}
345430
}
346431
}

DatatableJS/Helpers/CommonHelpers.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System;
3+
using System.ComponentModel;
24
using System.Globalization;
35

46
namespace DatatableJS
@@ -15,5 +17,14 @@ public static GridModel GetGrid(TagHelperContext context)
1517
context.Items.TryGetValue("DataGrid", out object model);
1618
return (GridModel)model;
1719
}
20+
21+
public static string GetEnumDescription(this Enum enumValue)
22+
{
23+
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
24+
25+
var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
26+
27+
return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString();
28+
}
1829
}
1930
}

DatatableJS/JSHelper.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ public static IHtmlContent Render<T>(this GridBuilder<T> gridBuilder)
7979
}},"
8080
: string.Empty;
8181

82+
var selectInit = gridBuilder._selectEnable ?
83+
$@"select: {{
84+
info: {gridBuilder._selectInfo.ToLowString()},
85+
style: '{gridBuilder._selectStyle.GetEnumDescription()}',
86+
items: '{gridBuilder._selectItems.GetEnumDescription()}',
87+
toggleable: {gridBuilder._selectToggleable.ToLowString()},
88+
selector: '{(gridBuilder._selectItems == SelectItems.Checkbox ? "td:first-child" : string.Empty)}',
89+
blurable: true
90+
}},"
91+
: string.Empty;
92+
8293
var lengthMenu = (
8394
gridBuilder._lengthMenuValues.Count == 0
8495
) ? string.Empty :
@@ -101,6 +112,7 @@ public static IHtmlContent Render<T>(this GridBuilder<T> gridBuilder)
101112
processing:{gridBuilder._processing.ToLowString()},
102113
scrollX:{gridBuilder._scrollX.ToLowString()},
103114
serverSide:{gridBuilder._serverSide.ToLowString()},
115+
{selectInit}
104116
fixedColumns: {{
105117
leftColumns: {gridBuilder._leftColumns},
106118
rightColumns: {gridBuilder._rightColumns}
@@ -127,7 +139,7 @@ public static IHtmlContent Render<T>(this GridBuilder<T> gridBuilder)
127139
'searchable': {a.Searchable.ToLowString()},
128140
'className': '{a.ClassName}',
129141
'visible': {a.Visible.ToLowString()},
130-
'width': '{a.Width}%',
142+
'width': '{(a.Width > 0 ? $"{a.Width}%" : string.Empty)}',
131143
{(string.IsNullOrEmpty(a.Render) ? string.Empty : $"'render': function(data, type, row, meta) {{ if (data == null) {{ return ''; }} else {{ return {a.Render}; }} }}")}
132144
}}"))}]
133145
}});

0 commit comments

Comments
 (0)