相思资源网 Design By www.200059.com

Vue-draggable 的github传送门 :

https://github.com/SortableJS/Vue.Draggable

一. 下载依赖包:npm install vuedraggable -S 

二. 在需要使用的当前界面引入依赖,注册组件:

import draggable from "vuedraggable";
export default {
 components: {
 draggable,
 } 

三. 在template 中建立表格,分别写出thead 部分不变, 此处需要将draggable 渲染成tbody,不然draggable会被解析成div 影响样式。

(渲染方法:<draggable v-model="tablelist" element="tbody">)

<table class="dataTabble">
 <thead>
 <tr>
  <th width="110">栏目名称</th>
  <th width="200">发布时间</th>
  <th width="160">公告数量</th>
  <th width="160">操作</th>
 </tr>
 </thead>
 <draggable v-model="tablelist" element="tbody" :move="getdata" @update="datadragEnd">
 <tr v-for="(item,id) in tablelist" :key="id">
  <td>{{item.name}}</td>
  <td>{{item.time}}</td>
  <td>{{item.num}}</td>
  <td>
  <div class="tabopa">
   <a @click="dialogFormVisible = true" style="cursor:pointer">添加</a>
   <a @click="open2">删除</a>
  </div>
  </td>
 </tr>
 </draggable>
</table>
<div class="zhu mt40">提示:拖动可对栏目进行排序</div> 

此处data部分,通过{ {   } } 获取data中数据,实际中通过请求获取

data() {
 return {
 tablelist: [
  { id: 1, name: "活动消息1", time: "2018-08-25 14:54", num: "1000" },
  { id: 2, name: "公司消息2", time: "2018-08-25 14:54", num: "200" },
  { id: 3, name: "个人消息3", time: "2018-08-25 14:54", num: "30000" },
  { id: 4, name: "客户消息4", time: "2018-08-25 14:54", num: "40" }
 ],
 };
}, 

四.相关方法

获取拖动中和拖动结束时的id

methods: {
 //拖动中与拖动结束
 getdata(evt) {
  console.log(evt.draggedContext.element.id);
 },
 datadragEnd(evt) {
  console.log("拖动前的索引 :" + evt.oldIndex);
  console.log("拖动后的索引 :" + evt.newIndex);
  console.log(this.tags);
 }, 

 五.贴出全部代码

<template>
 <div>
 <!--main-->
   <table class="dataTabble">
    <thead>
    <tr>
     <th width="110">栏目名称</th>
     <th width="200">发布时间</th>
     <th width="160">公告数量</th>
     <th width="160">操作</th>
    </tr>
    </thead>
    <draggable v-model="tablelist" element="tbody" :move="getdata" @update="datadragEnd">
    <tr v-for="(item,id) in tablelist" :key="id">
     <td>{{item.name}}</td>
     <td>{{item.time}}</td>
     <td>{{item.num}}</td>
     <td>
     <div class="tabopa">
      <a @click="dialogFormVisible = true" style="cursor:pointer">添加</a>
      <a @click="open2">删除</a>
     </div>
     </td>
    </tr>
    </draggable>
   </table>
   <div class="zhu mt40">提示:拖动可对栏目进行排序</div>
 <!--main end-->
 </div>
</template>
<script>
import draggable from "vuedraggable";
export default {
 components: {
 draggable,
 },
 data() {
 return {
  tablelist: [
  { id: 1, name: "活动消息1", time: "2018-08-25 14:54", num: "1000" },
  { id: 2, name: "公司消息2", time: "2018-08-25 14:54", num: "200" },
  { id: 3, name: "个人消息3", time: "2018-08-25 14:54", num: "30000" },
  { id: 4, name: "客户消息4", time: "2018-08-25 14:54", num: "40" }
  ],
 };
 },
 methods: {
 //拖动中与拖动结束
 getdata(evt) {
  console.log(evt.draggedContext.element.id);
 },
 datadragEnd(evt) {
  console.log("拖动前的索引 :" + evt.oldIndex);
  console.log("拖动后的索引 :" + evt.newIndex);
  console.log(this.tags);
 },
 }
}
</script>
<style>
</style> 

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

标签:
Vue-draggable表格拖拽排序,Vue,表格拖拽排序

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“利用Vue-draggable组件实现Vue项目中表格内容的拖拽排序”

暂无利用Vue-draggable组件实现Vue项目中表格内容的拖拽排序的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。