Skip to content

Commit b20c45a

Browse files
dimodiDimo Dimov
andauthored
docs: Document autofit methods for Grid and TreeList (#537)
Co-authored-by: Dimo Dimov <[email protected]>
1 parent 9ae9391 commit b20c45a

File tree

4 files changed

+119
-29
lines changed

4 files changed

+119
-29
lines changed

components/grid/columns/resize.md

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,59 @@ position: 3
1010

1111
# Resize Columns
1212

13-
The Grid lets the user resize its columns by dragging the borders between their headers.
13+
The Grid features two different column resizing mechanisms:
1414

15-
To enable the column resizing, set the `Resizable` parameter of the grid to `true`.
15+
* [Resize by Dragging](#resize-by-dragging)
16+
* [Fit to Content](#autofit-columns)
17+
18+
The [example at the end of this page](#example) shows both options in action.
19+
20+
## Resize by Dragging
21+
22+
The Grid allows users to resize columns by dragging the borders between header cells.
23+
24+
To enable such column resizing, set the `Resizable` parameter of the grid to `true`.
1625

1726
To prevent the user from resizing a certain column, set its own parameter `Resizable="false"`. Note that the user can still resize other columns around it.
1827

19-
When column resizing is enabled, a double click on the resize handle between the header cells will automatically fit the column width to the content of the header, data and footers.
28+
## Autofit Columns
29+
30+
When column resizing is enabled, a double click on the resize handle between the header cells will automatically fit the column width to the content of the header, data and footers. This will remove text wrapping in the component.
31+
32+
The Grid also exposes methods to programmatically resize columns to fit their contents:
33+
34+
* `AutoFitColumn(string id)` - autofits the column with the specified [`Id` attribute]({% slug components/grid/columns/bound%}#identification);
35+
* `AutoFitColumns(IEnumerable<string> ids)` - autofits multiple columns at once'
36+
* `AutoFitAllColumns()` - autofits all applicable columns (for example, this method does not affect the hierarchy expand/collapse columns);
2037

21-
>caption Enable column resizing in Telerik Grid
38+
Autofitting specific columns preserves the current widths of all the other columns. Similar to [column resizing](#resize-by-dragging), column autofitting can trigger a horizontal Grid scrollbar, or leave empty space after the last column.
39+
40+
Programmatic autofitting works even if column resizing is disabled.
41+
42+
## Example
43+
44+
>caption How Column Resizing Works in the Telerik Grid
45+
46+
![](images/column-resize-preview.gif)
47+
48+
>caption Grid Column Resizing and Autofitting
2249
2350
````CSHTML
24-
@* Drag the border between column headers to change the column size. You cannot resize the command column. Note that actual CRUD operations and settings are not implemented here for brevity. *@
51+
@* Grid column resizing and autofitting *@
52+
@* Drag the border between column headers to change the column width. The command column is not resizable by the user. *@
53+
54+
<TelerikButton OnClick="@AutoFitSingleColumn">AutoFit ID Column</TelerikButton>
55+
<TelerikButton OnClick="@AutoFitMultipleColumns">AutoFit String Columns</TelerikButton>
56+
<TelerikButton OnClick="@AutoFitAllColumns">AutoFit All Columns</TelerikButton>
2557
26-
<TelerikGrid Data="@GridData"
58+
<TelerikGrid @ref="@Grid"
59+
Data="@GridData"
2760
Resizable="true"
2861
Pageable="true" PageSize="10" Sortable="true" Height="300px">
2962
<GridColumns>
30-
<GridColumn Field=@nameof(SampleData.Id) Title="Id" />
31-
<GridColumn Field=@nameof(SampleData.Name) Title="First Name" />
32-
<GridColumn Field=@nameof(SampleData.LastName) Title="Last Name" />
63+
<GridColumn Field=@nameof(SampleData.Id) Title="ID" Id="IDColumn" />
64+
<GridColumn Field=@nameof(SampleData.Name) Title="First Name" Id="NameColumn1" />
65+
<GridColumn Field=@nameof(SampleData.LastName) Title="Last Name" Id="NameColumn2" />
3366
<GridCommandColumn Width="100px" Resizable="false">
3467
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
3568
<GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
@@ -40,8 +73,25 @@ When column resizing is enabled, a double click on the resize handle between the
4073
</TelerikGrid>
4174
4275
@code {
76+
public TelerikGrid<SampleData> Grid { get; set; }
4377
public List<SampleData> GridData { get; set; }
4478
79+
private void AutoFitSingleColumn()
80+
{
81+
Grid.AutoFitColumn("IDColumn");
82+
}
83+
84+
private void AutoFitMultipleColumns()
85+
{
86+
var columns = new List<string>() { "NameColumn1", "NameColumn2" };
87+
Grid.AutoFitColumns(columns);
88+
}
89+
90+
private void AutoFitAllColumns()
91+
{
92+
Grid.AutoFitAllColumns();
93+
}
94+
4595
protected override void OnInitialized()
4696
{
4797
GridData = GetData();
@@ -66,11 +116,6 @@ When column resizing is enabled, a double click on the resize handle between the
66116
}
67117
````
68118

69-
70-
>caption How column resizing works in the Telerik grid
71-
72-
![](images/column-resize-preview.gif)
73-
74119
## See Also
75120

76121
* [Live Demo: Column Resizing](https://demos.telerik.com/blazor-ui/grid/column-resizing)

components/grid/columns/width.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ With regard to the widths of its columns, the scrollable (default) Grid typicall
2626

2727
* When no column widths are set, the available width is distributed evenly between all Grid columns.
2828

29-
* To allow the users to auto-fit the column widths to the content, enable [column resizing]({%slug components/grid/columns/resize%}) - a double click on the border between the headers will have the grid adjust the column width according to the size of the data, headers and footers content.
29+
* To allow the users to auto-fit the column widths to the content, enable [column resizing]({%slug components/grid/columns/resize%}) - a double click on the border between the headers will have the grid adjust the column width according to the size of the data, headers and footers content. It is also possible to [auto-fit columns programmatically]({%slug components/grid/columns/resize%}#autofit-columns).
3030

3131
# See Also
3232

components/treelist/columns/resize.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,82 @@ position: 3
1010

1111
# Resize Columns
1212

13-
The treelist lets the user resize its columns by dragging the borders between their headers.
13+
The TreeList features two different column resizing mechanisms:
14+
15+
* [Resize by Dragging](#resize-by-dragging)
16+
* [Fit to Content](#autofit-columns)
17+
18+
The [example at the end of this page](#example) shows both options in action.
19+
20+
## Resize by Dragging
21+
22+
The TreeList allows users to resize columns by dragging the borders between header cells.
1423

1524
To enable the column resizing, set the `Resizable` parameter of the treelist to `true`.
1625

1726
To prevent the user from resizing a certain column, set its own parameter `Resizable="false"`. Note that the user can still resize other columns around it.
1827

19-
When column resizing is enabled, a double click on the resize handle between the header cells will automatically fit the column width to the content of the header and data.
28+
## Autofit Columns
29+
30+
When column resizing is enabled, a double click on the resize handle between the header cells will automatically fit the column width to the content of the header, data and footers. This will remove text wrapping in the component.
31+
32+
The TreeList also exposes methods to programmatically resize columns to fit their contents:
33+
34+
* `AutoFitColumn(string id)` - autofits the column with the specified `Id` attribute;
35+
* `AutoFitColumns(IEnumerable<string> ids)` - autofits multiple columns at once;
36+
* `AutoFitAllColumns()` - autofits all applicable columns (for example, this method does not affect the hierarchy expand/collapse column);
37+
38+
Autofitting specific columns preserves the current widths of all the other columns. Similar to [column resizing](#resize-by-dragging), column autofitting can trigger a horizontal Grid scrollbar, or leave empty space after the last column.
39+
40+
Programmatic autofitting works even if column resizing is disabled.
41+
42+
## Example
43+
44+
>caption How column resizing works in the Telerik TreeList
45+
46+
![](images/column-resize-preview.gif)
47+
2048

21-
>caption Enable column resizing in Telerik treelist
49+
>caption TreeList Column Resizing and Autofitting
2250
2351
````CSHTML
24-
@* Drag the border between column headers to change the column size. You cannot resize the ID column itself. *@
52+
@* TreeList column resizing and autofitting *@
53+
@* Drag the border between column headers to change the column width. You cannot resize the ID column itself. *@
2554
26-
<TelerikTreeList Data="@Data" Resizable="true"
55+
<TelerikButton OnClick="@AutoFitSingleColumn">AutoFit Name Column</TelerikButton>
56+
<TelerikButton OnClick="@AutoFitMultipleColumns">AutoFit Id and ParentId Columns</TelerikButton>
57+
<TelerikButton OnClick="@AutoFitAllColumns">AutoFit All Columns</TelerikButton>
58+
59+
<TelerikTreeList @ref="@TreeList" Data="@Data" Resizable="true"
2760
Pageable="true" IdField="Id" ParentIdField="ParentId" Width="650px" Height="400px">
2861
<TreeListColumns>
29-
<TreeListColumn Field="Name" Expandable="true" Width="320px" />
30-
<TreeListColumn Field="Id" Resizable="false" />
31-
<TreeListColumn Field="ParentId" />
62+
<TreeListColumn Field="Name" Expandable="true" Width="320px" Id="NameColumn" />
63+
<TreeListColumn Field="Id" Resizable="false" Id="IdColumn" />
64+
<TreeListColumn Field="ParentId" Id="ParentIdColumn" />
3265
<TreeListColumn Field="HireDate" />
3366
</TreeListColumns>
3467
</TelerikTreeList>
3568
3669
@code {
70+
public TelerikTreeList<Employee> TreeList { get; set; }
3771
public List<Employee> Data { get; set; }
3872
73+
private void AutoFitSingleColumn()
74+
{
75+
TreeList.AutoFitColumn("NameColumn");
76+
}
77+
78+
private void AutoFitMultipleColumns()
79+
{
80+
var columns = new List<string>() { "IdColumn", "ParentIdColumn" };
81+
TreeList.AutoFitColumns(columns);
82+
}
83+
84+
private void AutoFitAllColumns()
85+
{
86+
TreeList.AutoFitAllColumns();
87+
}
88+
3989
protected override async Task OnInitializedAsync()
4090
{
4191
Data = await GetTreeListData();
@@ -95,11 +145,6 @@ When column resizing is enabled, a double click on the resize handle between the
95145
}
96146
````
97147

98-
99-
>caption How column resizing works in the Telerik treelist
100-
101-
![](images/column-resize-preview.gif)
102-
103148
## See Also
104149

105150
* [Live Demo: Column Resizing](https://demos.telerik.com/blazor-ui/treelist/column-resizing)

components/treelist/columns/width.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ With regard to the widths of its columns, the scrollable (default) treelist typi
2424

2525
* When no column widths are set, the available width is distributed evenly between all treelist columns.
2626

27-
* To allow the users to auto-fit the column widths to the content, enable [column resizing]({%slug treelist-columns-resize%}) - a double click on the border between the headers will have the treelist adjust the column width according to the size of the data, and header content.
27+
* To allow the users to auto-fit the column widths to the content, enable [column resizing]({%slug treelist-columns-resize%}) - a double click on the border between the headers will have the treelist adjust the column width according to the size of the data, and header content. It is also possible to [auto-fit columns programmatically]({%slug treelist-columns-resize%}#autofit-columns).
2828

2929

3030
# See Also

0 commit comments

Comments
 (0)