一、vue项目中的表格须要实现行拖拽功能html
二、表格使用element组件库中el-tablevue
介绍:Sortable.js是一款轻量级的拖放排序列表的js插件git
引用自官方文档:No jQuery required. Supports Meteor, AngularJS, React, Polymer, Vue, Knockout and any CSS library, e.g. Bootstrap.
参考地址:https://github.com/SortableJS...github
介绍:基于Sortable.js的vue组件,用以实现拖拽功能npm
引用自官方文档:Vue drag-and-drop component based on Sortable.js
参考地址:https://github.com/SortableJS...app
在使用vuedraggable的过程当中,发现必须用<draggable></draggable>包裹拖动项的父级元素,可是element组件库对table进行封装,没法直接包裹拖动项(即tr)的父级元素
若是你的项目中,表格未使用组件库,实现能够参考https://www.cnblogs.com/xiang...ui
使用 sortable.js this
步骤一: 安装插件
npm install vuedraggable
步骤二:引入code
import Sortable from 'sortablejs'; @Component({ components: { Sortable } })
步骤三: el-table 添加row-key属性
<el-table ref="filterTable" row-key="ip" @filter-change="handlerFilterChange" class="cl-table" :data="resourceList" v-loading="resourceListLoading" stripe style="width:100%;"> <el-table-column prop="name" label="主机名" :min-width="150" show-overflow-tooltip> </el-table-column> </el-table>
步骤四 : 将拖拽元素设置为要拖动项的父级元素
mounted() { // 表格中须要实现行拖动,因此选中tr的父级元素 const table = document.querySelector('.el-table__body-wrapper tbody') const self = this Sortable.create(table, { onEnd({ newIndex, oldIndex }) { console.log(newIndex, oldIndex) const targetRow = self.resourceList.splice(oldIndex, 1)[0] self.resourceList.splice(newIndex, 0, targetRow) } }) }