博客
关于我
vue页面数组做数据置顶
阅读量:529 次
发布时间:2019-03-08

本文共 819 字,大约阅读时间需要 2 分钟。

`

//置顶功能async function stickHonor(index) {    const obj = this.honorList[index];    const id = obj.id;    obj.isStick = 1;    // 调用接口置顶    const res = await stickStudentHonors({id, isStick: 1});    // 删除原位置    this.honorList.splice(index, 1);    // 将元素插入到数组开头    this.honorList.splice(0, 0, obj);}//取消置顶功能async function unstickHonor(index) {    // 计算当前被置顶的元素总数    let sum = 0;    for (const data of this.honorList) {        if (data.isStick === 1) {            sum++;        }    }    const obj = this.honorList[index];    const id = obj.id;    // 调用接口取消置顶    const res = await stickStudentHonors({id, isStick: 0});    // 删除原位置    this.honorList.splice(index, 1);    // 将元素插入到置顶前面一个位置    this.honorList.splice(sum - 1, 0, obj);}

置顶和取消置顶功能是前端常见的逻辑操作。上述代码实现了基于数组操作的置顶逻辑,其中使用splice方法进行元素插入和删除。通过这种方法,数组会自动响应式更新,避免了手动刷新的需求。

转载地址:http://ffviz.baihongyu.com/

你可能感兴趣的文章
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>
Node的Web应用框架Express的简介与搭建HelloWorld
查看>>
Node第一天
查看>>
node编译程序内存溢出
查看>>
Node读取并输出txt文件内容
查看>>
node防xss攻击插件
查看>>
noi 1996 登山
查看>>
noi 7827 质数的和与积
查看>>