欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中使用elementui與Sortable.js實(shí)現(xiàn)列表拖動(dòng)排序

 更新時(shí)間:2022年04月18日 17:16:15   作者:_yangyi  
這篇文章主要為大家詳細(xì)介紹了Vue中使用elementui與Sortable.js實(shí)現(xiàn)列表拖動(dòng)排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

  • Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究

    Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究

    Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • vue3基于script?setup語法$refs的使用

    vue3基于script?setup語法$refs的使用

    這篇文章主要介紹了vue3基于script?setup語法$refs的使用,<BR>?在用vue3開發(fā)項(xiàng)目的時(shí)候,需要調(diào)用子組件的方法,于是想著用$refs來實(shí)現(xiàn),但是我是使用script?setup語法糖,原先vue2的語法已經(jīng)不適用了。下面我們一起進(jìn)入文章看詳細(xì)內(nèi)容吧</P><P>
    2021-12-12
  • vue微信分享插件使用方法詳解

    vue微信分享插件使用方法詳解

    這篇文章主要介為大家詳細(xì)紹了vue微信分享插件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 關(guān)于pinia的使用和持久化方式(pinia-plugin-persistedstate)

    關(guān)于pinia的使用和持久化方式(pinia-plugin-persistedstate)

    本文介紹了Pinia的使用方法,包括安裝和配置插件pinia-plugin-persistedstate,以及在項(xiàng)目中創(chuàng)建和使用Store模塊,同時(shí),還講解了Pinia的state、getters和actions的使用,并提供了在uniapp中使用持久化插件的總結(jié)
    2025-02-02
  • vite+vue3+tsx項(xiàng)目打包后動(dòng)態(tài)路由無法加載頁面的問題及解決

    vite+vue3+tsx項(xiàng)目打包后動(dòng)態(tài)路由無法加載頁面的問題及解決

    這篇文章主要介紹了vite+vue3+tsx項(xiàng)目打包后動(dòng)態(tài)路由無法加載頁面的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue?elementUI?處理文件批量上傳方式

    vue?elementUI?處理文件批量上傳方式

    這篇文章主要介紹了vue?elementUI?處理文件批量上傳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法

    vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法

    這篇文章主要介紹了vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Vue 對(duì)象和數(shù)據(jù)的強(qiáng)制更新方式

    Vue 對(duì)象和數(shù)據(jù)的強(qiáng)制更新方式

    這篇文章主要介紹了Vue 對(duì)象和數(shù)據(jù)的強(qiáng)制更新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue項(xiàng)目中js-cookie的使用存儲(chǔ)token操作

    vue項(xiàng)目中js-cookie的使用存儲(chǔ)token操作

    這篇文章主要介紹了vue項(xiàng)目中js-cookie的使用存儲(chǔ)token操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 詳解Vue3中useLocalStorage的用法

    詳解Vue3中useLocalStorage的用法

    這篇文章主要為大家詳細(xì)介紹了Vue3中useLocalStorage用法的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-10-10

最新評(píng)論