首页 > 开发 > Javascript > 正文

vue.draggable实现表格拖拽排序效果

2020-02-25 02:02:58
字体:
来源:转载
供稿:网友

本文实例为大家分享了vue.draggable实现表格拖拽排序效果展示的具体代码,供大家参考,具体内容如下

主要使用vuedraggable和sortablejs两个组件。

1、安装组件

npm install vuedraggablenpm install sortablejs

2、引入组件

import draggable from 'vuedraggable';import Sortable from 'sortablejs';export default {  components: {    draggable,    Sortable  },  ....

3、HTML

我的例子是给表格排序,项目整体使用的是ivew,所以用了ivew的栅格来画表格

<Row class="draggableTable-head">  <Col span="1">序号</Col>  <Col span="2">商品条码</Col>  <Col span="3">商品名称</Col>  <Col span="1">单位</Col></Row><draggable class="list-group" v-model="tableData" :options="{draggable:'.rows'}"  :move="getdata" @update="datadragEnd">  <Row class="rows" v-for="(item,index) in tableData" :key="index">    <Col span="1">      <div class="cell">{{index+1}}</div>    </Col>    <Col span="2">      <div class="cell">{{item.barCode}}</div>    </Col>    <Col span="2">      <div class="cell">{{item.name}}</div>    </Col>    <Col span="2">      <div class="cell">{{item.unit}}</div>    </Col>  </Row></draggable>

options中draggable的值是拖动的class。一开始怎么都不能拖动,加上这个就可以了。

4、两个方法

move:拖动中
update:拖拽结束

getdata (data) {  // console.log('getdata方法');},datadragEnd (evt) {  // console.log('datadragEnd方法');  console.log('拖动前的索引 :' + evt.oldIndex)  console.log('拖动后的索引 :' + evt.newIndex)}

表格的处理逻辑是:
1、当前行的id和排序号作为参数,调用后台更改顺序的方法
2、不论调用成功与否,都重新渲染表格数据

【注意】如果有分页,那么传给后台的排序号就要再加上之前的条数,即(页码-1)*每页条数

Vue.Draggable作者的git地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持错新站长站。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表