Skip to content

Commit 89bbf2b

Browse files
committed
add double click to datagrid row
1 parent 793c0ff commit 89bbf2b

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
<button class="btn btn-secondary" @onclick="ApplyFilter">Filter High Score</button>
99

1010

11-
<DataGrid Data="People" class="table table-hover" Filterable="true" Selectable="true" @bind-SelectedItems="Selected" @ref="DataGrid">
11+
<DataGrid Data="People"
12+
TItem="Person"
13+
Filterable="true"
14+
Selectable="true"
15+
RowDoubleClick="HandleRowClick"
16+
@bind-SelectedItems="Selected"
17+
@ref="DataGrid"
18+
class="table table-hover">
1219
<DataColumns>
1320
<DataColumn TItem="Person" Property="p => p.Id" Width="90px">
1421
<Template Context="item">
@@ -72,4 +79,8 @@
7279
await DataGrid.ApplyFilters(filters, true);
7380
}
7481

82+
private void HandleRowClick(Person person)
83+
{
84+
Console.WriteLine("Row Click: {0}", person.FullName);
85+
}
7586
}

src/LoreSoft.Blazor.Controls/Data/DataGrid.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
{
265265
<tr @key="new {item, i = 0}"
266266
@attributes="RowAttributes?.Invoke(item)"
267+
@ondblclick="() => RowDoubleClick.InvokeAsync(item)"
267268
class="@RowClass"
268269
style="@(RowStyle?.Invoke(item))">
269270
@if (DetailTemplate != null)

src/LoreSoft.Blazor.Controls/Data/DataGrid.razor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public partial class DataGrid<TItem> : DataComponentBase<TItem>
5050
[Parameter]
5151
public EventCallback<IEnumerable<TItem>> SelectedItemsChanged { get; set; }
5252

53+
[Parameter]
54+
public EventCallback<TItem> RowDoubleClick { get; set; }
5355

5456
[Parameter]
5557
public QueryGroup Query { get; set; }

src/LoreSoft.Blazor.Controls/Utilities/ConstrainedAction.cs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ namespace LoreSoft.Blazor.Controls.Utilities;
22

33
public static class ConstrainedAction
44
{
5-
public static Action<T> Debounce<T>(Action<T> action, TimeSpan interval)
5+
public static Action<T> Debounce<T>(Action<T> action, TimeSpan? delay = null)
66
{
77
if (action == null)
88
throw new ArgumentNullException(nameof(action));
99

10+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
1011
var last = 0;
1112

1213
return (T arguments) =>
@@ -20,11 +21,68 @@ public static Action<T> Debounce<T>(Action<T> action, TimeSpan interval)
2021
};
2122
}
2223

23-
public static Action<T> Throttle<T>(Action<T> action, TimeSpan interval)
24+
public static Action Debounce(Action action, TimeSpan? delay = null)
2425
{
2526
if (action == null)
2627
throw new ArgumentNullException(nameof(action));
2728

29+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
30+
var last = 0;
31+
32+
return () =>
33+
{
34+
var current = Interlocked.Increment(ref last);
35+
Task.Delay(interval).ContinueWith(task =>
36+
{
37+
if (current == last)
38+
action();
39+
});
40+
};
41+
}
42+
43+
44+
public static Func<T, Task> Debounce<T>(Func<T, Task> action, TimeSpan? delay = null)
45+
{
46+
if (action == null)
47+
throw new ArgumentNullException(nameof(action));
48+
49+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
50+
var last = 0;
51+
52+
return async (T arguments) =>
53+
{
54+
var current = Interlocked.Increment(ref last);
55+
await Task.Delay(interval);
56+
if (current == last)
57+
await action(arguments);
58+
};
59+
}
60+
61+
public static Func<Task> Debounce(Func<Task> action, TimeSpan? delay = null)
62+
{
63+
if (action == null)
64+
throw new ArgumentNullException(nameof(action));
65+
66+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
67+
var last = 0;
68+
69+
return async () =>
70+
{
71+
var current = Interlocked.Increment(ref last);
72+
await Task.Delay(interval);
73+
if (current == last)
74+
await action();
75+
};
76+
}
77+
78+
79+
public static Action<T> Throttle<T>(Action<T> action, TimeSpan? delay = null)
80+
{
81+
if (action == null)
82+
throw new ArgumentNullException(nameof(action));
83+
84+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
85+
2886
Task task = null;
2987
var locker = new object();
3088
T arguments = default;
@@ -48,4 +106,34 @@ public static Action<T> Throttle<T>(Action<T> action, TimeSpan interval)
48106
}
49107
};
50108
}
109+
110+
public static Action Throttle(Action action, TimeSpan? delay = null)
111+
{
112+
if (action == null)
113+
throw new ArgumentNullException(nameof(action));
114+
115+
var interval = delay ?? TimeSpan.FromMilliseconds(800);
116+
117+
Task task = null;
118+
var locker = new object();
119+
120+
return () =>
121+
{
122+
if (task != null)
123+
return;
124+
125+
lock (locker)
126+
{
127+
if (task != null)
128+
return;
129+
130+
task = Task.Delay(interval).ContinueWith(t =>
131+
{
132+
action();
133+
task = null;
134+
});
135+
}
136+
};
137+
}
138+
51139
}

0 commit comments

Comments
 (0)