antd-Table-上移下移问题

8次阅读

共计 983 个字符,预计需要花费 3 分钟才能阅读完成。

[状态管理 mobx](https://cn.mobx.js.org/)
import {observable ,action, toJS} from 'mobx';

@observable DataSource = []; // 表格列表数据
@observable selectedObj = {}; // 选中的数据
@observable selectedRowKey = ''; // 当前行 index


// 上移
@action MoveUp(){
 // 下面三行代码意思就相当于冒泡排序 替换 ordernum 这个值
      let x = this.selectedRowKey
      let orderNum = this.DataSource[x- 1].orderNum; // 点击当前行 index 的上一条数据 (根据 index 获取数据)
      this.DataSource[x - 1].orderNum = this.DataSource[x].orderNum; // 上移的 orderNum 等于 当前点击数据的 orderNum
      this.DataSource[x].orderNum = orderNum; // 当前数据的 orderNum 等于上移 orderNum
      this.selectedObj.orderNum = orderNum;
      // 使用 splice 来交换数据
      this.DataSource
        .splice(x- 1, 1, ...this.DataSource
          .splice((x+ 1) - 1, 1, this.DataSource[x - 1]));
}


// 下移
@action moveDown(){
      // 同理上移
      let x = this.selectedRowKey + 1;
      let y = this.selectedRowKey + 2;
      let a = this.selectedRowKey;
      let orderNum = this.DataSource[a + 1].orderNum; 
      this.DataSource[a + 1].orderNum = this.DataSource[a].orderNum; 
      this.DataSource[a].orderNum = orderNum; 
      this.selectedObj.orderNum = orderNum;
      this.DataSource
        .splice(x - 1, 1, ...this.DataSource
          .splice(y - 1, 1, this.DataSource[x - 1]))

}
     

正文完
 0