Drag 拖拽排序

支持列表上下拖拽、卡片选项拖拽和九宫格位置互换三种常见拖拽场景。

拖拽排序组件,长按拖动列表项进行排序

演示

9:41

代码示例

<!-- 1. 列表上下拖拽 -->
  <re-drag :list="listDragItems" :columns="1" itemKey="id" :gap="10" :itemHeight="76" sortMode="shift" @change="onListChange" @drag-start="onListDragStart" @drag-end="onListDragEnd" > <template #default="{ item, index }"> <view class="demo-list-item"> <view class="demo-list-item__left"> <text class="demo-list-item__index">{{ index + 1 }}</text> <view class="demo-list-item__content"> <text class="demo-list-item__title">{{ item['title'] }}</text> <text class="demo-list-item__desc">{{ item['desc'] }}</text> </view> </view> <text class="demo-list-item__handle">⋮⋮</text> </view> </template> </re-drag>

<!-- 2. 卡片选项拖拽 -->
  <re-drag :list="cardDragItems" :columns="2" itemKey="id" :gap="12" :itemHeight="104" sortMode="shift" @change="onCardChange" > <template #default="{ item, index }"> <view class="demo-card-item" :style="'background-color:' + item['bgColor'] + ';'"> <text class="demo-card-item__icon">{{ item['icon'] }}</text> <text class="demo-card-item__title">{{ item['name'] }}</text> <text class="demo-card-item__tag">{{ item['tag'] }}</text> <text class="demo-card-item__index">#{{ index + 1 }}</text> </view> </template> </re-drag>

<!-- 3. 九宫格拖拽(位置互换) -->
  <re-drag :list="gridSwapItems" :columns="3" itemKey="id" :gap="10" :itemHeight="92" sortMode="swap" @change="onGridChange" > <template #default="{ item }"> <view class="demo-grid-item"> <text class="demo-grid-item__icon">{{ item['icon'] }}</text> <text class="demo-grid-item__title">{{ item['name'] }}</text> </view> </template> </re-drag>

<!-- 操作 -->
  <re-button theme="primary" @click="resetAll">重置全部示例</re-button>

引入

推荐使用 easycom 模式,配置后无需手动 import 即可直接使用 <re-drag />。

API

Drag Props

名称类型默认值描述必传
listArray<UTSJSONObject> as Array<UTSJSONObject>-Y
columnsnumber3-Y
itemKeystring'id'-Y
disabledbooleanfalse是否禁用Y
customStylestring''自定义样式Y
gapnumber8-Y
itemHeightnumber100-Y
sortModestring'shift'-Y

Drag Events

名称参数描述
changelist: Array<UTSJSONObject>值变化时触发
drag-startindex: number-
drag-endindex: number-
update:listlist: Array<UTSJSONObject>-

Drag Slots

名称描述
default默认插槽