2

I'm following this example, trying to implement it in Angular.

I import as follows (no jQuery dependencies):

import * as GridStack from 'gridstack/dist/gridstack-h5';

And the code

ngAfterViewInit() {
    
  var items = [
      {content: 'my first widget'}, // will default to location (0,0) and 1x1
      {w: 2, content: 'another longer widget!'} // will be placed next at (1,0) and 2x1
   ];

    var grid = GridStack.init();
    grid.load(items);
}

But all I see is static divs instead of a grid, and there are no errors. What's missing?

ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

1

Please post your HTML template.

Double check your config from GridStack Read Me, if you are using an ngfor then you need to see step 3 and invoke this on your main div $('.grid-stack').gridstack();

  1. You must install:

yarn add gridstack
// or
npm install --save gridstack
  1. You must import it into angular component 2A or 2B for HTML:

  • 2A For your answer Typescript like so:
import 'gridstack/dist/gridstack.min.css';
import GridStack from 'gridstack';

You should now have gridstack-h5.js gridstack native js version like here

  • 2B For others lookging to do it in HTML optional, like so:

    <script src="node_modules/gridstack/dist/gridstack-h5.js"></script>

    <link href="node_modules/gridstack/dist/gridstack.min.css" rel="stylesheet"/>

  1. What is your HTML template, like here

  ngAfterViewInit() {
        setTimeout(() => {
            $('.grid-stack').gridstack();
            this.gridStack = $('.grid-stack').data('gridstack');
        }, 300); //This MUST finish after dynamic data is retrieved for ngfor, and calling these statements in another area just doesn't work
    }
Transformer
  • 6,963
  • 2
  • 26
  • 52
  • Am I forced to use jQuery? The idea of Gridstack 3 is to eliminate the dependency – ps0604 Jan 11 '21 at 19:30
  • if you look at my post, you will see three options in step 4 this is native but also very new `import 'gridstack/dist/h5/gridstack-dd-native';` . So, to test I would try the other two imports as well one at a time. – Transformer Jan 11 '21 at 19:44
  • meanwhile Gridstack is JQuery free, please look at https://github.com/gridstack/gridstack.js/blob/develop/README.md#requirements – pbachman Jan 11 '21 at 19:45
  • if you use the option I mentioned above in step 4 – Transformer Jan 11 '21 at 19:46
  • I did not understand why the scripts property has to reference jquery. The question is about implementation without jquery. Can you make an example jquery free? – E.Tiake Mar 18 '21 at 12:08
  • It does not , please see my answer again. I just gave an option if they wanted to use jquery to support legacy... I will update it to clarify. you only need 1) `import 'gridstack/dist/gridstack.min.css';` /`` & if depending on if you need Drag and Drop *OR* Static grid... `import 'gridstack/dist/h5/gridstack-dd-native';` or for html link `` hope it helps – Transformer Mar 29 '21 at 04:31
  • Why are we using a settimeout here @Transformer? Adding a settimeout seems like a hack to me. – Aiguo Apr 08 '22 at 17:19