详细的官网案例:https://www.npmjs.com/package/vuedraggable

  1. 简单的拖动使用
<template>
    <div>
        <draggable @start="start" @end="end">
            <div v-for='i in list' :key='i.id' class='dragItem'>{{i.name}}</div>
        </draggable>
    </div>
</template>

<script>
import draggable from "vuedraggable"
export default {
    components: {
        draggable
    },
    data () {
        return {
            list: [
                { id: 1, name: 'aaa' },
                { id: 2, name: 'bbb' },
                { id: 3, name: 'ccc' }
            ]
        }
    },
    methods: {
        start (e) {
            console.log(e)
        },
        end (e) {
            console.log(e)
        },
    }
}
</script>

引入安装好的vuedraggable插件,后引入到该vue实例作为组件,即可使用<draggable>标签,那么<draggable>标签下的元素就可以进行拖拽排序。

其中的@start, @end事件是拖拽的开始事件和结束事件,有4个index参数表示拖动开始的下标和结束的下标。

  1. 固定某些元素,进行其他元素排序

如果需要进行某些进行拖拽排序,而其他元素不可拖拽:

<template>
    <div>
        <draggable v-model='list' :options='dragOptions'>
            <div v-for='i in list' :key='i.id' class='dragItem' :class='i.active ? "active" : "disabled"'>{{i.name}}</div>
        </draggable>
    </div>
</template>
<script>
import draggable from "vuedraggable"
export default {
    components: {
        draggable
    },
    data () {
        return {
            dragOptions: {
                animation: 500,
                filter: '.disabled'
            },
            list: [
                { id: 1, name: 'aaa', active: true },
                { id: 2, name: 'bbb', active: false },
                { id: 3, name: 'ccc', active: true }
            ]
        }
    },
    methods: {}
}
</script>

实现效果:aaa,ccc可拖拽,b可被移动,但拖拽b无效。由dragOptions中的filter实现过滤,并添加动画效果。

  1. 复制拖拽

需要实现左侧元素可拖拽到右侧,且左侧元素依旧存在:

<template>
    <!-- 左侧-->
    <div style='width: 50%; float: left;'>
        <draggable v-model='list1' v-bind='Object.assign(dragOptions, options1)'>
            <div v-for='i in list1' :key='i.id' class='dragItem'>{{i.name}}</div>
        </draggable>
    </div>

    <!-- 右侧-->
    <div style='width: 50%; float: right;'>
        <draggable v-model='list2' v-bind='dragOptions' group="field">
            <div v-for='i in list2' :key='i.id' class='dragItem'>{{i.name}}</div>
        </draggable>
    </div>
</template>
<script>
import draggable from "vuedraggable"
export default {
    components: {
        draggable
    },
    data () {
        return {
            dragOptions: {
                animation: 500
            },
            options1: {
                sort: false,
                group: {
                    name: 'field',
                    pull:'clone',
                    put: false
                }
            },
            list1: [
                { id: 1, name: 'aaa' },
                { id: 2, name: 'bbb' },
                { id: 3, name: 'ccc' }
            ]
            list2: [
                { id: 4, name: 'ddd' },
                { id: 5, name: 'eee' },
                { id: 6, name: 'fff' }
            ]
        }
    },
    methods: {}
}
</script>

左侧的draggable补充了options配置项,sort: false表示拖拽时禁止排序操作,group: {name: 'field', pull: 'clone', put: false}表示进行克隆拖拽操作,并且该name和拖拽的目标位置group名称一致,如右侧draggable的group=field,右侧接收的draggable添加标识:group=“field”


坂田银八
24 声望2 粉丝

前端底层打工仔