|
| 1 | +--- |
| 2 | +title: Checkbox Column |
| 3 | +page_title: Grid - Checkbox Column |
| 4 | +description: Configuration options for the Telerik Blazor Grid Checkbox Column |
| 5 | +slug: components/grid/columns/checkbox |
| 6 | +tags: telerik,blazor,grid,column,selection |
| 7 | +published: True |
| 8 | +position: 2 |
| 9 | +--- |
| 10 | + |
| 11 | +# Grid Checkbox Column |
| 12 | + |
| 13 | +This article describes the configuration parameters of the Blazor `GridCheckboxColumn`. |
| 14 | + |
| 15 | +The `GridCheckboxColumn` provides an additional way for users to [select Grid rows]({%slug components/grid/selection/overview%}). By default, users can select and unselect rows by clicking anywhere on them. |
| 16 | + |
| 17 | +## Parameters |
| 18 | + |
| 19 | +The Grid checkbox column has the following exclusive parameters. For other available parameters, see the [appearance settings of bound columns]({%slug components/grid/columns/bound%}#appearance). |
| 20 | + |
| 21 | +@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout) |
| 22 | + |
| 23 | +| Parameter | Type and Default Value | Description | |
| 24 | +| --- | --- | --- | |
| 25 | +| `CheckBoxOnlySelection` | `bool` | Determines if row selection occurs only on checkbox clicks. By default, user can select rows by clicking anywhere, except on command buttons. | |
| 26 | +| `SelectAll` | `bool` <br /> (`true`) | Determines if the column header renders a checkbox to select all rows. Set this to `false` if the [Grid `SelectionMode` is `Single`]({%slug components/grid/selection/single%}). The `SelectAll` parameter has no effect when the checkbox column has a [`HeaderTemplate`](#headertemplate). | |
| 27 | +| `SelectAllMode` | `GridSelectAllMode` enum <br /> (`Current`) | Determines if the header cell checkbox selects all rows on the current page, or all rows in the Grid. `Current` selects the visible rows on the current page. `All` selects all the data items, including ones that may be currently filtered out. `All` requires the [Grid to be data-bound via its `Data` parameter, and not `OnRead`]({%slug common-features-data-binding-overview%}#how-to-provide-data). When using `OnRead`, the two `SelectAllMode`s behave identically, because the Grid controls only one page of items. | |
| 28 | + |
| 29 | +>note If the Grid is bound to `IQueriable`, a header checkbox with an `All` option will execute the query over all the data. This may be a performance hit. |
| 30 | +
|
| 31 | +## Header Template |
| 32 | + |
| 33 | +The `HeaderTemplate` of the Grid checkbox column enables developers to customize the header cell's rendering and checkbox behavior. |
| 34 | + |
| 35 | +On a side note, it is possible to [center the checkboxes in the `GridCheckboxColumn`]({%slug grid-kb-center-checkbox-column%}) without using a template. |
| 36 | + |
| 37 | +>caption Grid Checkbox Column Header Template |
| 38 | +
|
| 39 | +````CSHTML |
| 40 | +<TelerikGrid @ref="@GridRef" |
| 41 | + Data="@GridData" |
| 42 | + SelectionMode="GridSelectionMode.Multiple" |
| 43 | + @bind-SelectedItems="SelectedItems"> |
| 44 | + <GridColumns> |
| 45 | + <GridCheckboxColumn Width="140px" HeaderClass="header-select-all"> |
| 46 | + <HeaderTemplate> |
| 47 | + @{ |
| 48 | + <TelerikCheckBox @bind-Value="@SelectAllCheckBoxValue" |
| 49 | + Enabled="@SelectAllEnabled" |
| 50 | + TabIndex="-1" |
| 51 | + Indeterminate="@(SelectAllCheckBoxValue == null)" /> |
| 52 | +
|
| 53 | + <TelerikButton OnClick="@ToggleSelectAll"> |
| 54 | + @(SelectAllCheckBoxValue.HasValue && SelectAllCheckBoxValue.Value ? "Deselect All" : "Select All") |
| 55 | + </TelerikButton> |
| 56 | + } |
| 57 | + </HeaderTemplate> |
| 58 | + </GridCheckboxColumn> |
| 59 | + <GridColumn Field="@(nameof(Product.Name))" Title="Product Name" /> |
| 60 | + </GridColumns> |
| 61 | +</TelerikGrid> |
| 62 | +
|
| 63 | +<style> |
| 64 | + .k-grid .header-select-all .k-checkbox { |
| 65 | + vertical-align: middle; |
| 66 | + } |
| 67 | +
|
| 68 | + .k-grid .header-select-all, |
| 69 | + .k-grid td:first-child { |
| 70 | + text-align: center; |
| 71 | + } |
| 72 | +</style> |
| 73 | +
|
| 74 | +@code { |
| 75 | + List<Product> GridData { get; set; } |
| 76 | + TelerikGrid<Product> GridRef { get; set; } |
| 77 | + IEnumerable<Product> SelectedItems { get; set; } = Enumerable.Empty<Product>(); |
| 78 | + bool SelectAllEnabled { get; set; } |
| 79 | +
|
| 80 | + void ToggleSelectAll() |
| 81 | + { |
| 82 | + if (SelectAllCheckBoxValue.HasValue && SelectAllCheckBoxValue.Value) |
| 83 | + { |
| 84 | + SelectAllCheckBoxValue = false; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + SelectAllCheckBoxValue = true; |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + bool? SelectAllCheckBoxValue |
| 93 | + { |
| 94 | + get |
| 95 | + { |
| 96 | + if (IsAllDataSelected()) |
| 97 | + { |
| 98 | + return true; |
| 99 | + } |
| 100 | + else if (IsAnyDataSelected()) |
| 101 | + { |
| 102 | + return null; |
| 103 | + } |
| 104 | +
|
| 105 | + return false; |
| 106 | + } |
| 107 | +
|
| 108 | + set |
| 109 | + { |
| 110 | + if (value.HasValue && value.Value == true) |
| 111 | + { |
| 112 | + SelectedItems = GridRef.Data; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + SelectedItems = new List<Product>(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +
|
| 121 | + bool IsAnyDataSelected() |
| 122 | + { |
| 123 | + return GridRef.SelectedItems.Count() > 0 && GridRef.SelectedItems.Count() < GridRef.Data.Count(); |
| 124 | + } |
| 125 | +
|
| 126 | + bool IsAllDataSelected() |
| 127 | + { |
| 128 | + return GridRef.SelectedItems.Count() == GridRef.Data.Count(); |
| 129 | + } |
| 130 | +
|
| 131 | + bool GridHasData() |
| 132 | + { |
| 133 | + return GridRef.Data.Count() > 0; |
| 134 | + } |
| 135 | +
|
| 136 | + protected override void OnInitialized() |
| 137 | + { |
| 138 | + GridData = Enumerable.Range(1, 5).Select(x => new Product |
| 139 | + { |
| 140 | + Id = x, |
| 141 | + Name = "Product name " + x |
| 142 | + }).ToList(); |
| 143 | +
|
| 144 | + // OR use an empty collection to test the behavior with no data |
| 145 | + //GridData = new List<Product>(); |
| 146 | +
|
| 147 | + base.OnInitialized(); |
| 148 | + } |
| 149 | +
|
| 150 | + protected override void OnAfterRender(bool firstRender) |
| 151 | + { |
| 152 | + if (firstRender) |
| 153 | + { |
| 154 | + var gridHasData = GridHasData(); |
| 155 | +
|
| 156 | + if (SelectAllEnabled != gridHasData) |
| 157 | + { |
| 158 | + SelectAllEnabled = gridHasData; |
| 159 | + StateHasChanged(); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | + public class Product |
| 165 | + { |
| 166 | + public int Id { get; set; } |
| 167 | + public string Name { get; set; } |
| 168 | + public decimal Price { get; set; } |
| 169 | + public DateTime Released { get; set; } |
| 170 | + public bool Discontinued { get; set; } |
| 171 | + } |
| 172 | +} |
| 173 | +```` |
| 174 | + |
| 175 | +## See Also |
| 176 | + |
| 177 | +* [Live Demo: Grid Selection](https://demos.telerik.com/blazor-ui/grid/selection) |
| 178 | +* [Grid Selection Overview]({%slug components/grid/selection/overview%}) |
0 commit comments