zoukankan      html  css  js  c++  java
  • ElementUI 基于vue+sortable.js实现表格行拖拽

    基于vue+sortable.js实现表格行拖拽

    By:授客 QQ:1033553122

    实践环境

    sortablejs@1.13.0

    vue@2.6.11

    element-ui@2.13.2

    安装sortable.js拖拽库

    npm install sortablejs
    

    代码示例

    <template>
      <div class="demo-table-wrapper">
        <el-table :data="tableData" border style=" 100%">
          <el-table-column prop="date" label="日期" width="180"> </el-table-column>
          <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
          <el-table-column prop="address" label="地址"> </el-table-column>
        </el-table>
      </div>
    </template>
    
    <script>
    import Sortable from "sortablejs";
    
    export default {
      data() {
        return {
          tableData: [
            {
              date: "2016-05-02",
              name: "王小虎1",
              address: "上海市普陀区金沙江路 1518 弄1"
            },
            {
              date: "2016-05-04",
              name: "王小虎2",
              address: "上海市普陀区金沙江路 1517 弄2"
            },
            {
              date: "2016-05-01",
              name: "王小虎3",
              address: "上海市普陀区金沙江路 1519 弄3"
            },
            {
              date: "2016-05-03",
              name: "王小虎4",
              address: "上海市普陀区金沙江路 1516 弄4"
            }
          ]
        };
      },
    
      mounted() {
        this.dragRow();
      },
      methods: {
        // 行拖拽
        dragRow() {
          // 查找要拖拽元素的父容器
          const tbody = document.querySelector(
            ".demo-table-wrapper .el-table__body-wrapper tbody"
          );
    
          const _this = this;
          Sortable.create(tbody, {
            draggable: ".demo-table-wrapper .el-table__row", //  指定父容器下可被拖拽的子元素
    
            onEnd({ newIndex, oldIndex }) {
              /**
               * onEnd 拖拽结束后的事件处理函数
               * newIndex:目标位置对应行的索引
               * oldIndex:被拖拽行的索引
               * 
               * ====================(被拖拽记录行1)
               * before
               * ====================(拖拽至目标位置对应记录行)
               * after
               * ====================(被拖拽记录行2)
               * 如果从上往下拖拽,即newIndex > oldIndex,那么在目标位置对应记录行上移(目标位置对应记录行索引值减1),在newIndex所指位置插入被拖拽行(被拖拽行索引设置为newIndex),视觉效果就是在after位置(即目标位置对应行下方)插入被拖拽行
               * 如果从下往上拖拽,即newIndex < oldIndex,那么在目标位置对应记录行下移(目标位置对应记录行索引值加1),在newIndex所指位置插入被拖拽行(被拖拽行索引设置为newIndex),视觉效果就是在上述before位置(即目标位置对应行上方)插入被拖拽行
               * */ 
    
              console.log(newIndex, oldIndex);
              if(newIndex > oldIndex){
                  // 请求服务器做数据更新处理,然后根据处理结果对前端页面处理 
              } else {
                  // 请求服务器做数据更新处理 ,然后根据处理结果对前端页面处理
              }           
            }
          });
        }
       
      }
    };
    </script>
    
    <style scoped>
    .demo-table-wrapper {
    }
    </style>
    

    参考连接

    http://www.itxst.com/sortablejs/neuinffi.html

    作者:授客
    公众号:授客的知识库
    QQ:1033553122
    全国软件测试QQ交流群:7156436

    Git地址:https://gitee.com/ishouke
    友情提示:限于时间仓促,文中可能存在错误,欢迎指正、评论!
    作者五行缺钱,如果觉得文章对您有帮助,请扫描下边的二维码打赏作者,金额随意,您的支持将是我继续创作的源动力,打赏后如有任何疑问,请联系我!!!
                微信打赏                       支付宝打赏                        授课的知识库               全国软件测试交流QQ群  
                          

  • 相关阅读:
    jquery之实例应用
    jquery之文档操作
    jquery之css操作
    jquery属性的操作
    jquery筛选器
    jquery选择器之表单选择表单对象属性
    jquery选择器之属性选择器
    jquery选择器之子元素
    数位dp基础
    Leetcode 5195. 最长快乐字符串(贪心)
  • 原文地址:https://www.cnblogs.com/shouke/p/14380017.html
Copyright © 2011-2022 走看看