elementui + vue2实现表格行的上下移动

场景:

在这里插入图片描述
如上,要实现表格行的上下移动

实现:

<el-dialog
          append-to-body
          title="条件编辑"
          :visible.sync="dialogVisible"
          width="60%"
        >
          <el-table :data="data1" border style="width: 100%">
            <el-table-column type="index" label="序" width="100" align="center">
            </el-table-column>
            <el-table-column prop="edit" label="" align="center">
              <template slot-scope="scope">
                <el-button
                  :disabled="scope.$index === 0"
                  plain
                  type="primary"
                  icon="el-icon-top"
                  @click="moveUp(scope.$index, scope.row)"
                  >上移</el-button
                >
                <el-button
                :disabled="scope.$index === data1.length - 1"
                  plain
                  type="primary"
                  icon="el-icon-bottom"
                  @click="moveDown(scope.$index, scope.row)"
                  >下移</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
  // 上移
    moveUp(index, row) {
   
     if(index !== 0){
   
      const currentRow = row;
      this.data1.splice(index, 1);
     this.data1.splice(index - 1, 0, currentRow);
     }
    },
    // 下移
    moveDown(index, row) {
   
     if(index !== this.data1.length-1){
   
      const currentRow = row;
      this.data1.splice(index, 1);
     this.data1.splice(index + 1, 0, currentRow);
     }
    },

参考:

点击上移或下移按钮对当前行进关联操作,如果是表格第一行则不能上移,如果是表格的最后一行,则不能进行下移,不能思意就是禁用。
当前需要获取到表格的index,可以通过:row-class-name="tableRowClassName"这个方法


---

# 原因分析:
> 上移禁用功能可以根据row.index ===0 来判断,下移禁用根据row.index === tableDate.length - 1 来判断

---

# 解决方案:
>提示:这里填写该问题的具体解决方案:
关键性代码:
   //  上移功能
	moveUp(index) {
   
      const currentRow = this.tableData.splice(index, 1)[0]
      this.tableData.splice(index - 1, 0, currentRow)
    }
  //  下移功能
  moveDown(index) {
   
      const currentRow = this.tableData.splice(index, 1)[0]
      this.tableData.splice(index + 1, 0, currentRow)
   }




原文

splice的使用

在这里插入图片描述
原文

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-17 03:26:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-17 03:26:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-17 03:26:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-17 03:26:02       20 阅读

热门阅读

  1. (第29天)Oracle 数据泵传输表空间

    2023-12-17 03:26:02       37 阅读
  2. ArcGIS Pro SDK 将几何输出为要素

    2023-12-17 03:26:02       43 阅读
  3. epoll timer实现定时任务

    2023-12-17 03:26:02       29 阅读
  4. 【C++ 语法】__VA_ARGS__ 可变参数宏

    2023-12-17 03:26:02       28 阅读
  5. 记录 | ubuntu安装jdk1.6

    2023-12-17 03:26:02       40 阅读
  6. 前端深浅拷贝各有哪些方法,优缺点

    2023-12-17 03:26:02       40 阅读
  7. 二维数组——特征匹配(c++)

    2023-12-17 03:26:02       33 阅读
  8. 最流行的视频创作者

    2023-12-17 03:26:02       36 阅读