SortableList 可排序列表
简单的列表排序
基础用法
使用v-model
来双向绑定列表值
默认插槽内容为每一项数据
a
b
c
d
e
vue
<template>
<el-sortable-list v-model="list" class="sortable-list">
<template #default="{ data }">
<div :key="data.id" class="sortable-list-item">{{ data.name }}</div>
</template>
</el-sortable-list>
<!-- <pre>{{ list }}</pre> -->
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const list = ref([
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd' },
{ id: 5, name: 'e' },
])
</script>
<style scoped>
.sortable-list {
max-width: 500px;
}
.sortable-list-item {
display: flex;
align-items: center;
justify-content: center;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
}
</style>
隐藏源代码
指定拖动元素
使用options
来进行传参,值为Sortable.options
a
b
c
d
e
vue
<template>
<el-sortable-list
v-model="list"
class="sortable-list"
:options="{
handle: '.handle',
animation: 300,
}"
>
<template #default="{ data }">
<div :key="data.id" class="sortable-list-item">
<el-icon class="handle"><Rank /></el-icon>
<span>{{ data.name }}</span>
</div>
</template>
</el-sortable-list>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElIcon } from 'element-plus'
import { Rank } from '@element-plus/icons-vue'
const list = ref([
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd' },
{ id: 5, name: 'e' },
])
</script>
<style scoped lang="scss">
.sortable-list {
max-width: 500px;
}
.sortable-list-item {
display: flex;
align-items: center;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
padding: 0 16px;
.handle {
display: inline-block;
margin-right: 16px;
cursor: pointer;
}
}
</style>
隐藏源代码
API
属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
model-value | 列表数据 | Array | - |
options | Sortable.options | Object | - |
tag | 标签元素 | String | div |
Slots
事件名 | 说明 | 参数 |
---|---|---|
default | 每一项内容 | {data: any, index: number} |
Event
事件名 | 说明 | 参数 |
---|---|---|
change | 排序后的数据 | any[] |