vue中实现数组元素的上移和下移

本文介绍了一种使用JavaScript实现列表项上移和下移功能的方法,通过修改数组内部元素的位置来达到目的,并提供了禁用无效操作的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们需要实现列表元素上移和下移交换位置,我们把数组数据渲染到视图中,可以通过数组元素交换位置来实现上移和下移功能

一、要移动的数组列表

  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]));
    },
  },

三、效果

四、注意

当元素处于第一个位置时,禁止让它继续上移,当元素处于最后一个位置时,禁止让它继续下移。我们给按钮增加disabled属性,index为for循环渲染视图中的index

    <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>

文章每周持续更新,可以微信搜索「 前端大集锦 」第一时间阅读,回复【视频】【书籍】领取200G视频资料和30本PDF书籍资料

Vue.js项目中实现表格选中行的上下移动功能,可以结合`v-model`绑定数据列表、通过操作数组索引来调整元素顺序,并利用事件触发来完成这一需求。 以下是基本思路及步骤: ### 实现原理 1. **维护一份数据源**:将表格的数据存储在一个数组变量中,例如 `tableData`。 2. **监听按钮点击事件**: - 上移逻辑:找到当前选中项在其数组中的位置(即索引),将其从当前位置取出并插入到前一位的位置。 - 下移逻辑:同样定位选中项的索引值,然后把它放置到其后的那个位置。 3. **更新视图**:由于 Vue 的响应式机制,当修改了原数组之后,前端展示的内容会自动刷新同步最新状态。 ### 示例代码片段 ```vue <template> <div> <!-- 表格 --> <el-table :data="tableData" @selection-change="handleSelectionChange"> <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table> <!-- 操作按钮 --> <button @click="moveUp">上移</button> <button @click="moveDown">下移</button> </div> </template> <script> export default { data() { return { tableData: [ { name: "张三", age: 20 }, { name: "李四", age: 25 }, // 更多初始数据... ], selectedRows: [], // 存储选中的行信息 }; }, methods: { handleSelectionChange(selection) { this.selectedRows = selection; }, moveUp() { if (this.selectedRows.length === 0) return; let movedAny = false; // 标记是否真的发生了移动 for (let i = 0; i < this.tableData.length; i++) { const row = this.tableData[i]; if ( this.selectedRows.includes(row) && i > 0 && !this.selectedRows.includes(this.tableData[i - 1]) ) { [this.tableData[i], this.tableData[i - 1]] = [this.tableData[i - 1], this.tableData[i]]; movedAny = true; } } if (!movedAny) alert("无法继续向上移动!"); }, moveDown() { if (this.selectedRows.length === 0) return; let movedAny = false; // 同样用于标记实际变动情况 for (let j = this.tableData.length - 1; j >= 0; j--) { const row = this.tableData[j]; if( this.selectedRows.includes(row)&&j<this.tableData.length-1&&!this.selectedRows.includes(this.tableData[j+1])) ){ [this.tableData[j + 1],this.tableData[j]]=[this.tableData[j ],this.tableData[j + 1]] movedAny=true} } if(!movedAny){ alert('已达到底部,不能再往下移动啦')} } } }; </script> ``` 上述例子采用了Element UI框架构建了一个简易版支持行列选择以及相应位移的功能模块。其中关键点在于如何准确获取用户勾选项的信息并通过程序控制它们相对次序的变化。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Demi

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值