Reorderは、アイテムのグループ内での順序を変更するためにアイテムをドラッグできるようにするコンポーネントです。視覚的なドラッグ&ドロップのインターフェイスを提供するために、reorder group内で使用されなければなりません。
Reorderは、アイテムをドラッグ&ドロップするために使用されるアンカーです。Reorderが完了すると、reorder groupからionReorderEndイベントが発行され、completeメソッドを呼び出す必要があります。
基本的な使い方
Reorderの最も基本的な例は、アイテムの中にスロットを入れることです。デフォルトでは、reorder groupのreorder機能は無効になっています。reorder groupの disabled プロパティを false に設定することで有効にすることができます。Reorderアイコンを使ってアイテムをドラッグ&ドロップし、並び替えることができます。
import { Component } from '@angular/core';
import { IonItem, IonLabel, IonList, IonReorder, IonReorderGroup, ReorderEndCustomEvent } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonItem, IonLabel, IonList, IonReorder, IonReorderGroup],
})
export class ExampleComponent {
handleReorderEnd(event: ReorderEndCustomEvent) {
// The `from` and `to` properties contain the index of the item
// when the drag started and ended, respectively
console.log('Dragged from index', event.detail.from, 'to', event.detail.to);
// Finish the reorder and position the item in the DOM based on
// where the gesture ended. This method can also be called directly
// by the reorder group.
event.detail.complete();
}
}並べ替えの切り替え
場合によっては、並べ替えの機能を切り替えるオプションが欲しいかもしれない。これは、関数や変数に基づいて disabled プロパティをリアクティブにすることで実現できます。
import { Component } from '@angular/core';
import {
IonButton,
IonItem,
IonLabel,
IonList,
IonReorder,
IonReorderGroup,
ReorderEndCustomEvent,
} from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton, IonItem, IonLabel, IonList, IonReorder, IonReorderGroup],
})
export class ExampleComponent {
public isDisabled = true;
handleReorderEnd(event: ReorderEndCustomEvent) {
// The `from` and `to` properties contain the index of the item
// when the drag started and ended, respectively
console.log('Dragged from index', event.detail.from, 'to', event.detail.to);
// Finish the reorder and position the item in the DOM based on
// where the gesture ended. This method can also be called directly
// by the reorder group.
event.detail.complete();
}
toggleReorder() {
this.isDisabled = !this.isDisabled;
}
}Custom Reorder Icon
Reorderコンポーネントは、iOSでは3行、Material Designでは2行のReorderアイコンを使用しています。これは、利用可能なIoniconのいずれかを使って、Reorderの中にIconコンポーネントを追加することでカスタマイズできます。
<ion-list>
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
<!-- Casting $event to $any is a temporary fix for this bug https://github.com/ionic-team/ionic-framework/issues/24245 -->
<ion-reorder-group [disabled]="false" (ionReorderEnd)="handleReorderEnd($any($event))">
<ion-item>
<ion-label> Item 1 </ion-label>
<ion-reorder slot="end">
<ion-icon name="pizza"></ion-icon>
</ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 2 </ion-label>
<ion-reorder slot="end">
<ion-icon name="pizza"></ion-icon>
</ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 3 </ion-label>
<ion-reorder slot="end">
<ion-icon name="pizza"></ion-icon>
</ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 4 </ion-label>
<ion-reorder slot="end">
<ion-icon name="pizza"></ion-icon>
</ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 5 </ion-label>
<ion-reorder slot="end">
<ion-icon name="pizza"></ion-icon>
</ion-reorder>
</ion-item>
</ion-reorder-group>
</ion-list>Reorder Wrapper
並び替えは、項目を囲むラッパーとして使うこともできます。下の項目のどこかをクリックし、ドラッグすると、リストの順番を入れ替えることができます。
import { Component } from '@angular/core';
import { IonItem, IonLabel, IonList, IonReorder, IonReorderGroup, ReorderEndCustomEvent } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonItem, IonLabel, IonList, IonReorder, IonReorderGroup],
})
export class ExampleComponent {
handleReorderEnd(event: ReorderEndCustomEvent) {
// The `from` and `to` properties contain the index of the item
// when the drag started and ended, respectively
console.log('Dragged from index', event.detail.from, 'to', event.detail.to);
// Finish the reorder and position the item in the DOM based on
// where the gesture ended. This method can also be called directly
// by the reorder group.
event.detail.complete();
}
}データの更新
パラメータを指定せずに reorder グループに対して complete メソッドを呼び出すと、DOM ノードが並べ替えられます。項目がソートが必要なデータの配列からレンダリングされた場合、データと DOM が同期しない可能性があります。
並べ替えの完了時に配列をソートするには、complete メソッドのパラメータとして配列を渡す必要があります。complete`メソッドは配列をソートして返し、再代入できるようにする。配列を渡すと、IonicはDOMノードの並び替えを行わないので注意してください。
場合によっては、アプリが独自に配列と DOM ノードの両方を並べ替える必要があるかもしれません。その場合は complete メソッドに false をパラメータとして渡す必要があります。これにより、Ionicはreorder group内のDOMノードの並び替えを行わないようになります。
どのようなアプローチを取るにせよ、ループの中でアイテムを提供する場合は、アイテムを並べ替えるための安定したIDを提供する必要があります。つまり、Angularでは trackBy を使用し、ReactとVueでは key を使用します。
import { Component } from '@angular/core';
import { IonItem, IonLabel, IonList, IonReorder, IonReorderGroup, ReorderEndCustomEvent } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonItem, IonLabel, IonList, IonReorder, IonReorderGroup],
})
export class ExampleComponent {
items = [1, 2, 3, 4, 5];
handleReorderEnd(event: ReorderEndCustomEvent) {
// Before complete is called with the items they will remain in the
// order before the drag
console.log('Before complete', this.items);
// Finish the reorder and position the item in the DOM based on
// where the gesture ended. Update the items variable to the
// new order of items
this.items = event.detail.complete(this.items);
// After complete is called the items will be in the new order
console.log('After complete', this.items);
}
}Event Handling
Using ionReorderStart and ionReorderEnd
The ionReorderStart event is emitted when the user begins a reorder gesture. This event fires when the user taps and holds an item, before any movement occurs. This is useful for preparing the UI for the reorder operation, such as hiding certain elements or updating the visual state of items. For example, icons in list items can be hidden while they are being dragged and shown again when the reorder is complete.
The ionReorderEnd event is emitted when the user completes the reorder gesture. This occurs when the user releases the item they are dragging, for example by lifting their finger on a touch screen or releasing the mouse button. The event includes the from and to indices of the item, as well as the complete method that should be called to finalize the reorder operation. The from index will always be the position of the item when the gesture started, while the to index will be its final position. This event will fire even if no items have changed position, in which case the from and to indices will be the same.
<ion-list>
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
<!-- Casting $event to $any is a temporary fix for this bug https://github.com/ionic-team/ionic-framework/issues/24245 -->
<ion-reorder-group
[disabled]="false"
(ionReorderStart)="handleReorderStart()"
(ionReorderEnd)="handleReorderEnd($any($event))"
>
@for (item of items; track item; let i = $index) {
<ion-item>
<ion-label>{{ item.label }}</ion-label>
<ion-icon #icon [name]="item.icon" [color]="item.color" slot="end"></ion-icon>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
}
</ion-reorder-group>
</ion-list>Using ionReorderMove
The ionReorderMove event is emitted continuously during the reorder gesture as the user drags an item. The event includes the from and to indices of the item. Unlike ionReorderEnd, the from index in this event represents the last known position of the item (which updates as the item moves), while the to index represents its current position. If the item has not changed position since the last event, the from and to indices will be the same. This event is useful for tracking position changes during the drag operation. For example, the ranking or numbering of items can be updated in real-time as they are being dragged to maintain a logical ascending order.
Do not call the complete method during the ionReorderMove event as it can break the gesture.
import { Component } from '@angular/core';
import {
IonItem,
IonLabel,
IonList,
IonReorder,
IonReorderGroup,
ReorderEndCustomEvent,
ReorderMoveCustomEvent,
} from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonItem, IonLabel, IonList, IonReorder, IonReorderGroup],
})
export class ExampleComponent {
items = ['Buy groceries', 'Call the bank', 'Finish project report', 'Book flight tickets', 'Read a book'];
handleReorderMove(event: ReorderMoveCustomEvent) {
const from = event.detail.from;
const to = event.detail.to;
if (from !== to) {
console.log('Dragged from index', from, 'to', to);
}
// Get all items and sort by their current id (item-1, item-2, ...)
const itemElements = Array.from(document.querySelectorAll('ion-item')).sort((a, b) => {
const aNum = parseInt(a.id.replace('item-', ''), 10);
const bNum = parseInt(b.id.replace('item-', ''), 10);
return aNum - bNum;
});
// Dragging down: shift up items between from+1 and to, set dragged to to+1
if (from < to) {
for (let i = from; i <= to; i++) {
const item = itemElements[i];
const itemNum = item.querySelector('b');
if (itemNum) {
if (i === from) {
// Dragged item
itemNum.textContent = String(to + 1);
item.id = `item-${to + 1}`;
} else {
// Items shift up
itemNum.textContent = String(i);
item.id = `item-${i}`;
}
}
}
// Dragging up: shift down items between to and from-1, set dragged to to+1
} else if (from > to) {
for (let i = to; i <= from; i++) {
const item = itemElements[i];
const itemNum = item.querySelector('b');
if (itemNum) {
if (i === from) {
// Dragged item
itemNum.textContent = String(to + 1);
item.id = `item-${to + 1}`;
} else {
// Items shift down
itemNum.textContent = String(i + 2);
item.id = `item-${i + 2}`;
}
}
}
}
}
handleReorderEnd(event: ReorderEndCustomEvent) {
// Finish the reorder and update the items data
this.items = event.detail.complete(this.items);
}
}仮想スクロールでの使用
Reorderを正しく動作させるには、スクロールコンテナが必要です。仮想スクロールソリューションを使用する場合は、カスタムスクロールターゲットを指定する必要があります。コンテンツのスクロールを無効にし、スクロールを担当する要素に .ion-content-scroll-host クラスを追加する必要があります。
<ion-content [scrollY]="false">
<div class="ion-content-scroll-host">
<ion-list>
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
<!-- Casting $event to $any is a temporary fix for this bug https://github.com/ionic-team/ionic-framework/issues/24245 -->
<ion-reorder-group [disabled]="false" (ionReorderEnd)="handleReorderEnd($any($event))">
<ion-item>
<ion-label> Item 1 </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 2 </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 3 </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 4 </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
<ion-item>
<ion-label> Item 5 </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
</ion-reorder-group>
</ion-list>
</div>
</ion-content>プロパティ
No properties available for this component.
イベント
No events available for this component.
メソッド
No public methods available for this component.
CSS Shadow Parts
| Name | Description |
|---|---|
icon | リオーダーハンドルのアイコンです(ion-iconを使用)。 |
CSSカスタムプロパティ
No CSS custom properties available for this component.
Slots
No slots available for this component.