Comments (4)
The problem is that you are deleting a grid item from the layout, but not from your html. Check out this code:
<ktd-grid [cols]="cols"
[rowHeight]="rowHeight"
[layout]="layout"
[compactType]="compactType"
[gap]="gap"
(layoutUpdated)="onLayoutUpdated($event)"
(gridItemResize)="onGridItemResize($event)">
<ktd-grid-item id="0">
<div>Card 0</div>
<div class="grid-item-remove-handle" (mousedown)="stopEventPropagation($event)" (click)="removeItem('0')">
</div>
</ktd-grid-item>
<ktd-grid-item id="1">
<div>Card 1</div>
<div class="grid-item-remove-handle" (mousedown)="stopEventPropagation($event)" (click)="removeItem('1')">
</div>
</ktd-grid-item>
<ktd-grid-item id="2">
<div>Card 2</div>
<div class="grid-item-remove-handle" (mousedown)="stopEventPropagation($event)" (click)="removeItem('2')">
</div>
</ktd-grid-item>
</ktd-grid>
All 3 grid items will always be projected even if they are removed! Later, Angular Grid Layout can't find the grid item projected in the layout, here the error.
The recommended solution is render the grid items using the layout value:
<ktd-grid
[cols]="cols"
[rowHeight]="rowHeight"
[layout]="layout"
[compactType]="compactType"
[gap]="gap"
(layoutUpdated)="onLayoutUpdated($event)"
(gridItemResize)="onGridItemResize($event)"
>
<ktd-grid-item *ngFor="let layoutItem of layout" [id]="layoutItem.id">
<div>Card {{ layoutItem.id }}</div>
<div
class="grid-item-remove-handle"
(mousedown)="stopEventPropagation($event)"
(click)="removeItem(layoutItem.id)"
></div>
</ktd-grid-item>
</ktd-grid>
Here is an stackblitz fork with the solution stackblitz
from angular-grid-layout.
Thanks, but I'm already using this approach form the playground project:
removeItem(id: string) {
// Important: Don't mutate the array. Let Angular know that the layout has changed creating a new reference.
this.layout = this.ktdArrayRemoveItem(this.layout, item => item.id === id);
}
/**
* Removes and item from an array. Returns a new array instance (it doesn't mutate the source array).
* @param array source array to be returned without the element to remove
* @param condition function that will return true for the item that we want to remove
*/
private ktdArrayRemoveItem<T>(array: T[], condition: (item: T) => boolean) {
console.log('ktdArrayRemoveItem');
const arrayCopy = [...array];
const index = array.findIndex(item => condition(item));
if (index > -1) {
arrayCopy.splice(index, 1);
}
return arrayCopy;
}
from angular-grid-layout.
@arminus Can you share the code where this error occurs? A reproduction in stackblitz would be nice.
from angular-grid-layout.
Here you go:
https://stackblitz.com/edit/angular-stpfqg?file=src/app/app.component.ts
Click on one of the X buttons to see the issue
from angular-grid-layout.
Related Issues (20)
- Add v0 branch HOT 1
- Angular 16 support to use standalone component and signals HOT 6
- Have you supported feature related in drag and drop to add new item in grid and nested grid?
- How to auto adjust the grid height based on content
- Dragging and Resizing not working on windows touch screen
- Can I give specific colour to the cell from database?
- Feature to download whole layout as a pdf
- Grid items not resizing if container is resized
- Lack of Auto-Reversion to Default Position in 'Compact type none' Mode HOT 1
- Feature Request - New Directive for Dragging Items into Grid HOT 2
- Documentation for backgroundConfig HOT 1
- Emit layoutUpdated event when adding or removing items from grid HOT 1
- Absolute width/height
- Add all other resize options (now is only available 'se-resize'). HOT 3
- How can I lock down a ktd-grid-item? Preventing any movement or replacement! HOT 6
- Support for Angular 16? HOT 1
- RTL direction HOT 2
- Setting Max-height for grid HOT 1
- Drag doesn't work properly when the drag handle is in a child component HOT 1
- Responsive number of columns HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-grid-layout.