vue对数组元素的上移下移和置顶等操作

1、要移动的数组列表

  data() {
    return {
      questionList: [
        { question: "第一个" },
        { question: "第二个" },
        { question: "第三个" },
        { question: "第四个" },
        { question: "第五个" },
      ],
    };
  },

二、上移、下移、置顶函数

  methods: {
    // 上移
    clickUp(index) {
      let arr = this.questionList;
      arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]));
    },
    // 下移
    clickDown(index) {
      let arr = this.questionList;
      arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]));
    },
    // 补充置顶的操作
      arr.unshift(arr.splice(index , 1)[0]);

  },

三、页面代码

    <div v-for="(item, index) in questionList" :key="index">
      <p>{{item.question}}</p>
      <div class="btns">
        <!-- 上移 -->
        <Button :disabled="index == 0" @click="clickUp(index)"></Button>
        <!-- 下移 -->
        <Button :disabled="index == questionList.length - 1" @click="clickDown(index)"></Button>
      </div>
    </div>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值