8282
8383 _handler
8484 . subscribe ( _grid . onClick , handleClick )
85- . subscribe ( _grid . onSort , handleSort ) ;
85+ . subscribe ( _grid . onSort , handleSort )
86+ . subscribe ( _grid . onScroll , handleScroll ) ;
8687
8788 _grid . getData ( ) . onRowCountChanged . subscribe ( function ( ) { _grid . updateRowCount ( ) ; _grid . render ( ) ; } ) ;
8889 _grid . getData ( ) . onRowsChanged . subscribe ( function ( e , a ) { _grid . invalidateRows ( a . rows ) ; _grid . render ( ) ; } ) ;
143144 collapseAll ( ) ;
144145 }
145146
147+ // If we scroll save detail views that go out of cache range
148+ function handleScroll ( e , args ) {
149+
150+ var range = _grid . getRenderedRange ( ) ;
151+
152+ var start = ( range . top > 0 ? range . top : 0 ) ;
153+ var end = ( range . bottom > _dataView . getLength ( ) ? range . bottom : _dataView . getLength ( ) ) ;
154+
155+ // Get the item at the top of the view
156+ var topMostItem = _dataView . getItemByIdx ( start ) ;
157+
158+ // Check it is a parent item
159+ if ( topMostItem . _parent == undefined )
160+ {
161+ // This is a standard row as we have no parent.
162+ var nextItem = _dataView . getItemByIdx ( start + 1 ) ;
163+ if ( nextItem !== undefined && nextItem . _parent !== undefined )
164+ {
165+ // This is likely the expanded Detail Row View
166+ // Check for safety
167+ if ( nextItem . _parent == topMostItem )
168+ {
169+ saveDetailView ( topMostItem ) ;
170+ }
171+ }
172+ }
173+
174+ // Find the bottom most item that is likely to go off screen
175+ var bottomMostItem = _dataView . getItemByIdx ( end - 1 ) ;
176+
177+ // If we are a detailView and we are about to go out of cache view
178+ if ( bottomMostItem . _parent !== undefined )
179+ {
180+ saveDetailView ( bottomMostItem . _parent ) ;
181+
182+ }
183+ }
184+
146185 // Toggle between showing and hiding a row
147186 function toggleRowSelection ( row ) {
148187 _grid . getData ( ) . beginUpdate ( ) ;
156195 collapseItem ( _expandedRows [ i ] ) ;
157196 }
158197 }
159-
198+
199+ // Saves the current state of the detail view
200+ function saveDetailView ( item )
201+ {
202+ var view = $ ( "#innerDetailView_" + item . id ) ;
203+ if ( view )
204+ {
205+ var html = $ ( "#innerDetailView_" + item . id ) . html ( ) ;
206+ if ( html !== undefined )
207+ {
208+ item . _detailContent = html ;
209+ }
210+ }
211+ }
212+
160213 // Colapse an Item so it is notlonger seen
161214 function collapseItem ( item ) {
162215
163- // If we are loading once then lets save view when we collapse it incase it's changed
164- if ( _options . loadOnce )
165- item . _detailContent = $ ( "#innerDeatilView_" + item . id ) . html ( ) ;
216+ // Save the details on the collapse assuming onetime loading
217+ if ( _options . loadOnce ) {
218+ saveDetailView ( item ) ;
219+ }
166220
167221 item . _collapsed = true ;
168222 for ( var idx = 1 ; idx <= item . _sizePadding ; idx ++ ) {
182236 item . _collapsed = false ;
183237 _expandedRows . push ( item ) ;
184238
239+ // In the case something went wrong loading it the first time such a scroll of screen before loaded
240+ if ( ! item . _detailContent ) item . _detailViewLoaded = false ;
241+
185242 // display pre-loading template
186243 if ( ! item . _detailViewLoaded || _options . loadOnce !== true ) {
187244 item . _detailContent = _options . preTemplate ( item ) ;
331388 html . push ( "style='height:" , dataContext . _height , "px;" ) ; //set total height of padding
332389 html . push ( "top:" , rowHeight , "px'>" ) ; //shift detail below 1st row
333390 html . push ( "<div id='detailViewContainer_" , dataContext . id , "' class='detail-container' style='max-height:" + ( dataContext . _height - rowHeight + bottomMargin ) + "px'>" ) ; //sub ctr for custom styling
334- html . push ( "<div id='innerDeatilView_ " , dataContext . id , "'>" , dataContext . _detailContent , "</div></div>" ) ;
391+ html . push ( "<div id='innerDetailView_ " , dataContext . id , "'>" , dataContext . _detailContent , "</div></div>" ) ;
335392 //&omit a final closing detail container </div> that would come next
336393
337394 return html . join ( "" ) ;
345402 // Grad each of the dom items
346403 var mainContainer = document . getElementById ( 'detailViewContainer_' + item . id ) ;
347404 var cellItem = document . getElementById ( 'cellDetailView_' + item . id ) ;
348- var inner = document . getElementById ( 'innerDeatilView_ ' + item . id ) ;
405+ var inner = document . getElementById ( 'innerDetailView_ ' + item . id ) ;
349406
350407 if ( ! mainContainer || ! cellItem || ! inner ) return ;
351408
364421
365422 item . _sizePadding = Math . ceil ( ( ( rowCount * 2 ) * lineHeight ) / rowHeight ) ;
366423 item . _height = ( item . _sizePadding * rowHeight ) ;
424+
425+ // If the padding is now more than the original minRowBuff we need to increase it
426+ if ( _grid . getOptions ( ) . minRowBuffer < item . _sizePadding )
427+ {
428+ // Update the minRowBuffer so that the view doesn't disappear when it's at top of screen + the original default 3
429+ _grid . getOptions ( ) . minRowBuffer = item . _sizePadding + 3 ;
430+ }
367431
368432 mainContainer . setAttribute ( "style" , "max-height: " + item . _height + "px" ) ;
369433 if ( cellItem ) cellItem . setAttribute ( "style" , "height: " + item . _height + "px;top:" + rowHeight + "px" ) ;
388452 "resizeDetailView" : resizeDetailView
389453 } ) ;
390454 }
391- } ) ( jQuery ) ;
455+ } ) ( jQuery ) ;
0 commit comments