vue和sortable.js实现表格拖拽

1、预先安装sortable.js:

npm install sortablejs --save

2、效果及代码:

<template>
  <div>
    <p id="msg"></p>
    <table id="itxst" width="80%" border="0" cellspacing="1" cellpadding="0" class="t1">
      <tr class="filter">
        <th>index</th>
        <th>Name</th>
        <th>Age</th>
        <th>Address</th>
      </tr>
      <tr v-for="(item,index) in table_data" :key="index" :data-no="index">
        <td>{{index + 1}}</td>
        <td>{{item.name}}</td>
        <td>{{item.age}}</td>
        <td>{{item.address}}</td>
      </tr>
      <tfoot class="filter">
        <tr>
          <td colspan="4">
            <a-button @click="reset">刷新</a-button>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  data () {
    return {
      table_data: [
        {
          name: 'Edward King(0)',
          age: 30,
          address: 'London, Park Lane no. 0'
        }, {
          name: 'Edward King(1)',
          age: 30,
          address: 'London, Park Lane no. 0'
        }, {
          name: 'Edward King(2)',
          age: 31,
          address: 'London, Park Lane no. 0'
        }, {
          name: 'Edward King(3)',
          age: 32,
          address: 'London, Park Lane no. 0'
        }
      ]
    }
  },
  mounted () {
    this.drag()
  },
  methods: {
    drag () {
      var that = this
      // 获取对象
      var el = document.getElementById('itxst')
      // 设置配置
      var ops = {
        animation: 500,
        filter: '.filter',
        dataIdAttr: 'data-no',
        onEnd: function (evt) {
          console.log(evt)
          // 获取拖动后的排序
          var arr = sortable.toArray().splice(1, that.table_data.length)
          document.getElementById('msg').innerHTML = 'A组排序结果:' + arr
        }
      }
      // 初始化
      var sortable = Sortable.create(el, ops)
    },
    reset () {
    }
  }
}
</script>

<style scoped>
.t1{
  margin: auto;
}
.t1 tr{
  height: 50px;
  border-bottom: 1px solid gainsboro;
}
.t1 tr:first-child, .t1 tr:first-child:hover{
  background: #eeeeee;
}
.t1 tr:hover{
  background: #b9f9fc;
  border: 0;
  cursor: move;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LINGK98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值