Skip to main content

ion-reorder

shadow

重新排序是一个允许拖动项目以更改其在一组项目中的顺序的组件。它必须在 重新排序组 中使用以提供可视化的拖放界面。

¥Reorder is a component that allows an item to be dragged to change its order within a group of items. It must be used within a reorder group to provide a visual drag and drop interface.

重新排序是用于拖放项目的锚点。重新排序完成后,将从重新排序组中调度 ionReorderEnd 事件,并且需要调用 complete 方法。

¥The reorder is the anchor used to drag and drop the items. Once the reorder is complete, the ionReorderEnd event will be dispatched from the reorder group and the complete method needs to be called.

基本用法

¥Basic Usage

重新排序最基本的示例是将其放入项目内。默认情况下,重新排序组的重新排序功能处于禁用状态。可以通过将重新排序组上的 disabled 属性设置为 false 来启用它。然后可以使用重新排序图标拖放项目并重新排序。

¥The most basic example of a reorder is slotting it inside of an item. By default, the reorder functionality is disabled for a reorder group. It can be enabled by setting the disabled property on the reorder group to false. The reorder icon can then be used to drag and drop the items and reorder them.

Console
Console messages will appear here when logged from the example above.

切换重新排序

¥Toggling Reorder

在某些情况下,可能需要切换重新排序功能的选项。这可以通过基于函数或变量使 disabled 属性具有反应性来完成。

¥In some cases, it may be desired to have the option to toggle the reorder functionality. This can be done by making the disabled property reactive, based on a function or variable.

Console
Console messages will appear here when logged from the example above.

自定义重新排序图标

¥Custom Reorder Icon

重新排序组件使用一个重新排序图标,在 iOS 上为三行,在 Material Design 上为两行。这可以通过在重新排序中添加 图标 组件以及任何可用的 Ionicon 来进行自定义。

¥The reorder component uses a reorder icon with three lines on iOS and two lines on Material Design. This can be customized by adding an Icon component inside of the reorder with any of the available Ionicons.

Console
Console messages will appear here when logged from the example above.

重新排序封装

¥Reorder Wrapper

重新排序还可以用作项目的封装器,使项目本身成为锚点。单击下面项目的任意位置并将其拖动以重新排序列表。

¥Reorder can also be used as a wrapper around an item, making the item itself the anchor. Click anywhere on an item below and drag it to reorder the list.

Console
Console messages will appear here when logged from the example above.

更新数据

¥Updating Data

当在不带参数的重新排序组上调用 complete 方法时,DOM 节点将被重新排序。如果项目是从需要排序的数据数组中渲染的,则可能会导致数据和 DOM 不同步。

¥When the complete method is called on the reorder group with no parameters, the DOM nodes will be reordered. If the items are rendered from an array of data that needs to be sorted, this can result in the data and DOM being out of sync.

为了在完成重新排序后对数组进行排序,应将数组作为参数传递给 complete 方法。complete 方法将对数组进行排序并返回它,以便可以重新分配它。请注意,传递数组将阻止 Ionic 对 DOM 节点重新排序。

¥In order to sort the array upon completion of the reorder, the array should be passed as a parameter to the complete method. The complete method will sort the array and return it so it can be reassigned. Note that passing the array will prevent Ionic from reordering the DOM nodes.

在某些情况下,应用可能需要自行重新排序数组和 DOM 节点。如果需要,false 应作为参数传递给 complete 方法。这将阻止 Ionic 对重新排序组内的任何 DOM 节点进行重新排序。

¥In some cases, it may be necessary for an app to reorder both the array and the DOM nodes on its own. If this is required, false should be passed as a parameter to the complete method. This will prevent Ionic from reordering any DOM nodes inside of the reorder group.

无论采用哪种方法,如果在循环中提供,都应该提供稳定的身份来重新排序项目。这意味着 Angular 使用 track,React 和 Vue 使用 key

¥Regardless of the approach taken, a stable identity should be provided to reorder items if provided in a loop. This means using track for Angular, and key for React and Vue.

Console
Console messages will appear here when logged from the example above.

事件处理

¥Event Handling

使用 ionReorderStartionReorderEnd

¥Using ionReorderStart and ionReorderEnd

当用户开始重新排序手势时,会触发 ionReorderStart 事件。当用户点击并按住某个项目时,在发生任何移动之前,此事件会触发。这对于为重新排序操作准备 UI 非常有用,例如隐藏某些元素或更新项目的视觉状态。例如,列表项中的图标可以在拖动时隐藏,并在重新排序完成后再次显示。

¥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.

当用户完成重新排序手势时,会触发 ionReorderEnd 事件。当用户释放正在拖动的项目时,例如通过在触摸屏上抬起手指或释放鼠标按钮,会发生这种情况。该事件包含项目的 fromto 索引,以及应调用以完成重新排序操作的 complete 方法。from 索引始终是手势开始时项目的位置,而 to 索引是其最终位置。即使没有任何项目改变位置,此事件也会触发,在这种情况下,fromto 索引将相同。

¥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.

Console
Console messages will appear here when logged from the example above.

使用 ionReorderMove

¥Using ionReorderMove

在重新排序手势期间,当用户拖动项目时,会持续触发 ionReorderMove 事件。该事件包含项目的 fromto 索引。与 ionReorderEnd 不同,此事件中的 from 索引表示项目的最后已知位置(会随着项目的移动而更新),而 to 索引表示其当前位置。如果该项目自上次事件后位置未发生变化,则 fromto 索引将相同。此事件对于跟踪拖动操作期间的位置变化非常有用。例如,项目的排名或编号可以在拖动过程中实时更新,以保持逻辑上的升序。

¥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.

警告

请勿在 ionReorderMove 事件期间调用 complete 方法,因为它可能会破坏手势。

¥Do not call the complete method during the ionReorderMove event as it can break the gesture.

Console
Console messages will appear here when logged from the example above.

与虚拟滚动一起使用

¥Usage with Virtual Scroll

重新排序需要滚动容器才能正常工作。使用虚拟滚动解决方案时,需要提供自定义滚动目标。需要禁用内容滚动,并且需要将 .ion-content-scroll-host 类添加到负责滚动的元素中。

¥Reorder requires a scroll container to work properly. When using a virtual scrolling solution, a custom scroll target needs to be provided. Scrolling on the content needs to be disabled and the .ion-content-scroll-host class needs to be added to the element responsible for scrolling.

Console
Console messages will appear here when logged from the example above.

属性

¥Properties

No properties available for this component.

事件

¥Events

No events available for this component.

方法

¥Methods

No public methods available for this component.

CSS 阴影部分

¥CSS Shadow Parts

NameDescription
iconThe icon of the reorder handle (uses ion-icon).

CSS 自定义属性

¥CSS Custom Properties

No CSS custom properties available for this component.

插槽

¥Slots

No slots available for this component.