Can't listen any event of grid-item in nested like resize or dnd ...?
Angular:
I tried to create a nested grid. But all events of <gridstack> only are called when I affect in direct child, nested item is not.
I accessed lib and don't see any function event in <gridstack-item> component
yeah I would need to add nesting of events to the lib, then fix the ng wrapper, which is currently not supported but could be added for the right donation.
@adumesny What would the right donation be?
PM me directly on slack. for one thing the CB signatures will have to change to now pass the grid instance, and recursively go up until outer most grid is found and call that handler instead. and do I fix that just in angular (simpler but not correct for others) or in GS as well, but then the two will conflict and might get called twice... likely half a day of work.
I managed to do it manually, but thank you for being responsive.
I managed to do it manually, but thank you for being responsive. How do you do it? Can you show me your solutions pls!
I managed to do it manually, but thank you for being responsive. How do you do it? Can you show me your solutions pls!
Yeah sure.
public recursiveAddEventHandlers(grid: GridStack) {
this.addEvents(grid);
for(let node of grid.engine.nodes) {
if('subGrid' in node) {
this.recursiveAddEventHandlers(node.subGrid!);
}
}
}
private addEvents(grid: GridStack) {
grid.on('change', (event: any, items: any) => {
console.log('changed');
// This is for if an item within a nested grid is relocated or resized, you have to check if the grid is over-extended
let itemOverextension = containerOverextendedNumberOfRows(items);
if(itemOverextension > 0) {
this.extendItemRows(items[0].grid.parentGridItem, itemOverextension);
}
})
.on('dropped', (event: any, previousNode: any, newNode: any) => {
console.log('dropped');
// This is if an item is moved into a nested grid
let itemOverextension = itemOverextendingContainerBy(newNode);
if(itemOverextension > 0) {
this.extendItemRows(newNode.grid!.parentGridItem!, itemOverextension);
}
})
.on('resizestop', (event: any, el: any) => {
console.log('resizestop');
// This is if an item which itself contains a nested grid is resized
let n = el.gridstackNode;
if(n.subGrid) {
let itemOverextension = containerOverextendedNumberOfRows(n.subGrid.engine.nodes);
if(itemOverextension > 0) {
this.extendItemRows(n, itemOverextension);
}
}
});
}
private extendItemRows(gridItem: GridStackNode, numberOfRows: number) {
this.grid!.update(gridItem.el!, {h: gridItem.h! + numberOfRows})
}
function containerOverextendedNumberOfRows(items: GridStackNode[]) {
let extendAmount = 0;
for(let item of items) {
let itemOverextension = itemOverextendingContainerBy(item);
if (itemOverextension > extendAmount) {
extendAmount = itemOverextension;
}
}
return extendAmount;
}
function itemOverextendingContainerBy(item: GridStackNode) {
if(!item.grid!.parentGridItem) {
return 0;
}
return item.y! + item.h! - item.grid!.parentGridItem.h!;
}
Note that I only have one grid at the topmost level. You'll have to call recursiveAddEventHandlers for every top-level grid. Also note this solution only works with the first layer of nested grids. It stopped working 2 layers in because there is a bug in the repo in which the GridStackItem h property is incorrect (I think it's a bug). In my application it isn't important to allow a second layer of nested grids, so I just prevented nested grids:
private customSubGridOptions: NgGridStackOptions = {
column: 'auto',
acceptWidgets: (element: Element) => {
let el = element as any;
if('subGrid' in el.gridstackNode) {
return false;
}
return true;
},
...this.customBaseOptions
}
(Edit: no need to quote entire msg again)
I really appreciate that. Thanks! @calebmgshort
It is possible to listen to events on nested grids. However, It is a bit tricky:
The GridStack instance returned by GridStack.add has a method getGridItems()
Each of the items returned by this method has a property gridstackNode?.subGrid that can be observed with method .on.
Therefore, a solution I am using is to recursively add listeners on each subGrid
If GridStack event management ever changes to a simpler one at the top level, there are cases I noticed that could help a lot if addressed:
- be able to know from which subGrid an item has originated (or get a new event moved instead of having to observe events: added and removed)
- get a change event on any widget from the top grid without having to look at all the subgrids manually
- getting listeners from the top grid added automatically to subgrids that were created programmatically with makeSubGrid
- be able to unregister granularly functions from events like addEventListener works
closign since there is a workaround and no-one wanted to donate to get this out of the box...