I would only advise that you use valid HTML markup, because the RowTemplate is rendered inside a <tr> element, so it must have only <td> elements as children, and their count must match the count of the grid columns (so it matches the header the grid generates).
This is getting popular and I am posting below a workaround devised for another ticket, which relies on a row template so you can have custom styling, a bit of binding expressions for updating the actual data, has a button that shows how you can execute logic on the current item (which you could also do through the checkbox events), and, most importantly, relies on a view model that knows whether an item is selected.
If you have solved this in a better way, you could post this here for everyone else to use until this becomes a built-in feature.
<style>
.selected {
background-color: red;
}
</style>
@
using
Telerik.Blazor.Components.Grid
@
using
Telerik.Blazor.Components.Button
<TelerikGrid Data=@MyData Height=
"500px"
>
<RowTemplate Context=
"employee"
>
<td
class
=
"@(employee.Selected ? "
selected
" : "
")"
> @* consider adding such a
class
to all the cells to
get
uniform row appearance *@
<input type=
"checkbox"
@bind=
"(employee.Selected)"
/>
@employee.Selected
<TelerikButton
class
=
"k-button k-button-list-box w-100 btn align-self-start"
OnClick=
"@( () => MoveToSelected(employee) )"
>Select @(employee.Name)</TelerikButton>
</td>
<td>
<strong>@employee.Name</strong>
</td>
<td>
Hired on: @(String.Format(
"{0:dd MMM yyyy}"
, employee.HireDate))
</td>
</RowTemplate>
<TelerikGridColumns>
<TelerikGridColumn Title=
""
Field=
"@nameof(SampleData.Selected)"
Filterable=
"false"
></TelerikGridColumn>
<TelerikGridColumn Field=@nameof(SampleData.Name) Title=
"Employee Name"
/>
<TelerikGridColumn Field=@nameof(SampleData.HireDate) Title=
"Hire Date"
/>
</TelerikGridColumns>
</TelerikGrid>
@result
@{
var selectedItems = MyData.Where(p => p.Selected ==
true
);
<ul>
@
foreach
(SampleData item
in
selectedItems)
{
<li>@item.Name</li>
}
</ul>
}
@code {
public
class
SampleData
{
public
int
ID {
get
;
set
; }
public
bool
Selected {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
DateTime HireDate {
get
;
set
; }
}
string
result {
get
;
set
; }
void
MoveToSelected(SampleData selectedEmployee)
{
SampleData currDataItem = MyData.FirstOrDefault(e => e.ID == selectedEmployee.ID);
if
(currDataItem !=
null
)
{
currDataItem.Selected = !currDataItem.Selected;
}
result = $
"ButtonClickl: selected employees number: {MyData.Where(p => p.Selected == true).Count()}, last employee clicked on: {selectedEmployee.ID}"
;
StateHasChanged();
}
public
List<SampleData> MyData {
get
;
set
; } = Enumerable.Range(1, 50).Select(x =>
new
SampleData
{
ID = x,
Name =
"name "
+ x,
HireDate = DateTime.Now.AddDays(-x)
}).ToList();
}
Regards,
Marin Bratanov
Progress Telerik UI for Blazor