Skip to content

Commit 85777fe

Browse files
authored
docs(Grid): Simplify and show args.Field in Grid row event examples (#2788)
* docs(Grid): Simplify and show args.Field in Grid row event examples * Add missing lines in TreeList examples
1 parent b3fc748 commit 85777fe

File tree

4 files changed

+103
-149
lines changed

4 files changed

+103
-149
lines changed

_contentTemplates/common/click-events.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ The event *does not fire* when the user:
3232

3333
#end
3434

35-
#rowclick-args-example
36-
37-
if (args.EventArgs is KeyboardEventArgs keyboardEventArgs)
38-
{
39-
Console.WriteLine($"The user pressed {keyboardEventArgs.Key} on row {model.Name} and column {args.Field}.");
40-
}
41-
else if (args.EventArgs is MouseEventArgs mouseEventArgs)
42-
{
43-
Console.WriteLine($"The user clicked {mouseEventArgs.ClientX} {mouseEventArgs.ClientY} on row {model.Name} and column {args.Field}.");
44-
}
45-
46-
#end
47-
4835
#rowcontextmenu
4936

5037
The `OnRowContextMenu` event fires when the user:

components/grid/events.md

Lines changed: 97 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -491,98 +491,61 @@ The `OnRowClick` event handler receives a `GridRowClickEventArgs` argument, whic
491491

492492
@[template](/_contentTemplates/common/click-events.md#clickeventargs)
493493

494-
>caption Use the OnRowClick event to load data on demand based on the clicked row
494+
>caption Using the Grid OnRowClick event
495495
496496
````RAZOR
497-
@* Use the OnRowClick event to load data on demand based on the clicked row *@
498-
499-
There is a deliberate delay in the data loading to showcase the async nature of the event
500-
501-
<TelerikGrid Data="@MyData"
502-
Height="400px"
503-
Width="700px"
504-
Pageable="true"
505-
OnRowClick="@OnRowClickHandler"
506-
SelectionMode="@GridSelectionMode.Single"
507-
@bind-SelectedItems="@SelectedItems">
497+
<TelerikGrid Data="@GridData"
498+
OnRowClick="@OnGridRowClick"
499+
Navigable="true">
508500
<GridColumns>
509-
<GridColumn Field="@(nameof(SampleData.Id))" Width="120px" />
510-
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
511-
<GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
512-
<GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
501+
<GridColumn Field="@nameof(Product.Id)" />
502+
<GridColumn Field="@nameof(Product.Name)" />
503+
<GridColumn Field="@nameof(Product.Price)" />
504+
<GridColumn Field="@nameof(Product.Quantity)" />
513505
</GridColumns>
514506
</TelerikGrid>
515507
516-
@if (ProjectData.Any())
517-
{
518-
<br />
519-
<TelerikGrid Data="@ProjectData" AutoGenerateColumns="true"
520-
Pageable="true" PageSize="4" Width="700px">
521-
</TelerikGrid>
522-
}
508+
<p style="margin-top:1em;font-size:1.5em">@( (MarkupString)RowEventLog )</p>
523509
524510
@code {
525-
// enable single row selection to showcase the clicked row to the user. Not mandatory
526-
public IEnumerable<SampleData> SelectedItems { get; set; } = Enumerable.Empty<SampleData>();
511+
private List<Product> GridData { get; set; } = new();
527512
528-
// data that will be loaded on demand for the next components - another grid in this sample
529-
public List<ProjectModel> ProjectData { get; set; } = new List<ProjectModel>();
513+
private string RowEventLog { get; set; } = "Click a Grid cell or hit Enter while it's focused...";
530514
531-
async Task OnRowClickHandler(GridRowClickEventArgs args)
515+
private void OnGridRowClick(GridRowClickEventArgs args)
532516
{
533-
var model = args.Item as SampleData;
517+
var dataItem = (Product)args.Item;
518+
string columnField = args.Field;
519+
string userAction = args.EventArgs is KeyboardEventArgs ? "key press" : "click";
534520
535-
ProjectData = await GetProjectData(model.Id);
536-
@[template](/_contentTemplates/common/click-events.md#rowclick-args-example)
521+
RowEventLog = $@"<code>OnRowClick</code> event fired for <strong>{userAction}</strong> on
522+
row <strong>{dataItem.Id}</strong> and column <strong>{columnField}</strong>";
537523
}
538524
539-
async Task<List<ProjectModel>> GetProjectData(int id)
525+
protected override void OnInitialized()
540526
{
541-
await Task.Delay(500); // simulate loading data from a service, remove from a real app
542-
543-
ProjectData = new List<ProjectModel>()
527+
for (int i = 1; i <= 5; i++)
544528
{
545-
new ProjectModel()
546-
{
547-
ProjectManagerId = id,
548-
ProjectName = $"Project name {id}",
549-
DueDate = DateTime.Today.AddDays(-id),
550-
isActive = id % 2 == 0 ? true : false
529+
GridData.Add(new Product()
530+
{
531+
Id = i,
532+
Name = $"Name {i}",
533+
Price = Random.Shared.Next(1, 100) * 1.23m,
534+
Quantity = Random.Shared.Next(0, 1000)
535+
});
551536
}
552-
};
553-
return await Task.FromResult(ProjectData);
554537
}
555538
556-
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
557-
{
558-
Id = x,
559-
Name = "name " + x,
560-
Team = "team " + x % 5,
561-
HireDate = DateTime.Now.AddDays(-x).Date
562-
});
563-
564-
public class SampleData
539+
public class Product
565540
{
566541
public int Id { get; set; }
567-
public string Name { get; set; }
568-
public string Team { get; set; }
569-
public DateTime HireDate { get; set; }
570-
}
571-
572-
public class ProjectModel
573-
{
574-
public int ProjectManagerId { get; set; }
575-
public string ProjectName { get; set; }
576-
public DateTime DueDate { get; set; }
577-
public bool isActive { get; set; }
542+
public string Name { get; set; } = string.Empty;
543+
public decimal Price { get; set; }
544+
public int Quantity { get; set; }
578545
}
579546
}
580547
````
581548

582-
>caption The result from the code snippet above
583-
584-
![OnRowClick example](images/onrowclick-example.gif)
585-
586549
## OnRowDoubleClick
587550

588551
@[template](/_contentTemplates/common/click-events.md#rowdoubleclick)
@@ -591,57 +554,55 @@ The `OnRowDoubleClick` event handler receives a `GridRowClickEventArgs` argument
591554

592555
@[template](/_contentTemplates/common/click-events.md#clickeventargs)
593556

594-
>caption Use the OnRowDoubleClick event to receive information on the clicked row
557+
>caption Using the Grid OnRowDoubleClick event
595558
596559
````RAZOR
597-
@* Use the OnRowDoubleClick event to receive information on the row the user clicked on *@
598-
599-
<TelerikGrid Data="@MyData"
600-
Height="400px"
601-
Width="700px"
602-
Pageable="true"
603-
OnRowDoubleClick="@OnRowDoubleClickHandler">
560+
<TelerikGrid Data="@GridData"
561+
OnRowDoubleClick="@OnGridRowDoubleClick">
604562
<GridColumns>
605-
<GridColumn Field="@(nameof(SampleData.Id))" Width="120px" />
606-
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" Groupable="false" />
607-
<GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
608-
<GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
563+
<GridColumn Field="@nameof(Product.Id)" />
564+
<GridColumn Field="@nameof(Product.Name)" />
565+
<GridColumn Field="@nameof(Product.Price)" />
566+
<GridColumn Field="@nameof(Product.Quantity)" />
609567
</GridColumns>
610568
</TelerikGrid>
611569
612-
@if (!String.IsNullOrEmpty(logger))
613-
{
614-
<div>
615-
@logger
616-
</div>
617-
}
570+
<p style="margin-top:1em;font-size:1.5em">@( (MarkupString)RowEventLog )</p>
618571
619572
@code {
620-
string logger = String.Empty;
573+
private List<Product> GridData { get; set; } = new();
621574
622-
void OnRowDoubleClickHandler(GridRowClickEventArgs args)
575+
private string RowEventLog { get; set; } = "Double-click a Grid cell...";
576+
577+
private void OnGridRowDoubleClick(GridRowClickEventArgs args)
623578
{
624-
var model = args.Item as SampleData;
579+
var dataItem = (Product)args.Item;
580+
string columnField = args.Field;
625581
626-
logger = $"Double clicked on {model.Name}";
627-
@[template](/_contentTemplates/common/click-events.md#rowclick-args-example)
628-
582+
RowEventLog = $@"<code>OnRowDoubleClick</code> event fired for
583+
row <strong>{dataItem.Id}</strong> and column <strong>{columnField}</strong>";
629584
}
630585
631-
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
586+
protected override void OnInitialized()
632587
{
633-
Id = x,
634-
Name = "name " + x,
635-
Team = "team " + x % 5,
636-
HireDate = DateTime.Now.AddDays(-x).Date
637-
});
588+
for (int i = 1; i <= 5; i++)
589+
{
590+
GridData.Add(new Product()
591+
{
592+
Id = i,
593+
Name = $"Name {i}",
594+
Price = Random.Shared.Next(1, 100) * 1.23m,
595+
Quantity = Random.Shared.Next(0, 1000)
596+
});
597+
}
598+
}
638599
639-
public class SampleData
600+
public class Product
640601
{
641602
public int Id { get; set; }
642-
public string Name { get; set; }
643-
public string Team { get; set; }
644-
public DateTime HireDate { get; set; }
603+
public string Name { get; set; } = string.Empty;
604+
public decimal Price { get; set; }
605+
public int Quantity { get; set; }
645606
}
646607
}
647608
````
@@ -654,53 +615,57 @@ The `OnRowContextMenu` event handler receives a `GridRowClickEventArgs` argument
654615

655616
@[template](/_contentTemplates/common/click-events.md#clickeventargs)
656617

657-
>caption Use the Grid OnRowContextMenu event and get the data model
618+
>caption Using the Grid OnRowContextMenu event
658619
659620
````RAZOR
660-
@* Get the row model from a context menu action (right click/long tap) *@
661-
662-
<TelerikGrid Data="@MyData"
663-
Pageable="true"
664-
PageSize="6"
665-
OnRowContextMenu="@OnRowContextMenuHandler">
621+
<TelerikGrid Data="@GridData"
622+
OnRowContextMenu="@OnGridRowContextMenu"
623+
Navigable="true">
666624
<GridColumns>
667-
<GridColumn Field="@(nameof(SampleData.Id))" Width="120px" />
668-
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
669-
<GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
670-
<GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
625+
<GridColumn Field="@nameof(Product.Id)" />
626+
<GridColumn Field="@nameof(Product.Name)" />
627+
<GridColumn Field="@nameof(Product.Price)" />
628+
<GridColumn Field="@nameof(Product.Quantity)" />
671629
</GridColumns>
672630
</TelerikGrid>
673631
674-
<br />
675-
676-
@logger
632+
<p style="margin-top:1em;font-size:1.5em">@( (MarkupString)RowEventLog )</p>
677633
678634
@code {
679-
void OnRowContextMenuHandler(GridRowClickEventArgs args)
680-
{
681-
SampleData model = args.Item as SampleData;
635+
private List<Product> GridData { get; set; } = new();
682636
683-
logger = $"OnRowContextMenu event fired from right clicking on {model.Name}";
684-
@[template](/_contentTemplates/common/click-events.md#rowclick-args-example)
685-
}
637+
private string RowEventLog { get; set; } = "Right-click a Grid cell or hit the context menu key while it's focused...";
686638
687-
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
639+
private void OnGridRowContextMenu(GridRowClickEventArgs args)
688640
{
689-
Id = x,
690-
Name = "name " + x,
691-
Team = "team " + x % 5,
692-
HireDate = DateTime.Now.AddDays(-x).Date
693-
});
641+
var dataItem = (Product)args.Item;
642+
string columnField = args.Field;
643+
string userAction = args.EventArgs is KeyboardEventArgs ? "key press" : "click";
694644
695-
public string logger { get; set; } = String.Empty;
645+
RowEventLog = $@"<code>OnRowContextMenu</code> event fired for <strong>{userAction}</strong> on
646+
row <strong>{dataItem.Id}</strong> and column <strong>{columnField}</strong>";
647+
}
696648
649+
protected override void OnInitialized()
650+
{
651+
for (int i = 1; i <= 5; i++)
652+
{
653+
GridData.Add(new Product()
654+
{
655+
Id = i,
656+
Name = $"Name {i}",
657+
Price = Random.Shared.Next(1, 100) * 1.23m,
658+
Quantity = Random.Shared.Next(0, 1000)
659+
});
660+
}
661+
}
697662
698-
public class SampleData
663+
public class Product
699664
{
700665
public int Id { get; set; }
701-
public string Name { get; set; }
702-
public string Team { get; set; }
703-
public DateTime HireDate { get; set; }
666+
public string Name { get; set; } = string.Empty;
667+
public decimal Price { get; set; }
668+
public int Quantity { get; set; }
704669
}
705670
}
706671
````
-71 KB
Binary file not shown.

components/treelist/events.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,8 @@ The `OnRowClick` event handler receives a `TreeListRowClickEventArgs` argument,
987987
@code {
988988
private void OnRowClickHander(TreeListRowClickEventArgs args)
989989
{
990-
var clickedRow = args.Item as Employee;
990+
var clickedRow = (Employee)args.Item;
991+
string columnField = args.Field;
991992
}
992993
993994
public List<Employee> Data { get; set; }
@@ -1089,7 +1090,8 @@ The `OnRowDoubleClick` event handler receives a `TreeListRowClickEventArgs` argu
10891090
@code {
10901091
private void OnRowDoubleClickHander(TreeListRowClickEventArgs args)
10911092
{
1092-
var clickedRow = args.Item as Employee;
1093+
var clickedRow = (Employee)args.Item;
1094+
string columnField = args.Field;
10931095
}
10941096
10951097
public List<Employee> Data { get; set; }
@@ -1200,9 +1202,9 @@ The `OnRowContextMenu` event handler receives a `TreeListRowClickEventArgs` argu
12001202
12011203
private void OnRowContextMenuHandler(TreeListRowClickEventArgs args)
12021204
{
1203-
var clickedRow = args.Item as Employee;
1205+
var clickedRow = (Employee)args.Item;
12041206
1205-
logger = $"OnRowContextMenu event fired from right clicking on {clickedRow.Name}";
1207+
logger = $"OnRowContextMenu event fired from right clicking on row {clickedRow.Name} and column {args.Field}";
12061208
}
12071209
12081210
public List<Employee> Data { get; set; }

0 commit comments

Comments
 (0)