Skip to content

Commit ad1a2f2

Browse files
authored
chore(grid): update hierarchy docs and state exmapple (#780)
1 parent 10f1f84 commit ad1a2f2

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

components/grid/hierarchy.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ Click the + icon to expand the row details
9191

9292
## Expand Rows From Code
9393

94-
You can choose which detail templates will be expanded from your code through the grid [state]({%slug grid-state%}) by their indexes (all detail templates are collapsed by default).
94+
You can choose which detail templates will be expanded from your code through the grid [state]({%slug grid-state%}). Its `ExpandedItems` field contains a collection of the expanded Grid items (all detail templates are collapsed by default).
95+
96+
The `ExpandedItems` collection is compared against the Grid Data collection in order to determine which rows will be expanded. The default behavior of the framework is to compare objects by their reference.
97+
98+
When the `ExpandedItems` are obtained from a different data source to the Grid (e.g., from a separate service method and not from the view-model), the references may not match and so there will be no expanded items. In such cases, you have to override the [`Equals`](https://docs.microsoft.com/en-us/dotnet/api/system.object.equals?view=net-6.0) method of the underlying model class so that it matches them, for example, by a unique identifier rather than by reference so that two objects can be equal regardless of their origin, but according to their contents. When you are overriding the `Equals` method, it is also recommended to override the [`GetHashCode`](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-6.0) method as well. A similar example is available in the [Save and Load Grid State from Browser LocalStorage]({%slug grid-state%}#save-and-load-grid-state-from-browser-localstorage) example.
9599

96100
@[template](/_contentTemplates/grid/state.md#initial-state)
97101

components/grid/selection/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Asynchronous operations such as loading data on demand should be handled in the
178178

179179
The `SelectedItems` collection is compared against the Grid `Data` collection in order to determine which rows will be highlighted. The default behavior of the framework is to compare objects by their reference.
180180

181-
When the `SelectedItems` are obtained from a different data source to the Grid (e.g., from a separate service method and not from the view-model), the references may not match and so there will be no highlighted items. In such cases, you have to override the `Equals` method of the underlying model class so that it matches them, for example, by a unique identifier rather than by reference so that two objects can be equal regardless of their origin, but according to their contents. A similar example is available in the [Initiate Editing or Inserting of an Item]({%slug grid-state%}#initiate-editing-or-inserting-of-an-item) example for the Grid State where a similar scenario exists for items deserialized from the state.
181+
When the `SelectedItems` are obtained from a different data source to the Grid (e.g., from a separate service method and not from the view-model), the references may not match and so there will be no highlighted items. In such cases, you have to override the [`Equals`](https://docs.microsoft.com/en-us/dotnet/api/system.object.equals?view=net-6.0) method of the underlying model class so that it matches them, for example, by a unique identifier rather than by reference so that two objects can be equal regardless of their origin, but according to their contents. When you are overriding the `Equals` method, it is also recommended to override the [`GetHashCode`](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-6.0) method as well. A similar example is available in the [Initiate Editing or Inserting of an Item]({%slug grid-state%}#initiate-editing-or-inserting-of-an-item) example for the Grid State where a similar scenario exists for items deserialized from the state.
182182

183183
### Handle Data Changes
184184

components/grid/state.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ The following information is present in the grid state:
9191

9292
* **Paging** - page index, offset (skip) for virtual scrolling.
9393

94-
* **Rows** - indexes of expanded detail templates.
94+
* **Rows** - list of expanded items.
9595

9696
* **Sorting** - sort descriptors (fields by which the grid is sorted, and the direction).
9797

@@ -146,6 +146,17 @@ Change something in the grid (like sort, filter, select, page, resize columns, e
146146
OnUpdate=@UpdateItem OnDelete=@DeleteItem OnCreate=@CreateItem EditMode="@GridEditMode.Inline"
147147
OnStateInit="@((GridStateEventArgs<SampleData> args) => OnStateInitHandler(args))"
148148
OnStateChanged="@((GridStateEventArgs<SampleData> args) => OnStateChangedHandler(args))">
149+
<DetailTemplate>
150+
@{
151+
var employee = context as SampleData;
152+
<TelerikGrid Data="employee.Assignments" Pageable="true" PageSize="5">
153+
<GridColumns>
154+
<GridColumn Field="AssignmentId" Title="Assignment Id"></GridColumn>
155+
<GridColumn Field="AssignmentTitle" Title="Assignment Title"></GridColumn>
156+
</GridColumns>
157+
</TelerikGrid>
158+
}
159+
</DetailTemplate>
149160
<GridColumns>
150161
<GridColumn Field="@(nameof(SampleData.Id))" Editable="false" />
151162
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
@@ -266,6 +277,8 @@ Change something in the grid (like sort, filter, select, page, resize columns, e
266277
public int Id { get; set; }
267278
public string Name { get; set; }
268279
public string Team { get; set; }
280+
public List<Assignment> Assignments { get; set; }
281+
269282
270283
// example of comparing stored items (from editing or selection)
271284
// with items from the current data source - IDs are used instead of the default references
@@ -277,6 +290,17 @@ Change something in the grid (like sort, filter, select, page, resize columns, e
277290
}
278291
return false;
279292
}
293+
294+
public override int GetHashCode()
295+
{
296+
return Id.GetHashCode();
297+
}
298+
}
299+
300+
public class Assignment
301+
{
302+
public int AssignmentId { get; set; }
303+
public string AssignmentTitle { get; set; }
280304
}
281305
282306
async Task GetGridData()
@@ -307,12 +331,11 @@ Change something in the grid (like sort, filter, select, page, resize columns, e
307331
{
308332
for (int i = 1; i < 50; i++)
309333
{
310-
_data.Add(new SampleData()
311-
{
312-
Id = i,
313-
Name = "name " + i,
314-
Team = "team " + i % 5
315-
});
334+
SampleData employee = new SampleData { Id = i, Name = $"Name {i}", Team = "team " + i % 5 };
335+
336+
employee.Assignments = Enumerable.Range(1, 15).Select(x => new Assignment { AssignmentId = x, AssignmentTitle = "Assignment " + x }).ToList();
337+
338+
_data.Add(employee);
316339
}
317340
}
318341

0 commit comments

Comments
 (0)