跳至内容

useSortable

类别
导出大小
720 B
@vueuse/integrations
上次更改
上个月

对于 sortable 的包装器。

有关可以传递哪些选项的更多信息,请参阅 Sortable 文档中的 Sortable.options

演示

a
b
c
[{"id":1,"name":"a"},{"id":2,"name":"b"},{"id":3,"name":"c"}]
@vueuse/integrations 附加组件中可用。

安装

bash
npm i sortablejs@^1

用法

使用模板引用

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const el = ref<HTMLElement | null>(null)
const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

useSortable(el, list)
</script>

<template>
  <div ref="el">
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

使用指定要操作的选择器

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const el = ref<HTMLElement | null>(null)
const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

const animation = 200

const { option } = useSortable(el, list, {
  handle: '.handle',
  // or option set
  // animation
})

// You can use the option method to set and get the option of Sortable
option('animation', animation)
// option('animation') // 200
</script>

<template>
  <div ref="el">
    <div v-for="item in list" :key="item.id">
      <span>{{ item.name }}</span>
      <span class="handle">*</span>
    </div>
  </div>
</template>

使用选择器获取根元素

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

useSortable('#dv', list)
</script>

<template>
  <div id="dv">
    <div v-for="item in list" :key="item.id">
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

提示

如果要自己处理 onUpdate,可以传入 onUpdate 参数,我们还公开了用于移动项目位置的函数。

ts
import { moveArrayElement } from '@vueuse/integrations/useSortable'

useSortable(el, list, {
  onUpdate: (e) => {
    // do something
    moveArrayElement(list.value, e.oldIndex, e.newIndex)
    // nextTick required here as moveArrayElement is executed in a microtask
    // so we need to wait until the next tick until that is finished.
    nextTick(() => {
      /* do something */
    })
  }
})

类型声明

typescript
export interface UseSortableReturn {
  /**
   * start sortable instance
   */
  start: () => void
  /**
   * destroy sortable instance
   */
  stop: () => void
  /**
   * Options getter/setter
   * @param name a Sortable.Options property.
   * @param value a value.
   */
  option: (<K extends keyof Sortable.Options>(
    name: K,
    value: Sortable.Options[K],
  ) => void) &
    (<K extends keyof Sortable.Options>(name: K) => Sortable.Options[K])
}
export type UseSortableOptions = Options & ConfigurableDocument
export declare function useSortable<T>(
  selector: string,
  list: MaybeRefOrGetter<T[]>,
  options?: UseSortableOptions,
): UseSortableReturn
export declare function useSortable<T>(
  el: MaybeRefOrGetter<HTMLElement | null | undefined>,
  list: MaybeRefOrGetter<T[]>,
  options?: UseSortableOptions,
): UseSortableReturn
export declare function moveArrayElement<T>(
  list: MaybeRefOrGetter<T[]>,
  from: number,
  to: number,
): void

来源

来源演示文档

贡献者

Anthony Fu
丶远方
Doctorwu
David Gonzalez
Anthony Fu
Tycho
George Kinsman

变更日志

v10.8.0 on 2/20/2024
a086e - fix: stricter types
v10.6.0 on 11/9/2023
d9846 - fix: prevent from creating multi instances (#3501)
v10.4.0 on 8/25/2023
b8515 - fix: fixed moveArrayElement repeatedly triggering side effects (#3322)
v10.2.0 on 6/16/2023
14283 - feat: 添加设置获取方法 (#3108)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: 将MaybeComputedRef重命名为MaybeRefOrGetter
0a72b - feat(toValue): 将resolveUnref重命名为toValue
v10.0.0-beta.3 on 4/12/2023
3a508 - fix: DOM 和数组的顺序不同 (#2926)
v10.0.0-beta.0 on 3/14/2023
6bc60 - feat: 新功能 (#2763)

根据 MIT 许可证发布。