Skip to content

Commit 79d6c91

Browse files
NansiYanchevaNansiYancheva
authored andcommitted
docs(Grid): Update KB after review
1 parent 3cc6c78 commit 79d6c91

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

knowledge-base/grid-row-template-simulate-built-in-functions.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Add the Grid Built-in Functions when Using Grid Row Template
3-
description: Learn how to implement built-in Grid functions like checkbox or command columns when using the Grid's row template
2+
title: Add the Grid Built-In Functions When Using Grid Row Template
3+
description: Learn how to implement built-in Grid functions like CheckBoxColumn or CommandColumn when using the Grid's Row Template
44
type: how-to
55
page_title: Implement Built-in Functions when Using Grid Row Template
66
slug: grid-kb-row-template-simulate-built-in-functions
@@ -27,37 +27,41 @@ res_type: kb
2727
This KB article answers the following questions:
2828

2929
* How to [select rows]({%slug grid-selection-row%}) in the Grid when using a [Row Template]({%slug grid-templates-row%})?
30-
* How to add a [Checkbox column]({%slug components/grid/columns/checkbox%}) in the Grid when using a Row Template? I want to be able to select the row through its checkbox, but also to have the functionality to [select all rows]({%slug components/grid/columns/checkbox%}#parameters) from the header of the Checkbox column.
31-
* Do the built-in keyboard options to select a range of rows by clicking the **Shift** or **Ctrl** key work when using a Row Template? How to check the checkbox of the row when I select a row by clicking the row?
30+
* How to add a [checkbox column]({%slug components/grid/columns/checkbox%}) in the Grid when using a Row Template? I want to be able to select the row through its checkbox, but also to have the functionality to [select all rows]({%slug components/grid/columns/checkbox%}#parameters) from the header of the checkbox column.
31+
* Do the built-in keyboard options to select a range of rows by clicking the `Shift` or `Ctrl` key work when using a Row Template? How to check the checkbox of the row when I select a row by clicking the row?
3232
* How to add a [command column]({%slug components/grid/columns/command%}) in the Grid when using a Row Template?
33-
* When using a Row Template how to prevent the selection of Grid rows when clicking on command buttons?
33+
* When using a Row Template, how to prevent the selection of Grid rows when clicking on command buttons?
3434
* How to implement Grid column [resizing]({%slug components/grid/columns/resize%}), [auto-fitting]({%slug components/grid/columns/resize%}#autofit-columns), [visibility]({%slug grid-columns-visible%}), [locking]({%slug grid-columns-frozen%}), and [reordering]({%slug components/grid/columns/reorder%}) when using a Row Template?
3535

3636
## Solution
3737

38-
By default, using the Row Template takes the majority of built-in functionalities away from the Grid because the Grid no longer controls its own rendering. This gives you the control to add your own implementation for them. The example below showcases a possible implementation of some of the functionalities, such as row selection by clicking on a row and through a Checkbox column, column resizing and visibility, editing through command buttons, sorting, filtering.
38+
By default, using the Row Template disables most built-in functionalities of the Grid because the Grid no longer controls its own rendering. This lets you add custom implementations for these features. The [example below](#example) shows one way to implement functionalities such as row selection (both by clicking on a row and through a checkbox column), column resizing and visibility, editing through command buttons, sorting, and filtering.
3939

4040
### Selection
4141

42-
* In the [`<GridColumns>` collection]({%slug components/grid/columns/bound%}#show-data-in-a-grid) add the [`<GridCheckboxColumn>`]({%slug components/grid/columns/checkbox%}) and use the [`HeaderTemplate`]({%slug components/grid/columns/checkbox%}#header-template) to add a custom [CheckBox component]({%slug checkbox-overview%}). This CheckBox component handles the [select all rows]({%slug components/grid/columns/checkbox%}#parameters) functionallity.
43-
* In the `<RowTemplate>`, add a `<td>` element with a custom CheckBox component. Add a Boolean property to the Grid model to indicate selection so you can use it for the state of this CheckBox.
44-
* Handle the Grid's [`SelectedItemsChanged` event]({%slug grid-selection-row%}#selecteditemschanged-event) and the CheckBox's [`OnChange` event]({%slug checkbox-events%}#onchange) to manage the [`SelectedItems` collection]({%slug grid-selection-overview%}#access-selected-rows-or-cells). The `OnChange` event fires after the `SelectedItemsChanged` event. In this case, you need to create a separate collection of selected items to persist the selected items when multiselecting through the CheckBox.
42+
To implement custom selection functionality:
43+
44+
* In the [`<GridColumns>` collection]({%slug components/grid/columns/bound%}#show-data-in-a-grid) add the [`<GridCheckboxColumn>`]({%slug components/grid/columns/checkbox%}) and use the [`HeaderTemplate`]({%slug components/grid/columns/checkbox%}#header-template) to add a [CheckBox component]({%slug checkbox-overview%}). This CheckBox component handles the [select all rows]({%slug components/grid/columns/checkbox%}#parameters) functionallity.
45+
* In the Row Template, add a `<td>` element with a CheckBox component. Add a Boolean property to the Grid model to indicate selection so you can use it for the state of this CheckBox.
46+
* Handle the Grid's [`SelectedItemsChanged` event]({%slug grid-selection-row%}#selecteditemschanged-event) and the CheckBox's [`OnChange` event]({%slug checkbox-events%}#onchange) to manage the [`SelectedItems` collection]({%slug grid-selection-overview%}#access-selected-rows-or-cells). The `OnChange` event fires after the `SelectedItemsChanged` event. In this case, you need to create a separate collection of selected items to persist the selected items when multiselecting through the checkbox column.
4547

4648
### Editing, Sorting, Filtering
4749

48-
The built-in editing, sorting, and filtering will work if the RowTemplate structure is similar to an actual table and only for the first Grid data model property included in the `<td>` element, if any.
50+
The built-in editing, sorting, and filtering will work if the Row Template structure is similar to an actual table and only for the first Grid data model property included in the `<td>` element, if any.
4951

5052
### Command Column
5153

54+
To implement a custom command column:
55+
5256
* In the `<GridColumns>` collection add the [`<GridCommandColumn>`]({%slug components/grid/columns/command%}) and use the [built-in `Save` and `Cancel` commands]({%slug components/grid/columns/command%}#built-in-commands).
53-
* In the `<RowTemplate>` add a `<td>` element with custom [Button component]({%slug components/button/overview%}) and handle the Grid items editing and deleting programmatically. You can refer to the knowledge base article on how to [enter and exit Grid edit mode programmatically]({%slug grid-kb-add-edit-state%}).
57+
* In the Row Template add a `<td>` element with a [Button component]({%slug components/button/overview%}) and handle the Grid items editing and deleting programmatically. Refer to the knowledge base article on how to [enter and exit Grid edit mode programmatically]({%slug grid-kb-add-edit-state%}).
5458
* Set the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation" target="_blank">`stopPropagation` method</a> of the <a href="https://www.w3schools.com/jsref/event_onclick.asp" target="_blank">`onclick` event</a> to the `<td>` element to prevent row selection when clicking a command button.
5559

5660
### Column Resizing, Auto-Fitting, Visibility, Locking, Reordering
5761

5862
* Column resizing and auto-fitting will work if the Row Template structure resembles an actual table row, with a corresponding number of cells matching the Grid columns.
59-
* The column visibility depends if you include a `<td>` element for the column in the Row Template.
60-
* To implement column locking, add the `k-grid-content-sticky` class to the `<td>` element of the columns that you want locked, and calculate and set the correct `left` and `right` CSS properties, as the content inside the template can be any valid HTML.
63+
* Column visibility depends on including a `<td>` element for the column in the Row Template.
64+
* To implement column locking, add the `k-grid-content-sticky` class to the `<td>` element of the columns that you want locked, and calculate and set the correct `left` and `right` CSS properties, as the content inside the Row Template can be any valid HTML.
6165
* For column reordering, manage the `left` and `right` CSS properties on the `<td>` elements within the Row Template.
6266

6367
## Example
@@ -69,7 +73,6 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
6973
Data=@GridData
7074
Pageable="true"
7175
PageSize="15"
72-
Page="1"
7376
Sortable="true"
7477
Resizable="true"
7578
FilterMode="@GridFilterMode.FilterMenu"
@@ -134,6 +137,7 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
134137
private bool SelectAll { get; set; }
135138
136139
#region Selection
140+
137141
private void SelectAllHandler(bool newValue)
138142
{
139143
SelectAll = newValue;
@@ -144,7 +148,7 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
144148
}
145149
146150
// If SelectAll is true, assign all items to SelectedItems,
147-
// else set it to an empty list
151+
// else set it to an empty list.
148152
SelectedItems = SelectAll ? new List<ArticleDto>(GridData) : new List<ArticleDto>();
149153
TempSelectedItemsCollection = SelectAll ? new List<ArticleDto>(GridData) : new List<ArticleDto>();
150154
}
@@ -157,7 +161,7 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
157161
}
158162
159163
// Use temporary collection to be able to persist the
160-
// selected items when multiselecting with checkboxes
164+
// selected items when multiselecting with checkboxes.
161165
TempSelectedItemsCollection = SelectedItems.ToList();
162166
SelectedItems = selectedItems;
163167
@@ -184,15 +188,17 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
184188
}
185189
186190
// The OnChange event fires after the SelectedItemsChanged
187-
// thus we need to update the SelectedItems collection
191+
// thus we need to update the SelectedItems collection.
188192
SelectedItems = TempSelectedItemsCollection;
189193
190194
foreach (var item in SelectedItems)
191195
{
192196
item.IsSelected = true;
193197
}
194198
}
199+
195200
#endregion Selection
201+
196202
#region Edit
197203
198204
private void OnCreateHandler(GridCommandEventArgs args)
@@ -244,7 +250,9 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
244250
GridRef.Rebind();
245251
}
246252
}
253+
247254
#endregion Edit
255+
248256
#region Data Generation
249257
250258
private void GetGridData()
@@ -284,7 +292,9 @@ The built-in editing, sorting, and filtering will work if the RowTemplate struct
284292
};
285293
}
286294
}
295+
287296
#endregion DataGeneration
297+
288298
}
289299
````
290300

0 commit comments

Comments
 (0)