使用sorttable进行排序

<template>
  <div class="hello">
  <ul ref="sortTable">
    <li v-for="(v) in sortTable" :key="v" class="li">{{v}}</li>
  </ul>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data () {
    return {
      sortTable: [1,2,3,4,5,6,7,8,9,10,11,12]
    }
  },
  mounted() {
    this.setSort()
  },
  methods:{
    setSort () {
      const el = this.$refs.sortTable
      Sortable.create(el, {
        handle: '.li',
        animation: 200,
        // sorttable进行拖拽的时候并不改变原数组,想改变原数组就必须在拖拽完成后改变数据
        onEnd: ({ newIndex, oldIndex}) => {
          // 在拖动事件结束后把拖拽的值删掉
          const result = this.sortTable.splice(oldIndex, 1)
          // 在新的位置插入
          this.sortTable.splice(newIndex, 0, ...result)
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
ul {
  list-style-type: none;
  padding: 0;
}
li:first-of-type{
  border-top: 1px solid gray;
}
li {
  margin: 0 10px;
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid gray;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值