element 表格实现置顶/上移/下移操作(基于vue)

需求:

表格支持上移,下移,置顶,当第一行的时候,置顶/上移按钮不可点击
最后一行下移按钮不可点击,点击提交再一并请求数据。上图效果
在这里插入图片描述

  • 实现步骤1:
 <el-table-column prop="name" min-width="200" fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="getTopMove(scope.row,scope.$index)" :disabled="scope.$index==0">
              置顶
            </el-button>
            <el-button type="text" size="small" @click="getUpMove(scope.row,scope.$index)" :disabled="scope.$index==0">
              上移
            </el-button>
            <el-button type="text" size="small" @click="getDownMove(scope.row,scope.$index)"
                       :disabled="dataList.length-1==scope.$index">下移
            </el-button>
          </template>
        </el-table-column>
  • 实现步骤2:
methods: {
//置顶
  getTopMove(row, index) {
      this.dataList.splice(index, 1)
      this.dataList.unshift(row)
    },
    //上移
    getUpMove(row, index) {
      let arr = this.dataList;
      arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]));
    },
    //下移
    getDownMove(row, index) {
      let arr = this.dataList;
      arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]));
    },
 }

到这就结束了,保存的时候直接拿到dataList去请求接口完事。

  • 13
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue实现table行的上移下移,需要先在表格中添加对应的操作按钮,然后在点击按钮时调用接口进行数据的上移下移操作。具体实现步骤如下: 1. 在表格中添加上移下移按钮,可使用element-ui中的按钮组件: ``` <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="text" icon="el-icon-arrow-up" @click="handleMoveUp(scope.row)">上移</el-button> <el-button type="text" icon="el-icon-arrow-down" @click="handleMoveDown(scope.row)">下移</el-button> </template> </el-table-column> ``` 2. 在methods中定义handleMoveUp和handleMoveDown方法,分别调用接口进行数据上移下移操作: ``` methods: { // 上移操作 handleMoveUp(row) { const index = this.tableData.indexOf(row) if (index > 0) { // 调用上移接口 this.$axios.post('/api/moveUp', {id: row.id}).then(res => { this.tableData.splice(index, 1) this.tableData.splice(index - 1, 0, row) }) } }, // 下移操作 handleMoveDown(row) { const index = this.tableData.indexOf(row) if (index < this.tableData.length - 1) { // 调用下移接口 this.$axios.post('/api/moveDown', {id: row.id}).then(res => { this.tableData.splice(index, 1) this.tableData.splice(index + 1, 0, row) }) } } } ``` 3. 在接口中实现数据上移下移操作,可使用数据库中的排序字段进行排序: ``` // 上移接口 app.post('/api/moveUp', (req, res) => { const id = req.body.id // 查询当前行和前一行的数据 const sql = `SELECT *, (SELECT sort FROM table WHERE id = ${id-1}) AS prevSort FROM table WHERE id = ${id}` connection.query(sql, (err, results) => { if (err) throw err // 如果当前行不是第一行,则进行上移操作 if (results[0].prevSort) { const prevSort = results[0].prevSort const currSort = results[0].sort // 交换当前行和前一行的排序字段 const sql1 = `UPDATE table SET sort = ${prevSort} WHERE id = ${id}` const sql2 = `UPDATE table SET sort = ${currSort} WHERE id = ${id-1}` connection.query(sql1, (err, results) => { if (err) throw err connection.query(sql2, (err, results) => { if (err) throw err res.json({code: 0, message: '移动成功'}) }) }) } else { res.json({code: -1, message: '当前行已经是第一行,无法上移'}) } }) }) // 下移接口 app.post('/api/moveDown', (req, res) => { const id = req.body.id // 查询当前行和后一行的数据 const sql = `SELECT *, (SELECT sort FROM table WHERE id = ${id+1}) AS nextSort FROM table WHERE id = ${id}` connection.query(sql, (err, results) => { if (err) throw err // 如果当前行不是最后一行,则进行下移操作 if (results[0].nextSort) { const nextSort = results[0].nextSort const currSort = results[0].sort // 交换当前行和后一行的排序字段 const sql1 = `UPDATE table SET sort = ${nextSort} WHERE id = ${id}` const sql2 = `UPDATE table SET sort = ${currSort} WHERE id = ${id+1}` connection.query(sql1, (err, results) => { if (err) throw err connection.query(sql2, (err, results) => { if (err) throw err res.json({code: 0, message: '移动成功'}) }) }) } else { res.json({code: -1, message: '当前行已经是最后一行,无法下移'}) } }) }) ``` 以上是基本的实现思路,具体实现过程中需要根据实际业务进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值