博客
关于我
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/

你可能感兴趣的文章
Paystack Android SDK 集成与使用指南
查看>>
PC端编辑 但能在PC端模拟移动端预览的富文本编辑器
查看>>
PDF中的Pandoc语法突出显示不起作用
查看>>
pdf做成翻页电子书_第一弹:常见BOOX电子书阅读器问题解答,这些技能你都会吗?...
查看>>
PDF文字识/编辑?这个工具真的很强大!
查看>>
pdf文档出现乱码如何修改
查看>>
PDO中捕获SQL语句中的错误
查看>>
Penetration Testing、Security Testing、Automation Testing
查看>>
percona-xtrabackup 备份
查看>>
Perl的基本語法
查看>>
perl输出中文有乱码
查看>>
PermissionError:[Errno 13] 权限被拒绝:‘/manage.py‘
查看>>
php -- 魔术方法 之 判断属性是否存在或为空:__isset()
查看>>
php -树-二叉树的实现
查看>>
php csv 导出
查看>>
PHP imap 远程命令执行漏洞复现(CVE-2018-19518)
查看>>
php include和require
查看>>
php JS 导出表格特殊处理
查看>>
php mysql优化方法_MySQL优化常用方法
查看>>
PHP OAuth 2.0 Server
查看>>