如何利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序

這篇文章給大家分享的是有關(guān)如何利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)公司專注于霍邱網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供霍邱營(yíng)銷(xiāo)型網(wǎng)站建設(shè),霍邱網(wǎng)站制作、霍邱網(wǎng)頁(yè)設(shè)計(jì)、霍邱網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造霍邱網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供霍邱網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

Vue的優(yōu)點(diǎn)

Vue具體輕量級(jí)框架、簡(jiǎn)單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢(shì),Vue中頁(yè)面使用的是局部刷新,不用每次跳轉(zhuǎn)頁(yè)面都要請(qǐng)求所有數(shù)據(jù)和dom,可以大大提升訪問(wèn)速度和用戶體驗(yàn)。

Vue-draggable 的github傳送門(mén) :

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

一. 下載依賴包:npm install vuedraggable -S 

二. 在需要使用的當(dāng)前界面引入依賴,注冊(cè)組件:

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

三. 在template 中建立表格,分別寫(xiě)出thead 部分不變, 此處需要將draggable 渲染成tbody,不然draggable會(huì)被解析成div 影響樣式。

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

<table class="dataTabble">
 <thead>
 <tr>
  <th width="110">欄目名稱</th>
  <th width="200">發(fā)布時(shí)間</th>
  <th width="160">公告數(shù)量</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" >添加</a>
   <a @click="open2">刪除</a>
  </div>
  </td>
 </tr>
 </draggable>
</table>
<div class="zhu mt40">提示:拖動(dòng)可對(duì)欄目進(jìn)行排序</div>

此處data部分,通過(guò){ {   } } 獲取data中數(shù)據(jù),實(shí)際中通過(guò)請(qǐng)求獲取

data() {
 return {
 tablelist: [
  { id: 1, name: "活動(dòng)消息1", time: "2018-08-25 14:54", num: "1000" },
  { id: 2, name: "公司消息2", time: "2018-08-25 14:54", num: "200" },
  { id: 3, name: "個(gè)人消息3", time: "2018-08-25 14:54", num: "30000" },
  { id: 4, name: "客戶消息4", time: "2018-08-25 14:54", num: "40" }
 ],
 };
},

四.相關(guān)方法

獲取拖動(dòng)中和拖動(dòng)結(jié)束時(shí)的id

methods: {
 //拖動(dòng)中與拖動(dòng)結(jié)束
 getdata(evt) {
  console.log(evt.draggedContext.element.id);
 },
 datadragEnd(evt) {
  console.log("拖動(dòng)前的索引 :" + evt.oldIndex);
  console.log("拖動(dòng)后的索引 :" + evt.newIndex);
  console.log(this.tags);
 },

 五.貼出全部代碼

<template>
 <div>
 <!--main-->
   <table class="dataTabble">
    <thead>
    <tr>
     <th width="110">欄目名稱</th>
     <th width="200">發(fā)布時(shí)間</th>
     <th width="160">公告數(shù)量</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" >添加</a>
      <a @click="open2">刪除</a>
     </div>
     </td>
    </tr>
    </draggable>
   </table>
   <div class="zhu mt40">提示:拖動(dòng)可對(duì)欄目進(jìn)行排序</div>
 <!--main end-->
 </div>
</template>
<script>
import draggable from "vuedraggable";
export default {
 components: {
 draggable,
 },
 data() {
 return {
  tablelist: [
  { id: 1, name: "活動(dòng)消息1", time: "2018-08-25 14:54", num: "1000" },
  { id: 2, name: "公司消息2", time: "2018-08-25 14:54", num: "200" },
  { id: 3, name: "個(gè)人消息3", time: "2018-08-25 14:54", num: "30000" },
  { id: 4, name: "客戶消息4", time: "2018-08-25 14:54", num: "40" }
  ],
 };
 },
 methods: {
 //拖動(dòng)中與拖動(dòng)結(jié)束
 getdata(evt) {
  console.log(evt.draggedContext.element.id);
 },
 datadragEnd(evt) {
  console.log("拖動(dòng)前的索引 :" + evt.oldIndex);
  console.log("拖動(dòng)后的索引 :" + evt.newIndex);
  console.log(this.tags);
 },
 }
}
</script>
<style>
</style>

感謝各位的閱讀!關(guān)于“如何利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享題目:如何利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序
分享鏈接:http://bm7419.com/article36/jdjssg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站動(dòng)態(tài)網(wǎng)站、網(wǎng)站改版、網(wǎng)站排名、App設(shè)計(jì)品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)