Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"grid.save()" returns wrong grid representation (nested and changed column count) #2493

Open
adiessl opened this issue Oct 5, 2023 · 3 comments
Labels

Comments

@adiessl
Copy link

adiessl commented Oct 5, 2023

Subject of the issue

When using grid.save() after particular changes, the grid representation that gets returned is incorrect.

Your environment

  • version of gridstack.js: 9.3.0
  • which browser/OS: Windows 11, Google Chrome 117

Steps to reproduce

https://jsfiddle.net/vs81frby/

This Fiddle contains two grids, each one having two columns. The grids use the same options:

var options = {
  disableOneColumnMode: true,
  column: 2,
  row: 5,
  cellHeight: "50px",
  acceptWidgets: true,
  float: true
};

The grid on the right contains two items initially, labeled A and B. B is dragged to the left grid to position x: 1, y: 2. Item A is then resized to span the whole width of the right grid, thus having w: 2. It is then also moved to the left grid to position x: 0, y: 4. grid.save() returns x: 1, y: 2 in this case, however (the width returned is still correct: w: 2). A is then resized to be one column wide again – grid.save() incorrectly returns w: 6 now. Moving A to the top left position in the left grid should see grid.save() returning x: 0, y: 0, but it returns x: 1, y: -2.

The following GIF shows this behavior as well:

gridstack

Expected behavior

x, y, w and h should reflect the real values.

This might only be a bug in the grid.save() method, as the attributes gs-x, gs-y and gs-w on the actual DOM nodes do have the correct values.

@adumesny
Copy link
Member

adumesny commented Oct 5, 2023

looks like #2394 but that was addressed, so maybe not the correct fix then...

@adiessl
Copy link
Author

adiessl commented Oct 31, 2023

Quick update: the problem is still present in version 9.5.0.

Another, potentially related issue (that might help in diagnosing the cause of this issue?) can be seen in the following fiddle: https://jsfiddle.net/4Lpum5f2/

gridstack_2

The left grid has two columns, the right three. If an item spanning three columns is dragged to the grid spanning two columns, its w property still has the value 3, while its gs-w attribute has the correct value 2. When moving this item back to the grid with three columns, it spans two columns and its w property also has value 2 now.

Finally, for everyone coming across this problem, there is the following workaround: as the gs-* attributes apparently have the correct values, the saveCB callback parameter of grid.save can be used to manipulate the widgets before they get returned. As the current node is also passed in, it can be used directly to get the attribute values via node.el.

This callback is not documented here, however ...:
https://github.com/gridstack/gridstack.js/tree/master/doc#savesavecontent--true-savegridopt--false-gridstackwidget--gridstackoptions

... but it can be seen in the source code:

public save(saveContent = true, saveGridOpt = false, saveCB = GridStack.saveCB): GridStackWidget[] | GridStackOptions {

@adumesny adumesny changed the title "grid.save()" returns wrong grid representation "grid.save()" returns wrong grid representation (nested and changed column count) Mar 30, 2024
@adumesny
Copy link
Member

my guess has to do with having higher column count for nested grid using that instead of the current column count displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants