Skip to content

Commit 300c3b4

Browse files
authored
docs(grid): Enhance column autofit KB with OnRead example (#1074)
* docs(grid): Enhance column autofit KB with OnRead example * docs(grid): Add ticketid to KB
1 parent 67bbb6e commit 300c3b4

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

knowledge-base/grid-autofit-columns.md

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
2-
title: Autofit all Grid columns on initial load.
3-
description: Autofit all Grid columns on initial page load, so that their widths match their content.
2+
title: Autofit All Grid Columns on Initial Load
3+
description: How to auto fit all Grid columns on initial page load, so that their widths match their content.
44
type: how-to
55
page_title: Autofit all Grid columns on initial page load
66
slug: grid-autofit-columns-on-initial-load
77
position:
88
tags: grid, autofit, columns
9+
ticketid: 1568815
910
res_type: kb
1011
---
1112

@@ -20,6 +21,10 @@ In the 2.28.0 release of Telerik UI for Blazor, the Grid introduced [methods to
2021

2122
## Solution
2223

24+
There are two possible implementations to autofit Grid columns by default. They depend on whether the Grid is data-bound via [`Data` parameter](#data-parameter) or [`OnRead` event](#onread-event).
25+
26+
### Data Parameter
27+
2328
To AutoFit the Grid columns on initial load of the component you have to use a provision like the `MutationObserver`. This JavaScript tool notifies about DOM changes. The code snippet below uses the MutationObserver to trigger the `AutoFitAllColumns` method when the nodes in the content of Grid have mutated (rendered in this case).
2429

2530
Make sure to replace `<YOUR PROJECT NAMESPACE>` in the JavaScript code.
@@ -119,4 +124,104 @@ function observeTarget(gridClass) {
119124
observer.disconnect();
120125
}
121126
}
122-
````
127+
````
128+
129+
### OnRead Event
130+
131+
Raise a boolean flag inside the `OnRead` handler, and then use it inside `OnAfterRenderAsync` to trigger column autofit. Note that you may need a small `Task.Delay()` inside `OnAfterRenderAsync`.
132+
133+
This approach is simpler than the above, because it doesn't require JavaScript and there is somewhat greater predictability when the Grid will render its rows.
134+
135+
>caption Autofit Grid columns when using OnRead data binding
136+
137+
````CSHTML
138+
@using Telerik.DataSource
139+
@using Telerik.DataSource.Extensions
140+
141+
<TelerikButton OnClick="@AutoFit">AutoFit All Columns Manually</TelerikButton>
142+
143+
<TelerikGrid @ref="@Grid"
144+
OnRead="@OnGridRead"
145+
TItem="@Product"
146+
Pageable="true"
147+
Sortable="true"
148+
Resizable="true"
149+
FilterMode="GridFilterMode.FilterRow">
150+
<GridColumns>
151+
<GridColumn Field="@nameof(Product.Name)" Title="Product Name" />
152+
<GridColumn Field="@nameof(Product.Price)" />
153+
<GridColumn Field="@nameof(Product.ReleaseDate)" Title="Release Date" />
154+
<GridColumn Field="@nameof(Product.Active)" />
155+
</GridColumns>
156+
</TelerikGrid>
157+
158+
@code {
159+
private TelerikGrid<Product> Grid { get; set; }
160+
161+
private List<Product> GridData { get; set; }
162+
163+
private bool AutoFitFlag { get; set; }
164+
165+
private bool FirstGridBindFlag { get; set; } = true;
166+
167+
private async Task AutoFit()
168+
{
169+
Grid.AutoFitAllColumns();
170+
}
171+
172+
private async Task OnGridRead(GridReadEventArgs args)
173+
{
174+
DataSourceResult result = GridData.ToDataSourceResult(args.Request);
175+
176+
args.Data = result.Data;
177+
args.Total = result.Total;
178+
args.AggregateResults = result.AggregateResults;
179+
180+
if (FirstGridBindFlag)
181+
{
182+
// it is also possible to auto fit Grid columns on every rebind
183+
FirstGridBindFlag = false;
184+
AutoFitFlag = true;
185+
}
186+
}
187+
188+
protected override async Task OnAfterRenderAsync(bool firstRender)
189+
{
190+
if (AutoFitFlag)
191+
{
192+
AutoFitFlag = false;
193+
await Task.Delay(200);
194+
Grid.AutoFitAllColumns();
195+
}
196+
197+
await base.OnAfterRenderAsync(firstRender);
198+
}
199+
200+
protected override void OnInitialized()
201+
{
202+
GridData = new List<Product>();
203+
var rnd = new Random();
204+
205+
for (int i = 1; i <= 70; i++)
206+
{
207+
GridData.Add(new Product()
208+
{
209+
Id = i,
210+
Name = "Product " + i.ToString(),
211+
Price = (decimal)rnd.Next(1, 100),
212+
ReleaseDate = DateTime.Now.AddDays(-rnd.Next(60, 1000)),
213+
Active = i % 3 == 0
214+
});
215+
}
216+
}
217+
218+
public class Product
219+
{
220+
public int Id { get; set; }
221+
public string Name { get; set; }
222+
public decimal Price { get; set; }
223+
public DateTime ReleaseDate { get; set; }
224+
public bool Active { get; set; }
225+
}
226+
}
227+
````

0 commit comments

Comments
 (0)