• 027实现可视化编程所需拖拽技术实现方案之vuedraggable


    027实现可视化编程所需拖拽技术实现方案之vuedraggable

    vuedraggable目前Vue3中节点拖拽更适用的依赖包

    Vue.Draggable是一款基于Sortable.js实现的vue拖拽插件。支持移动设备、拖拽和选择文本、智能滚动,可以在不同列表间拖拽、不依赖jQuery为基础、vue 2过渡动画兼容、支持撤销操作,是一款非常优秀的vue拖拽组件。

    特性

    支持触摸设备
    支持拖拽和选择文本
    支持智能滚动
    支持不同列表之间的拖拽
    不以jQuery为基础
    和视图模型同步刷新
    和vue2的过渡动画兼容
    支持撤销操作
    当需要完全控制时,可以抛出所有变化
    可以和现有的UI组件兼容
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    安装与引入

    • 安装:npm install vuedraggable
    • 引入:import draggable from 'vuedraggable'

    基本拖拽调整排序功能示例

    <template>
      <div>
      <div>{{drag?'拖拽中':'拖拽停止'}}div>
      
     <draggable v-model="myArray"  chosenClass="chosen" forceFallback="true" group="people" animation="1000" @start="onStart" @end="onEnd">
        <transition-group>
         <div class="item" v-for="element in myArray" :key="element.id">{{element.name}}div>
        transition-group>
    draggable> 
      div>
    template>
    <style scoped>
            /*被拖拽对象的样式*/
            .item {
                padding: 6px;
                background-color: #fdfdfd;
                border: solid 1px #eee;
                margin-bottom: 10px;
                cursor: move;
            } 
            /*选中样式*/
            .chosen {
                border: solid 1px #3089dc !important;
            }
    style>
    <script>
    //导入draggable组件
    import draggable from 'vuedraggable'
    export default {
      //注册draggable组件
       components: {
                draggable,
            },
       data() {
        return { 
          drag:false,
          //定义要被拖拽对象的数组
          myArray:[
            {people:'cn',id:10,name:'www.aa.com'},
            {people:'cn',id:20,name:'www.bb.com'},
            {people:'cn',id:30,name:'www.cc.com'},
            {people:'us',id:40,name:'www.dd.com'}
            ] 
        };
      },
      methods: {
         //开始拖拽事件
          onStart(){
            this.drag=true;
          },
          //拖拽结束事件
           onEnd() {
           this.drag=false;
        },
      },
    };
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    跨列表的拖拽实现方案示例

    <script setup lang="ts">
    import { reactive, ref } from 'vue';
    import draggable from 'vuedraggable';
    import RawDisplayer from './RawDisplayer.vue';
    
    let id = ref(1);
    let list = reactive([
      { name: 'John 1', id: 0 },
      { name: 'Joao 2', id: 1 },
      { name: 'Jean 3', id: 2 },
    ]);
    
    let list2 = reactive([
      { name: 'Jonny 4', id: 3 },
      { name: 'Guisepe 5', id: 4 },
    ]);
    function getUid() {
      id.value++;
    }
    function add() {
      getUid();
      list.push({ name: 'Juan ' + id.value, id: id.value });
    }
    function replace() {
      list.splice(0, list.length);
    }
    function add2() {
      getUid();
      list2.push({ name: 'Juan ' + id.value, id: id.value });
    }
    function replace2() {
      list2.splice(0, list2.length);
    }
    script>
    
    <template>
      <el-row>
        <el-col :span="6">
          <h3>列表一h3>
    
          <draggable
            id="first"
            data-source="juju"
            :list="list"
            class="list-group"
            group="a"
            item-key="name"
          >
            <template #item="{ element }">
              <div class="list-group-item">
                {{ element.name }}
                <el-input style="width: 88px" v-model="element.name">el-input>
              div>
            template>
    
            <template #footer>
              <div class="btn-group" role="group">
                <el-button type="primary" @click="add">添加节点el-button>
                <el-button type="primary" @click="replace">清空el-button>
              div>
            template>
          draggable>
        el-col>
    
        <el-col :span="6">
          <h3>列表二h3>
    
          <draggable :list="list2" class="list-group" group="a" item-key="name">
            <template #item="{ element }">
              <div class="list-group-item">
                {{ element.name }}
              div>
            template>
    
            <template #header>
              <div class="btn-group" role="group" aria-label="Basic example">
                <el-button type="primary" @click="add2">添加节点el-button>
                <el-button type="primary" @click="replace2">清空el-button>
              div>
            template>
          draggable>
        el-col>
        <el-col :span="5">
          <RawDisplayer :value="list" title="数据一" />
        el-col>
        <el-col :span="5">
          <RawDisplayer :value="list2" title="数据二" />
        el-col>
      el-row>
    template>
    
    <style scoped lang="scss">
    h3 {
      padding: 5px 24px;
    }
    .list-group {
      background-color: #f5f5f5;
      margin: 5px 12px;
      padding: 15px 12px;
    }
    .list-group-item {
      background-color: #ffffff;
      padding: 18px 12px;
      border: #d8d8d8 1px solid;
      margin-top: -1px;
      cursor: move;
    }
    .btn-group {
      padding: 15px 0;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    jupyter notebook代码补全扩展安装遇到 Jupyter command `jupyter-contrib` not found.” 问题
    【MyBatis】mybatis工具类迭代
    「SpringBrick快速入门指南」:一款基于Spring Boot的高级插件化开发框架
    C++17静态数据成员声明为inline
    【Openxml】如何为OpenXml元素创建超链接
    【vue设计与实现】异步组件与函数式组件 2 - 超时与Error组件
    腾讯云GPU云服务器计算型和渲染型分别适用于哪些场景?
    【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
    MD5加密技术
    【C++】类和对象(四)“这三种”成员变量只能放在初始化列表中初始化,你都知道吗?
  • 原文地址:https://blog.csdn.net/qq_25753445/article/details/127725529