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

Emit layoutUpdated event when adding or removing items from grid #115

Open
vlaca99 opened this issue Mar 11, 2024 · 1 comment
Open

Emit layoutUpdated event when adding or removing items from grid #115

vlaca99 opened this issue Mar 11, 2024 · 1 comment

Comments

@vlaca99
Copy link

vlaca99 commented Mar 11, 2024

When implementing adding or removing items in grid, layoutUpdated event is not emitted and x and y values are not updated accordingly. Is there a way to trigger layoutUpdated event on add/remove?

@kova98
Copy link

kova98 commented May 2, 2024

I had the same issue, see removeItem and addItemToLayout in the playground example.

Just recalculate the y afterwards if you're going to use the value anywhere else.

removeItem(item: KtdGridLayoutItem & { id: string }) {
  this.layout = this.layout.filter(
    (layoutItem) => layoutItem.id !== item.id,
  );

  // recalculate y position of all items
  this.layout.forEach((layoutItem, index) => {
    const previousItem = this.layout[index - 1];
    if (previousItem && layoutItem.y - previousItem.y > 1) {
      layoutItem.y = previousItem.y + 1;
    }
  });
}

On add, make sure to create a new variable rather than changing the existing one

  addItem(x: number, y: number, width: number, height: number, id: string) {
    const item: KtdGridLayoutItem = {
      x: x,
      y: y,
      w: width,
      h: height,
      id: id,
    };
    
    this.layout = [...this.layout, item];
  }

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

No branches or pull requests

2 participants