关于数组:前端实现数组上移下移

// 上移
upData(text, record, index, column) {
  if (index === 0) {
    return;
  } else {
    // sort是排序 后端要的
    this.tableData[index - 1].sort =
      Number(this.tableData[index - 1].sort) + 1;
    record.sort = Number(record.sort) - 1;
  }
  // 在上一项插入该项
  this.tableData.splice(index - 1, 0, this.tableData[index]);
  // 删除后一项
  this.tableData.splice(index + 1, 1);
  this.$message.success("上移胜利");
}
// 下移
downData(text, record, index, column) {
  if (index === this.tableData.length - 1) {
    return;
  } else {
    // sort是排序 后端要的
    this.tableData[index + 1].sort =
      Number(this.tableData[index + 1].sort) - 1;
    record.sort = Number(record.sort) + 1;
  }
  // 在下一项插入该项
  this.tableData.splice(index + 2, 0, this.tableData[index]);
  // 删除前一项
  this.tableData.splice(index, 1);
  this.$message.success("下移胜利");
}
// 删除
this.tableData.splice(index, 1);

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理