The Ionic provides a <ion-reorder> component for reordering feature. It allows you to drag and drop the list items, which changes the order of the list elements. The <ion-reorder> component needs to be used with the <ion-reorder-group> to provide a visual drag and drop interface. The <ion-reorder-group> acts as a wrapper for the list elements inside <ion-reorder>.Once the user drags and drops a list of items to a new position, the ionItemReorder event is dispatched. The corresponding handler should be implemented, which calls the complete() method. The ionItemReorder event contains several properties that include all relevant information about the reorder information. This information includes from an index to new indexes. This feature is essential in many places like Song Ques, Todo list items, etc. Let us understand how the reorder component work with the following example.
In this example, we are going to discuss how we can implement a reorder component to allow items to be drag and drop to a new position. Here, we are also going to update the list items by calling the complete() method.
The HTML page contains the lists items inside the <ion-reorder-group> components, which have an iteration of items array using angular *ngFor directive. In the <ion-reorder-group>, we have set the disabled property false, which enables the drag and drop feature. The ionItemReorder is an event which enables to complete the reordering of the items.
This page contains the implementation of the onRenderItems($event). It contains the from and to index of the item dragged. Then, it uses a splice() method, which removes the dragged item from an array and assigns it to the variable draggedItem. After that, it calls the complete() method to complete the reorder operation.
When you execute the app, it will give the following output.
Next, drag any item from the list and drop it to a new position. For example, we have drag the Robert item and dropped it to the fourth position as like below image. You will also get a message in the console that moving items from 0 to 3.