@@ -491,98 +491,61 @@ The `OnRowClick` event handler receives a `GridRowClickEventArgs` argument, whic
491
491
492
492
@[ template] ( /_contentTemplates/common/click-events.md#clickeventargs )
493
493
494
- > caption Use the OnRowClick event to load data on demand based on the clicked row
494
+ > caption Using the Grid OnRowClick event
495
495
496
496
```` 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">
508
500
<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) " />
513
505
</GridColumns>
514
506
</TelerikGrid>
515
507
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>
523
509
524
510
@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();
527
512
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...";
530
514
531
- async Task OnRowClickHandler (GridRowClickEventArgs args)
515
+ private void OnGridRowClick (GridRowClickEventArgs args)
532
516
{
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";
534
520
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>";
537
523
}
538
524
539
- async Task<List<ProjectModel>> GetProjectData(int id )
525
+ protected override void OnInitialized( )
540
526
{
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++)
544
528
{
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
+ });
551
536
}
552
- };
553
- return await Task.FromResult(ProjectData);
554
537
}
555
538
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
565
540
{
566
541
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; }
578
545
}
579
546
}
580
547
````
581
548
582
- > caption The result from the code snippet above
583
-
584
- ![ OnRowClick example] ( images/onrowclick-example.gif )
585
-
586
549
## OnRowDoubleClick
587
550
588
551
@[ template] ( /_contentTemplates/common/click-events.md#rowdoubleclick )
@@ -591,57 +554,55 @@ The `OnRowDoubleClick` event handler receives a `GridRowClickEventArgs` argument
591
554
592
555
@[ template] ( /_contentTemplates/common/click-events.md#clickeventargs )
593
556
594
- > caption Use the OnRowDoubleClick event to receive information on the clicked row
557
+ > caption Using the Grid OnRowDoubleClick event
595
558
596
559
```` 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">
604
562
<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) " />
609
567
</GridColumns>
610
568
</TelerikGrid>
611
569
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>
618
571
619
572
@code {
620
- string logger = String.Empty ;
573
+ private List<Product> GridData { get; set; } = new() ;
621
574
622
- void OnRowDoubleClickHandler(GridRowClickEventArgs args)
575
+ private string RowEventLog { get; set; } = "Double-click a Grid cell...";
576
+
577
+ private void OnGridRowDoubleClick(GridRowClickEventArgs args)
623
578
{
624
- var model = args.Item as SampleData;
579
+ var dataItem = (Product)args.Item;
580
+ string columnField = args.Field;
625
581
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>";
629
584
}
630
585
631
- public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
586
+ protected override void OnInitialized()
632
587
{
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
+ }
638
599
639
- public class SampleData
600
+ public class Product
640
601
{
641
602
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; }
645
606
}
646
607
}
647
608
````
@@ -654,53 +615,57 @@ The `OnRowContextMenu` event handler receives a `GridRowClickEventArgs` argument
654
615
655
616
@[ template] ( /_contentTemplates/common/click-events.md#clickeventargs )
656
617
657
- > caption Use the Grid OnRowContextMenu event and get the data model
618
+ > caption Using the Grid OnRowContextMenu event
658
619
659
620
```` 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">
666
624
<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) " />
671
629
</GridColumns>
672
630
</TelerikGrid>
673
631
674
- <br />
675
-
676
- @logger
632
+ <p style="margin-top:1em;font-size:1.5em">@( (MarkupString)RowEventLog )</p>
677
633
678
634
@code {
679
- void OnRowContextMenuHandler(GridRowClickEventArgs args)
680
- {
681
- SampleData model = args.Item as SampleData;
635
+ private List<Product> GridData { get; set; } = new();
682
636
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...";
686
638
687
- public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
639
+ private void OnGridRowContextMenu(GridRowClickEventArgs args)
688
640
{
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";
694
644
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
+ }
696
648
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
+ }
697
662
698
- public class SampleData
663
+ public class Product
699
664
{
700
665
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; }
704
669
}
705
670
}
706
671
````
0 commit comments