gridstack.js icon indicating copy to clipboard operation
gridstack.js copied to clipboard

How to define sub-grid in HTML and initialize it later in JS

Open vmihailenco opened this issue 2 years ago • 5 comments

I am trying to initialize a sub-grid from HTML attributes, but can't find many examples except this one.

I am trying something like this (https://jsfiddle.net/0wvsym1j/2/):

<div class="grid-stack" id="gs">
  <div gs-w="6" gs-h="4" gs-auto-position="true" class="grid-stack-item grid-stack-sub-grid">
    <div class="grid-stack-item-content">
      <div class="grid-stack grid-stack-nested">
        <div gs-w="3" gs-h="2" gs-auto-position="true" class="grid-stack-item">
          <div class="grid-stack-item-content">
            <div>
              Subgrid
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

And then initialize it separately:

let grid = GridStack.init(options, '#gs')
let subgrids = GridStack.initAll({
    ...options,
    column: 'auto',
  },
  '.grid-stack-nested'
)

Could someone suggest a proper way?

vmihailenco avatar Nov 28 '23 15:11 vmihailenco

you can't init nested by dom attr. my focus has been dynamic grids (since they need to read/write them) using API options eg: https://gridstackjs.com/demo/nested.html, not dom values (which doesn't support all options anyway). see slack channel, someone also was trying...

adumesny avatar Nov 28 '23 17:11 adumesny

@adumesny I am on Slack, but can't find anything similar using the search...

Is it possible to initialize GridStack using API and content from HTML? I am looking for something like:

let grid = new GridStack(options)

let items = document.queryElementsByClass('grid-stack-item')
for (let item of items) {
    grid.makeWidget(item)
}

let subgrids = document.queryElementsByClass('grid-stack-nested')
for (let subgrid of subgrids) {
    grid.makeSubGrid(subgird, suboptions)
}

The use case is to render components using Vue.js and then initialize GridStack on top of it using DOM attributes or JS API.

vmihailenco avatar Nov 29 '23 08:11 vmihailenco

This almost works, but the positioning and sizing is off: https://jsfiddle.net/kznc3vr7/2/

vmihailenco avatar Nov 29 '23 10:11 vmihailenco

if you want to donate for this feature (init nested from DOM) it could be added, but frankly not of my radar...

adumesny avatar Mar 30 '24 21:03 adumesny

This functional requirement greatly improves the flexibility of nested layout!

carvinlo avatar Jul 30 '24 03:07 carvinlo