使用sortablejs对iview的table进行排序

8 篇文章 0 订阅
1 篇文章 0 订阅

安装

npm安装

$ npm install sortablejs --save

bower安装

 bower install --save sortablejs

直接script引入

<script src="../../js/Sortable.min.js"></script>

使用

html代码

	<Table
        ref="tablesMain"
        border
        :data="tableDate"
        :columns="columns">
     </Table>

js代码

import Sortable from 'sortablejs'

export default {
  name: 'banner_eidt',
  data () {
    return {
      columns: [
        {
          type: 'index',
          title: '顺序'
        },
        {
          key: 'id',
          title: 'ID'
        },
        {
          title: 'banner图',
          key: 'cover',
          render: (h, params) => {
            var bannerImg = null
            if (params.row.cover) {
              bannerImg = params.row.cover
              return h('img', {
                attrs: { src: bannerImg },
                style: {
                  width: '100%',
                  height: '100px',
                  padding: '5px 0px 0px 0px',
                  cursor: 'pointer'
                }
              })
            } else {
              return h('span', '/')
            }
          }
        }
      ],
      tableDate: [
        {
          id: 98,
          cover: 'http://api.ad.zhengfenduojin.cn/storage/pages/2020/01/03/5e0ec95b39036.png',
        },
        {
          id: 97,
          cover: 'http://tui.static.colorfulltech.cn/task/2020/04/02/5e856009d722f.jpeg',
        }
      ]
    }
  },
  mounted () {
    this.rowDrop()
  },
  methods: {
    // 表格拖拽排序
    rowDrop() {
      const tbody = document.querySelector('.ivu-table-tbody')
      const _this = this
      var newArr = JSON.parse(JSON.stringify(_this.tableDate))
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = newArr.splice(oldIndex, 1)[0]
          newArr.splice(newIndex, 0, currRow)
          _this.tableDate = []  // 清空之前已有的表格数据
          _this.$nextTick(function () {
            _this.tableDate = newArr // 再次赋值
          })
        }
      })
    },
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值