You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _contentTemplates/grid/state.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,8 @@
1
+
#initial-state
2
+
>tip If you want to set an initial state to the grid, use a similar snippet, but in the [`OnStateInit event`]({%slug grid-state%}#set-default-initial-state)
3
+
#end
4
+
5
+
1
6
#set-sort-from-code
2
7
@* This snippet shows how to set sorting state to the grid from your code *@
Copy file name to clipboardExpand all lines: components/grid/grouping.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,6 +80,8 @@ Drag the column header of the "Team" and/or "On Vacation" column to the group pa
80
80
81
81
You can set the grid grouping from your code through the grid [state]({%slug grid-state%}). You can define the list of fields by which the grid is grouped and indexes of groups that will be collapsed (all groups are expanded by default).
Copy file name to clipboardExpand all lines: components/grid/hierarchy.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,6 +92,8 @@ Click the + icon to expand the row details
92
92
93
93
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).
* [Get and Override User Action That Changes The Grid](#get-and-override-user-action-that-changes-the-grid)
26
30
* [Initiate Editing or Inserting of an Item](#initiate-editing-or-inserting-of-an-item)
27
31
28
32
33
+
<!-- End Document Outline -->
34
+
35
+
29
36
## Basics
30
37
31
38
The grid state is a generic class whose type is determined by the type of the model you use for the grid. It contains fields that correspond to the grid behaviors which you can use to save, load and modify the grid state.
@@ -282,6 +289,9 @@ The grid state allows you to control the behavior of the grid programmatically -
282
289
283
290
>tip The individual tabs below show how you can use the state to programmatically set the grid filtering, sorting, grouping and other features.
SortDescriptors = new List<Telerik.DataSource.SortDescriptor>
331
+
{
332
+
new Telerik.DataSource.SortDescriptor{ Member = "Name", SortDirection = Telerik.DataSource.ListSortDirection.Descending }
333
+
},
334
+
FilterDescriptors = new List<Telerik.DataSource.FilterDescriptorBase>()
335
+
{
336
+
new Telerik.DataSource.FilterDescriptor() { Member = "Id", Operator = Telerik.DataSource.FilterOperator.IsLessThan, Value = 5, MemberType = typeof(int) },
337
+
}
338
+
};
339
+
340
+
args.GridState = state;
341
+
}
342
+
343
+
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
344
+
{
345
+
Id = x,
346
+
Name = "name " + x,
347
+
Team = "team " + x % 5,
348
+
HireDate = DateTime.Now.AddDays(-x).Date
349
+
});
350
+
351
+
public class SampleData
352
+
{
353
+
public int Id { get; set; }
354
+
public string Name { get; set; }
355
+
public string Team { get; set; }
356
+
public DateTime HireDate { get; set; }
357
+
}
358
+
}
359
+
````
360
+
361
+
### Get and Override User Action That Changes The Grid
362
+
363
+
Sometimes you may want to know what the user changed in the grid (e.g., when they filter, sort and so on) and even override those operations. One way to do that is to monitor the [`OnRead`]({%slug components/grid/manual-operations%}#cache-data-request) event, cache the previous `DataSourceRequest`, compare against it, alter it if needed, and implement the operations yourself. Another is to use the `OnStateChanged` event.
364
+
365
+
The example below shows the latter. Review the code comments to see how it works and to make sure you don't get issues.
366
+
367
+
>caption Know when the grid state changes, which parameter changes, and amend the change
368
+
369
+
````CSHTML
370
+
@* This example does the following:
371
+
* Logs to the console what changed in the grid
372
+
* If the user changes the Name column filtering, the filter is always overriden to "Contains" and its value to "name 1"
373
+
* if there is no filter on the ID column, the ID column is filtered with ID < 15.
Member = "Id", MemberType = typeof (int), Operator = FilterOperator.IsLessThan, Value = 15
419
+
});
420
+
}
421
+
// needed only if you will be overriding user actions or amending them
422
+
// if you only need to be notified of changes, you should not call this method
423
+
await GridRef.SetState(args.GridState);
424
+
}
425
+
}
426
+
427
+
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 300).Select(x => new SampleData
428
+
{
429
+
Id = x,
430
+
Name = "name " + x,
431
+
Team = "team " + x % 5,
432
+
HireDate = DateTime.Now.AddDays(-x).Date
433
+
});
434
+
435
+
public class SampleData
436
+
{
437
+
public int Id { get; set; }
438
+
public string Name { get; set; }
439
+
public string Team { get; set; }
440
+
public DateTime HireDate { get; set; }
441
+
}
442
+
}
443
+
````
444
+
301
445
### Initiate Editing or Inserting of an Item
302
446
303
447
The grid state lets you store the item that the user is currently working on - both an existing model that is being edited, and a new item the user is inserting. This happens automatically when you save the grid state. If you want to save on every keystroke instead of on `OnChange` - use a custom editor template and update the `EditItem` or `InsertedItem` of the state object as required, then save the state into your service.
0 commit comments