vue拖拽排序插件vuedraggable使用方法详解

HTML引用(vue引用随便百度都有在此不做简介)

<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
<!-- CDNJS :: Sortable (https://cdnjs.com/) -->
<script src="//cdn.jsdelivr.net/npm/sortablejs@1.8.4/Sortable.min.js"></script>
<!-- CDNJS :: Vue.Draggable (https://cdnjs.com/) -->
<script src="//cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.20.0/vuedraggable.umd.min.js"></script>

注册组件

components: { vuedraggable },

vue.js 2.0 html引用好像只能加载“vuedraggable”,如果是“draggable”会报错找不到,不知道为什么希望有大神能够指点一下

示例

<template>
  <vuedraggable v-model="colors" @update="datadragEnd" :options = "{animation:500}">
      <transition-group>
        <div v-for="element in colors" :key="element.text" class = "drag-item">
          {{element.text}}
        </div>
      </transition-group>
  </vuedraggable>
</template>
 
<script>
  new Vue({
    data(){
      return{
        msg:"这是测试组件",
        colors: [
          {
            text: "Aquamarine",
          }, 
          {
            text: "Hotpink",
          }, 
          {
            text: "Gold",
          }, 
          {
            text: "Crimson",
          }, 
          {
            text: "Blueviolet",
          },
          {
            text: "Lightblue",
          }, 
          {
            text: "Cornflowerblue",
          }, 
          {
            text: "Skyblue",
          }, 
          {
            text: "Burlywood",
          }
        ],
        startArr:[],
        endArr:[],
        count:0,
      }
    },
    components: {
      draggable
    },
    methods:{
      getdata (evt) {
        console.log(evt.draggedContext.element.text)
      },
      datadragEnd (evt) {
        evt.preventDefault();
        console.log('拖动前的索引 :' + evt.oldIndex)
        console.log('拖动后的索引 :' + evt.newIndex)
        console.log(this.colors);
      }
    },
    mounted () {
      //为了防止火狐浏览器拖拽的时候以新标签打开,此代码真实有效
      document.body.ondrop = function (event) {
        event.preventDefault();
        event.stopPropagation();
      }
    }
  })
</script>
 
<style lang="scss" scoped>
  .test{
    border:1px solid #ccc;
  }
  .drag-item{
    width: 200px;
    height: 50px;
    line-height: 50px;
    margin: auto;
    position: relative;
    background: #ddd;
    margin-top:20px;
  }
  .ghostClass{
    opacity: 1;
  }
  .bottom{
    width: 200px;
    height: 50px;
    position: relative;
    background: blue;
    top:2px;
    left: 2px;
    transition: all .5s linear;
  }
</style>

写此文章主要是为了这个(确实真实可用)

mounted () {
    //为了防止火狐浏览器拖拽的时候以新标签打开,此代码真实有效
    document.body.ondrop = function (event) {
        event.preventDefault();
        event.stopPropagation();
    }
}

上下是可以拖动的,只是截图的话看不出效果来,小伙伴们注意了,里面有个options选项,这个选项怎么来的呢,据我观察这个插件是基于sortable.js,所以这个options里面的配置,和sortable.js是一样的,下面我贴两个地址,一个是vuedraggable的GitHub地址,一个是sortable.js的GitHub地址

vuedraggable: 学习地址 

sortable.js:学习地址

options配置如下

var sortable = new Sortable(el, {
  group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
  sort: true, // sorting inside list
  delay: 0, // time in milliseconds to define when the sorting should start
  touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
  disabled: false, // Disables the sortable if set to true.
  store: null, // @see Store
  animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
  handle: ".my-handle", // Drag handle selector within list items
  filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
  preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
  draggable: ".item", // Specifies which items inside the element should be draggable
  ghostClass: "sortable-ghost", // Class name for the drop placeholder
  chosenClass: "sortable-chosen", // Class name for the chosen item
  dragClass: "sortable-drag", // Class name for the dragging item
  dataIdAttr: 'data-id',
 
  forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
 
  fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback
  fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body
  fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.
 
  scroll: true, // or HTMLElement
  scrollFn: function(offsetX, offsetY, originalEvent, touchEvt, hoverTargetEl) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling
  scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
  scrollSpeed: 10, // px
 
  setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {
    dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent
  },
 
  // Element is chosen
  onChoose: function (/**Event*/evt) {
    evt.oldIndex; // element index within parent
  },
 
  // Element dragging started
  onStart: function (/**Event*/evt) {
    evt.oldIndex; // element index within parent
  },
 
  // Element dragging ended
  onEnd: function (/**Event*/evt) {
    var itemEl = evt.item; // dragged HTMLElement
    evt.to;  // target list
    evt.from; // previous list
    evt.oldIndex; // element's old index within old parent
    evt.newIndex; // element's new index within new parent
  },
 
  // Element is dropped into the list from another list
  onAdd: function (/**Event*/evt) {
    // same properties as onEnd
  },
 
  // Changed sorting within list
  onUpdate: function (/**Event*/evt) {
    // same properties as onEnd
  },
 
  // Called by any change to the list (add / update / remove)
  onSort: function (/**Event*/evt) {
    // same properties as onEnd
  },
 
  // Element is removed from the list into another list
  onRemove: function (/**Event*/evt) {
    // same properties as onEnd
  },
 
  // Attempt to drag a filtered element
  onFilter: function (/**Event*/evt) {
    var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.
  },
 
  // Event when you move an item in the list or between lists
  onMove: function (/**Event*/evt, /**Event*/originalEvent) {
    // Example: http://jsbin.com/tuyafe/1/edit?js,output
    evt.dragged; // dragged HTMLElement
    evt.draggedRect; // TextRectangle {left, top, right и bottom}
    evt.related; // HTMLElement on which have guided
    evt.relatedRect; // TextRectangle
    originalEvent.clientY; // mouse position
    // return false; — for cancel
  },
 
  // Called when creating a clone of element
  onClone: function (/**Event*/evt) {
    var origEl = evt.item;
    var cloneEl = evt.clone;
  }
});

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Validator是一个用于Vue.js的表单验证插件,可以方便地对表单进行验证。下面详细介绍一下Vue Validator的使用方法。 首先,我们需要在Vue项目中安装Vue Validator。可以通过命令`npm install vue-validator`来安装该插件。 安装完成后,在Vue组件中,需要先引入Vue Validator,并在`Vue.use(VueValidator)`中启用它。 接着,在需要进行验证的表单中,添加一个`validator`属性来指定验证规则。可以使用`v-validate`指令来设置验证规则。例如: ```html <input type="text" v-model="name" v-validate="'required|alpha'"> ``` 在上述代码中,我们使用`v-validate`指令来设置验证规则为必填(required)和只允许字母(alpha)。 然后,在表单中,可以使用`v-show`指令来根据验证结果来显示或隐藏错误信息。例如: ```html <div v-show="$validator.name.required">姓名不能为空</div> <div v-show="$validator.name.alpha">姓名只能包含字母</div> ``` 在上述代码中,使用`v-show="$validator.name.required"`来根据`name`字段的必填验证结果来显示或隐藏错误信息。 此外,还可以在Vue组件中定义自定义验证规则。可以使用`Vue.validator`方法来定义自定义验证规则。例如: ```javascript Vue.validator('customRule', function(val) { return val === 'validValue'; }); ``` 在上述代码中,我们自定义了一个验证规则`customRule`,它的值必须等于`'validValue'`。这样,我们就可以在表单中使用`v-validate="'customRule'"`来应用该自定义规则。 总结一下,Vue Validator的使用方法包括:安装插件、启用插件、设置验证规则、显示错误信息以及自定义验证规则。以上就是Vue Validator使用方法的详细解释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值