@@ -210,6 +210,8 @@ export class GridStack {
210210 private _isAutoCellHeight : boolean ;
211211 /** @internal track event binding to window resize so we can remove */
212212 private _windowResizeBind : ( ) => GridStack ;
213+ /** @internal true when loading items to insert first rather than append */
214+ private _insertNotAppend : boolean ;
213215
214216 /**
215217 * Construct a grid item from the given element and options
@@ -397,7 +399,11 @@ export class GridStack {
397399 this . engine . prepareNode ( options ) ;
398400 this . _writeAttr ( el , options ) ;
399401
400- this . el . appendChild ( el ) ;
402+ if ( this . _insertNotAppend ) {
403+ this . el . prepend ( el ) ;
404+ } else {
405+ this . el . appendChild ( el ) ;
406+ }
401407
402408 // similar to makeWidget() that doesn't read attr again and worse re-create a new node and loose any _id
403409 this . _prepareElement ( el , true , options ) ;
@@ -467,6 +473,7 @@ export class GridStack {
467473 **/
468474 public load ( layout : GridStackWidget [ ] , addAndRemove : boolean | ( ( g : GridStack , w : GridStackWidget , add : boolean ) => GridItemHTMLElement ) = true ) : GridStack {
469475 let items = GridStack . Utils . sort ( layout , - 1 , this . _prevColumn || this . opts . column ) ;
476+ this . _insertNotAppend = true ; // since create in reverse order...
470477
471478 // if we're loading a layout into 1 column (_prevColumn is set only when going to 1) and items don't fit, make sure to save
472479 // the original wanted layout so we can scale back up correctly #1471
@@ -503,6 +510,7 @@ export class GridStack {
503510 let sub = item . el . querySelector ( '.grid-stack' ) as GridHTMLElement ;
504511 if ( sub && sub . gridstack ) {
505512 sub . gridstack . load ( ( w . subGrid as GridStackOptions ) . children ) ; // TODO: support updating grid options ?
513+ this . _insertNotAppend = true ; // got reset by above call
506514 }
507515 }
508516 } else if ( addAndRemove ) {
@@ -523,7 +531,7 @@ export class GridStack {
523531
524532 // after commit, clear that flag
525533 delete this . _ignoreLayoutsNodeChange ;
526-
534+ delete this . _insertNotAppend ;
527535 return this ;
528536 }
529537
0 commit comments