element UI-表格数据上下移动功能

15 篇文章 0 订阅
12 篇文章 0 订阅

1.在<el-table>中增加一列

        <el-table-column label="操作"  >
          <template slot-scope="scope">
            <el-button size="mini" type='text' :disabled="scope.$index == 0" @click.stop="sortUp(scope.$index, scope.row)">向上↑ </el-button>
            <el-button size="mini" type='text' :disabled="(scope.$index + 1) == tableData.length" @click.stop="sortDown(scope.$index, scope.row)">向下↓</el-button>
          </template>
        </el-table-column>

2.函数如下:

    // 上移按钮
    sortUp (index, row) {
      console.log(index)
      if (index === 0) {
        this.$message({
          message: '已经是第一个了,爬不上去了',
          type: 'warning'
        })
      } else {
        let temp = this.tableData[index - 1]
        this.$set(this.tableData, index - 1, this.tableData[index])
        this.$set(this.tableData, index, temp)
      }
    },
    // 下移按钮
    sortDown (index, row) {
      if (index === (this.tableData.length - 1)) {
        this.$message({
          message: '已经是最后一个了,没有下降空间了',
          type: 'warning'
        })
      } else {
        let i = this.tableData[index + 1]
        this.$set(this.tableData, index + 1, this.tableData[index])
        this.$set(this.tableData, index, i)
      }
    }

3.如果还需要顺序标识的话,可以用index赋值。
4.上移、下移,是否可用的判断由disabled或函数中的if逻辑实现
5.this.$set( target, key, value ),为对象添加一个新属性时调用,方法说明
target---操作对象
key---要改变的位置
value---赋值

this.$set(this.tableData, index, i)
即为,改变this.tableData的index元素为i

6.实现效果

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值