I'm trying to use ion-reorder to reorder some items in a page. These item are organized in groups, so a group has several items. I want to organize these items inside the group, but more importantly I want to be able to change the item from a group to another.
I'm facing 2 problems currently:
- I can't update the array, consequently the item orders are not show correctly when changed.
- I can't move a item outside it's group, consequently I can't move to another group.
html
<ion-content>
<ion-list *ngFor='let group of groupArray'>
<ion-reorder-group (ionItemReorder)="doReorder($event)" disabled="false">
<ion-list-header>{{group.name}}</ion-list-header>
<ion-item *ngFor='let item of group.items'>
<ion-reorder slot="start"></ion-reorder>
<ion-label>
{{item.name}}
</ion-label>
</ion-item>
</ion-reorder-group>
</ion-list>
</ion-content>
typescript
interface Item {
id: number,
name: string
}
interface Group {
name: string,
items: Item[]
}
interface GroupArray extends Array<Group>{}
...
private groupArray: GroupArray = [
{
name: 'FirstGroup',
items: [
{
id: 1,
name: 'FirstItem'
},{
id: 2,
name: 'SecondItem'
},{
id: 3,
name: 'ThirdItem'
}
]
},
{
name: 'SecondGroup',
items: [
{
id: 4,
name: 'FourthItem'
},{
id: 5,
name: 'FifthItem'
},{
id: 6,
name: 'SixthItem'
}
]
}
];
/** Reorder */
doReorder(ev: CustomEvent<ItemReorderEventDetail>) {
this.groupArray = ev.detail.complete(this.groupArray);
}
I tried to move the <ion-reorder-group> to several positions, but this is the only one I managed to move the correct items. If I move up, above <ion-list>, the whole list becomes draggable and I can't move the item.
I understand that this is happening because the <ion-reorder-group> is created 2 times, but I can't think in a way to create only one time and maintain the same aspect, which is exactly like the documentation page that I linked. The difference is that in there they load the items individually, and the groups are just another item that can be drag around.
The other problem I believe is related to how complete() works. Although it expects any[] as a parameter, I don't think it was made to support a complex structure, just a simple array.
How can I solve these two problems?
I'm using Ionic 5 with Angular 10