elemetn 表格行与行之间实现上移下移

导读:本篇文章讲解 elemetn 表格行与行之间实现上移下移,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

实现效果

在这里插入图片描述


html

  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="redeemNum" label="已兑换"></el-table-column>
      <el-table-column prop="stock" label="兑换库存"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="rank" label="排序">
        <!-- //上移下移  (scope.$index)点击事件拿到当前下标-->
        <template slot-scope="scope">
          <div class="upper" @click="handelUpper(scope.$index)">
            <img src="@/assets/img/上.png" alt />
          </div>
          <div class="lower" @click="handelDown(scope.$index)">
            <img src="@/assets/img/下.png" alt />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handelEdit(scope.row.id)">编辑</el-button>
          <el-button @click="handleDel(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

data

 data() {
    return {
		tableData:[],//表格数据
	};
  },

js

 methods: {
    // 上移
    handelUpper(index) {
      // 后台需要的上层以及下层的id
      const data = {
        downId: this.tableData[index].id,//下层id
        upId: this.tableData[index - 1].id,//上层id
      };
      // MoveDown 接口
      MoveDown(data).then((res) => {
        console.log(res, "上移");
        if (res.code == 200) {
          //  this.IntegralGoodsInfoInfo 调用表格的方法 (this.currentPage, this.pageSize)里面两个参数分别是页码 页数
          this.IntegralGoodsInfoInfo(this.currentPage, this.pageSize);
        } else if (res.code == -2001) {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    // 下移
    handelDown(index) {
      // 后台需要的上层以及下层的id
      const data = {
        downId: this.tableData[index + 1].id,//下层id
        upId: this.tableData[index].id,//上层id
      };
      // MoveDown 接口
      MoveDown(data).then((res) => {
        console.log(res, "下移");
        if (res.code == 200) {
          //  this.IntegralGoodsInfoInfo调用表格的方法 (this.currentPage, this.pageSize) 里面两个参数分别是页码 页数
          this.IntegralGoodsInfoInfo(this.currentPage, this.pageSize);
        } else if (res.code == -2001) {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
  },

拓展

可能有些同学觉得点击事件的方式太麻烦了,可不可以通过鼠标拖动的方式去改变呢?答案是可以的,具体操作请参考博主另一篇文章(vue基于SortableJS实现表格拖动排序)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/79439.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!