1+ @{
2+ ViewData [" Title" ] = " Hierarchy Persist Expanded Children" ;
3+ }
4+
5+ <h1 >@ViewData ["Title"]</h1 >
6+
7+ <p >Expand the Detail Template of the Grid and click <b >Add new record</b ></p >
8+
9+ @( Html .Kendo ().Grid <Product >()
10+ .Name (" grid" )
11+ .DataSource (dataSource => dataSource
12+ .Ajax ()
13+ .Read (read => read .Action (" Read_Products" , " Hierarchy_Persist_Expanded_Children" ))
14+ .Update (update => update .Action (" Update_Products" , " Hierarchy_Persist_Expanded_Children" ))
15+ .Create (create => create .Action (" Create_Products" , " Hierarchy_Persist_Expanded_Children" ))
16+ .Model (m => {
17+ m .Id (id => id .ProductID );
18+ m .Field (f => f .ProductID ).Editable (true );
19+ })
20+ )
21+ .Editable (editable => editable .Mode (GridEditMode .InLine ))
22+ .ToolBar (t => t .Create ())
23+ .Columns (columns =>
24+ {
25+ columns .Bound (product => product .ProductID );
26+ columns .Bound (product => product .ProductName );
27+ columns .Bound (product => product .UnitsInStock );
28+ columns .Bound (product => product .Discontinued );
29+ columns .Bound (product => product .UnitPrice );
30+ columns .Bound (product => product .UnitsOnOrder );
31+ columns .Command (command => {
32+ command .Edit ();
33+ command .Destroy ();
34+ });
35+ })
36+ .Pageable ()
37+ .Sortable ()
38+ .Filterable ()
39+ .Groupable ()
40+ .ClientDetailTemplateId (" OrdersTemplate" )
41+ .Events (e => e .Edit (" onEdit" ).DetailExpand (" onDetailExpand" ).DetailCollapse (" onDetailCollapse" ).DataBound (" onDataBound" ))
42+ )
43+
44+ <script type =" text/kendo" id =" OrdersTemplate" >
45+ @(Html .Kendo ().Grid < Person> ()
46+ .Name (" People#=ProductID#" )
47+ .Columns (columns =>
48+ {
49+ columns .Bound (o => o .PersonID );
50+ columns .Bound (o => o .Name );
51+ columns .Bound (o => o .BirthDate ).Format (" {0:d}" );
52+
53+ columns .Command (command =>
54+ {
55+ command .Edit ();
56+ command .Destroy ();
57+ });
58+ })
59+ .ToolBar (toolbar => toolbar .Create ())
60+ .Editable (editable => editable .Mode (GridEditMode .InLine ))
61+ .Pageable ().Sortable ().Filterable ()
62+ .DataSource (source => source .Ajax ()
63+ .Model (model =>
64+ {
65+ model .Id (o => o .PersonID );
66+ model .Field (o => o .ProductID ).DefaultValue (0 );
67+ model .Field (o => o .PersonID ).Editable (true );
68+ })
69+ .Create (create => create .Action (" Create_People" ," Hierarchy_Persist_Expanded_Children" ))
70+ .Read (read => read .Action (" Read_People" , " Hierarchy_Persist_Expanded_Children" , new { productID = " #=ProductID#" }))
71+ .Update (update => update .Action (" Update_People" , " Hierarchy_Persist_Expanded_Children" ))
72+ )
73+ .ToClientTemplate ()
74+ )
75+ </script >
76+ <script >
77+ var expandedRows = new Set ();
78+ function onDetailExpand (e ) {
79+ console .log (" detailExpand" )
80+ var grid = e .sender ;
81+ var dataItem = grid .dataItem (e .masterRow );
82+ expandedRows .add (dataItem);
83+ }
84+ function onDetailCollapse (e ) {
85+ var grid = e .sender ;
86+ var dataItem = grid .dataItem (e .masterRow );
87+ var idToRemove;
88+ expandedRows .forEach ((item , idx ) => {
89+ var modelId = item .idField ;
90+ console .log (modelId);
91+ console .log (item[modelId], dataItem[modelId])
92+ if (item[modelId] == dataItem[modelId]) {
93+ expandedRows .delete (item)
94+ }
95+ })
96+ }
97+ function onEdit (e ) {
98+ var grid = e .sender ;
99+ expandedRows .forEach (item => {
100+ var row = $ (` tr[data-uid=${ item .uid } ]` );
101+ grid .expandRow (row);
102+ })
103+ }
104+ function onDataBound (e ) {
105+ var grid = $ (" #grid" ).data (" kendoGrid" );
106+ expandedRows .forEach (item => {
107+ var row = $ (` tr[data-uid=${ item .uid } ]` );
108+ grid .expandRow (row);
109+ })
110+ }
111+ </script >
0 commit comments