Vue中使用elementui與Sortable.js實(shí)現(xiàn)列表拖動(dòng)排序
本文實(shí)例為大家分享了使用elementui與Sortable.js實(shí)現(xiàn)列表拖動(dòng)排序的具體代碼,供大家參考,具體內(nèi)容如下
一、安裝使用Sortable.js
1、安裝
cnpm install sortablejs --save
2、在需要的vue頁面單獨(dú)引入
<script> ? ? import Sortable from 'sortablejs' ? ? export default{ ? ? ?? ?......... ? ? ?? ?data() { ?? ??? ??? ?return { ?? ??? ??? ??? ?accessoryList : [1,2,3,4,5,6,7,8,9] ?? ??? ??? ?} ?? ??? ?} ?? ??? ?......... ?? ?} </script>
3、在生命周期mounted中注冊(cè)事件以及在html中渲染數(shù)據(jù)
mounted() { ? //sortAble為要拖動(dòng)的盒子dom ? Sortable.create(document.getElementById('sortAble'), {animation: 150}) ?}
<div id="sortAble"> ?? ?<p v-for="(item, index) in accessoryList " :key="'' + index">{{item}}</p> </div>
說明: 至此sortable.js已經(jīng)可以看到拖動(dòng)的效果了;
二、元數(shù)據(jù)不刷新問題
1、前面的只能是前端頁面上看到數(shù)據(jù),但是對(duì)于數(shù)據(jù)怎樣拖動(dòng)數(shù)組arrdata中的順序仍舊未變,繼續(xù)看:
只需要把mounted中的Sortable.create(document.getElementById('sortAble'), {animation: 150})
這塊換為以下代碼即可:
rowDrop() { ? ?let that = this ? ? // eslint-disable-next-line no-unused-vars ? ? let sortable = new Sortable(document.getElementById('sortAble'), { ? ? ? ? sort: true, ? ? ? ? animation: 150, ? ? ? ? onEnd: function (evt) { ? ? ? ? ? ? that.accessoryList.splice(evt.newIndex, 0, that.accessoryList.splice(evt.oldIndex, 1)[0]) ? ? ? ? ? ? let newArray = that.accessoryList.slice(0) ? ? ? ? ? ? that.accessoryList = [] ? ? ? ? ? ? that.$nextTick(function () { ? ? ? ? ? ? ? ? that.accessoryList = newArray ? ? ? ? ? ? }) ? ? ? ? } ? ? }) }
現(xiàn)在數(shù)組accessoryList
就是改變順序后的,即可提交至后臺(tái)保存。
三、在elementUI中的彈窗中使用上述排序功能
說明:對(duì)于某些需求需要在el-dialog的全屏彈窗中使用,比如系統(tǒng)管理,就會(huì)發(fā)現(xiàn)拖動(dòng)失效,甚至還有報(bào)錯(cuò);
嘗試使用下面方法解決
1、原因:el-dialog的dom加載順序問題,查閱資料就能知道el-dialog的dom操作區(qū)是一個(gè)異步加載的機(jī)制,因此在mounted中不能調(diào)用該方法;
2、解決:使用組件將全屏彈窗寫到一個(gè)單獨(dú)組件內(nèi),如下:
<template> ? ? <el-dialog ? ? ? ? ? ? v-if="fileManage" ? ? ? ? ? ? custom-class="oaDialog" ? ? ? ? ? ? :visible.sync="fileManage" ? ? ? ? ? ? append-to-body ? ? ? ? ? ? width="100%"> ? ? ? ? </div> ? ? ? ? <div id="sortAble"> ?? ??? ??? ?<p v-for="(item, index) in accessoryList " :key="'' + index">{{item}}</p> ?? ??? ?</div> ? ? </el-dialog> </template> <script> ? ? import Sortable from 'sortablejs' ? ? export default { ? ? ? ? name: 'FileManage', ? ? ? ? data() { ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? fileManage: false, ? ? ? ? ? ? ? ? accessoryList : [1,2,3,4,5,6,7,8,9] ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? methods: { ? ? ? ? ? ? init () { ? ? ? ? ? ? ? ? this.fileManage = true ? ? ? ? ? ? ? ? this.$nextTick(() => { ? ? ? ? ? ? ? ? ? ? // Sortable.create(document.getElementById('sortAble'), {animation: 150}) ? ? ? ? ? ? ? ? ? ? this.rowDrop() ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? }, ? ? ? ? ? ? rowDrop() { ? ? ? ? ? ? ? ? let that = this ? ? ? ? ? ? ? ? // eslint-disable-next-line no-unused-vars ? ? ? ? ? ? ? ? let sortable = new Sortable(document.getElementById('sortAble'), { ? ? ? ? ? ? ? ? ? ? sort: true, ? ? ? ? ? ? ? ? ? ? animation: 150, ? ? ? ? ? ? ? ? ? ? onEnd: function (evt) { ? ? ? ? ? ? ? ? ? ? ? ? that.accessoryList.splice(evt.newIndex, 0, that.accessoryList.splice(evt.oldIndex, 1)[0]) ? ? ? ? ? ? ? ? ? ? ? ? let newArray = that.accessoryList.slice(0) ? ? ? ? ? ? ? ? ? ? ? ? that.accessoryList = [] ? ? ? ? ? ? ? ? ? ? ? ? that.$nextTick(function () { ? ? ? ? ? ? ? ? ? ? ? ? ? ? that.accessoryList = newArray ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? } ? ? ? ? } ? ? } </script> <style lang="scss" scoped> </style>
3、父組件中使用
<template> ?? ?<FileManage ref="FileManage"></FileManage> </template>
<script> import FileManage from './FileManage' export default { ?? ??? ?..... ?? ?methods: { ?? ??? ? ?open() { ?? ??? ??? ??? ?this.$refs.FileManage.init() ?? ??? ??? ?} ?? ?} } </script>
說明:這里利用了先加載dom完成后再調(diào)用
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 關(guān)于Element UI table 順序拖動(dòng)方式
- vue/Element?UI實(shí)現(xiàn)Element?UI?el-dialog自由拖動(dòng)功能實(shí)現(xiàn)
- 解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無法拖動(dòng)問題
- vue使用Element的Tree樹形控件實(shí)現(xiàn)拖動(dòng)改變節(jié)點(diǎn)順序方式
- element plus tree拖動(dòng)節(jié)點(diǎn)交換位置和改變層級(jí)問題(解決方案)
- elementplus+splitpanes實(shí)現(xiàn)左右拖動(dòng)控制寬度的項(xiàng)目實(shí)踐
相關(guān)文章
Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究
Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09關(guān)于pinia的使用和持久化方式(pinia-plugin-persistedstate)
本文介紹了Pinia的使用方法,包括安裝和配置插件pinia-plugin-persistedstate,以及在項(xiàng)目中創(chuàng)建和使用Store模塊,同時(shí),還講解了Pinia的state、getters和actions的使用,并提供了在uniapp中使用持久化插件的總結(jié)2025-02-02vite+vue3+tsx項(xiàng)目打包后動(dòng)態(tài)路由無法加載頁面的問題及解決
這篇文章主要介紹了vite+vue3+tsx項(xiàng)目打包后動(dòng)態(tài)路由無法加載頁面的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法
這篇文章主要介紹了vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Vue 對(duì)象和數(shù)據(jù)的強(qiáng)制更新方式
這篇文章主要介紹了Vue 對(duì)象和數(shù)據(jù)的強(qiáng)制更新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue項(xiàng)目中js-cookie的使用存儲(chǔ)token操作
這篇文章主要介紹了vue項(xiàng)目中js-cookie的使用存儲(chǔ)token操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11