ElementUI+sortablejs实现列表拖拽功能

ElementUI+sortablejs实现列表拖拽功能

一、简介

SortableJS 是一个用于实现拖放排序功能的 JavaScript 库,可以帮助开发者轻松地实现拖拽元素重新排序的功能。以下是 SortableJS 的基本用法。

二、使用

2.1 安装 SortableJS

如果使用 npm,可以运行以下命令进行安装:

npm install sortablejs
2.2 安装 引入 SortableJS

在 HTML 文件中引入 SortableJS 的库文件:

import Sortable from 'sortablejs'
2.3 完整代码示例
<template>
    <div>
      <el-table :data="planTableData"
                row-key="id">
        <el-table-column prop="createTime"
                         label="时间"
                         width="180"></el-table-column>
        <el-table-column prop="event"
                         label="事件"
                         width="180"></el-table-column>
        <!-- 其他列 -->
      </el-table>
    </div>
  </template>
  
  <script>
  import Sortable from 'sortablejs'
  import axios from 'axios' // 引入axios进行HTTP请求
  
  export default {
    name: 'PlanTableDraggable',
    data () {
      return {
        planTableData: []
      }
    },
    created () {
      this.planTableData = [
        { id: 1, createTime: '2023-01-01', event: '事件1' },
        { id: 2, createTime: '2023-01-02', event: '事件2' },
        { id: 3, createTime: '2023-01-03', event: '事件3' }
        // ...更多测试数据
      ]
    },
    mounted () {
      this.$nextTick(() => {
        const el = this.$el.querySelector('.el-table__body-wrapper tbody')
        Sortable.create(el, {
          onEnd: (event) => {
            const { oldIndex, newIndex } = event
            this.updateRowOrder(oldIndex, newIndex)
          }
        })
      })
    },
    methods: {
      updateRowOrder (oldIndex, newIndex) {
        const movedItem = this.planTableData.splice(oldIndex, 1)[0]
        this.planTableData.splice(newIndex, 0, movedItem)
        this.updateOrderOnServer()
      },
      updateOrderOnServer () {
        axios.post('/api/update-order', { newOrder: this.planTableData })
          .then(response => {
            console.log('Order updated:', response)
          })
          .catch(error => {
            console.error('Error updating order:', error)
            // 可能需要回滚操作
          })
      }
    }
  }
  </script>
  

相关推荐

  1. ElementUI+sortablejs实现列表功能

    2024-03-20 08:36:01       26 阅读
  2. 实现功能实战示例

    2024-03-20 08:36:01       15 阅读
  3. 【工具】VUE 前端列表功能代码

    2024-03-20 08:36:01       44 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-20 08:36:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-20 08:36:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-20 08:36:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-20 08:36:01       20 阅读

热门阅读

  1. React——关于表单元素

    2024-03-20 08:36:01       25 阅读
  2. 离散制造企业MES与流程企业MES的区别

    2024-03-20 08:36:01       20 阅读
  3. React.js快速入门教程

    2024-03-20 08:36:01       17 阅读
  4. 虚拟DOM是什么以及React 和Vue中有何区别

    2024-03-20 08:36:01       18 阅读
  5. 华岳M9制造企业管理软件业务流程 2/4

    2024-03-20 08:36:01       18 阅读
  6. 北斗校时服务器(GPS授时服务器,NTP同步时钟)

    2024-03-20 08:36:01       18 阅读
  7. uniapp小程序接入trtc-wx

    2024-03-20 08:36:01       16 阅读
  8. 2024-03-19 事业-代号s-商城系统-记录

    2024-03-20 08:36:01       20 阅读
  9. (css)element-ui表单整体居中

    2024-03-20 08:36:01       18 阅读
  10. ARM-GPIO电点灯实验

    2024-03-20 08:36:01       21 阅读