美文网首页JavaScript
js拖动排序功能

js拖动排序功能

作者: 剑指流云 | 来源:发表于2022-07-28 09:22 被阅读0次

拖动后直接更新数据到服务端即可,如果是vue,则需要设置key,请不要使用index作为key,如果是element-ui的table,则需要设置row-key属性,rowDrop方法需要在mounted生命周期函数中调用

import Sortable from "sortablejs";
// 行拖拽
    rowDrop() {
      // 此时找到的元素是要拖拽元素的父容器
      const tbody = document.querySelector(".el-table__body-wrapper tbody");
      const _this = this;
      Sortable.create(tbody, {
        //  指定父元素下可被拖拽的子元素
        draggable: ".el-table__row",

        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0];
          _this.tableData.splice(newIndex, 0, currRow);
          // 调后台拖拽排序接口
          _this._updateMoveData(newIndex, oldIndex, currRow);
        },
      });
    },
  },
// 拖拽更新接口
    _updateMoveData(nIndex, oIndex, crow) {
      if (nIndex == oIndex) return;
      let list = this.tableData.map((item, index) => {
        return { id: item.pk, show_id: ++index };
      });
      this.$axios.post("/admin/banner/sequence", { list }).then((res) => {
        if (res.code == 1) {
          this.$message.success("调整成功");
        }
      });
    },

相关文章

  • js拖动排序功能

    拖动后直接更新数据到服务端即可,如果是vue,则需要设置key,请不要使用index作为key,如果是elemen...

  • UITableViewCell拖动排序

    UITableViewCell拖动排序功能系统本身就有的,不过系统的只能长按一个按钮才能拖动,如何实现整行可以长按...

  • 如何优雅的实现数据置顶、置尾、交换、拖动排序?

    前言 有管理后台的地方,就肯定会有排序功能,有排序功能肯定就会要求良好的用户体验:置顶、置尾、交换、拖动。如何针对...

  • RecyclerView实现拖动排序

    最近项目中需要实现对某一类条目进行拖动排序功能,实现技术方案就是利用ItemTouchHelper绑定Recycl...

  • RecycleView的拖动排序

    列表拖动排序功能也算是比较常见的了。在RecycleView还没有出现的那个年代,依稀记得是重写GridView实...

  • 一款带排序功能的瀑布流插件

    今天我要跟大家分享的是一款带有排序功能的瀑布流插件【sortableJs】 它是一款带排序功能的js masonr...

  • DOM拖拽排序(JavaScript)

    实现拖拽排序功能,废话少说,上代码 html部分 js部分 效果如下

  • item可拖拽的GridView实现

    前言 之前的项目中有个类似网易新闻的标签排序功能.长按某个标签后可拖动进行排序,当时用GridView实现的,今天...

  • 实现简单的拖动

    拖动div实现 标签(空格分隔): js js 实现 2.jQuery 实现

  • react.js 拖拽

    react.js拖拽排序功能的实现 1.使用 react-dnd npm install--save react-...

网友评论

    本文标题:js拖动排序功能

    本文链接:https://www.haomeiwen.com/subject/yugtwrtx.html