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

Dynamically remove an element #7

Open
JamesJinnn opened this issue Sep 16, 2020 · 4 comments
Open

Dynamically remove an element #7

JamesJinnn opened this issue Sep 16, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@JamesJinnn
Copy link

When i try add an element it works great. Unfortunately, when i try to remove an element. it's didn't dynamically removed. it's leave a space over there.
Screen Shot 2020-09-16 at 12 44 09 PM

@dennisameling
Copy link
Owner

dennisameling commented Sep 16, 2020

@JamesJinnn
Thanks for reporting this issue. This is probably similar to #5. I think it should suffice if I add an ngOnDestoy() to the GridItemDirective which triggers this.tileGrid.refresh() so that the grid is updated when an item is removed. I'll have a look at this in the coming days.

Just to be sure:

  • How are you currently deleting items dynamically? Ideally with some example code so I can reproduce more easily 😊
  • If you try to resize your browser screen when you see this gap, does it re-order the grid and fix the gap automatically?

@JamesJinnn
Copy link
Author

JamesJinnn commented Sep 16, 2020

@dennisameling it does re-order the grid and fix the gap automatically after i resize your browser screen.
here is how i did for deleting items

 remove(item) {
    const find = this.tests.find(test => test === item);
    const ind = this.tests.indexOf(find);
    this.tests.splice(ind, 1);
  }

@ubergeoff
Copy link

ubergeoff commented Sep 17, 2020

I believe removing an item has changed slightly in murri >= v0.9.0. As it now expects an array:

Was playing around and found something that helped me:

When the item is destroyed:

ngOnDestroy(): void {
        this.tileGrid.removeItem(this.elRef);
    }

Then find this item and remove it from the grid:

removeItem(item: ElementRef) {
        const found = this.grid.getItems().find((t) => t._element === item.nativeElement);

        if (this._isInit && found) {
            this.grid.remove([found], { layout: true });
        }
    }

Demo with removing here: https://www.flingo.co.za/muuri/dashboard1

Code with directive here: https://github.com/ubergeoff/flingo/blob/master/libs/muuri/src/lib/directives/tile-grid.directive.ts

@dennisameling
Copy link
Owner

dennisameling commented Sep 30, 2020

Thanks @ubergeoff for catching this. There is an even easier way to create the removeItem function which I just committed in 56162d9. You can basically get the Muuri grid item by just passing the nativeElement to getItem().

@JamesJinnn can you try the new 0.2.0 version of this library please? Let's say you have the following code:

<button id="add-item-button" (click)="addToGrid()">+ Add new block</button>
<br><br>
<div #grid class="grid" muuriGrid [config]="layoutConfig">
    <div class="grid-item" muuriGridItem *ngFor="let item of blockItems">
        <div class="grid-item-content">
            {{ item }}
        </div>
    </div>
</div>

If you now remove an item from the blockItems array, the corresponding Muuri grid item is also removed and no gap should be visible.

Please let me know if that fixes the issue for you, so we can go ahead and close this issue 😊

@dennisameling dennisameling added the bug Something isn't working label Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants