【Vue2】一个数组按时间分割为【今年】和【往年】俩个数组

2024-01-11 06:44

本文主要是介绍【Vue2】一个数组按时间分割为【今年】和【往年】俩个数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一. 需求

后端返回一个数组,前端按时间维度将该数组的分割为【今年】和【往年】俩个数组
  1. 后端返回的数组格式如下
timeList:[{id:1,billTime:"2024-01-10",createTime:"2024-01-10 00:00:00",status:0},{id:2,billTime:"2022-05-25",createTime:"2022-05-25 00:00:00",status:1},{id:3,billTime:"2022-03-23",createTime:"2022-03-23 00:00:00",status:2},{id:4,billTime:"2022-02-11",createTime:"2022-02-11 00:00:00",status:0},{id:5,billTime:"2022-01-01",createTime:"2022-01-01 00:00:00",status:2},{id:6,billTime:"2021-12-01",createTime:"2021-12-01 00:00:00",status:2},{id:7,billTime:"2021-11-01",createTime:"2021-11-01 00:00:00",status:2},
]
  1. 需要的数组如下
// 往年
preYear:[{billTime:"2022-05",value:"2022-05"},{billTime:"2022-03",value:"2022-03"},{billTime:"2022-02",value:"2022-02"},{billTime:"2022-01",value:"2022-01"},{billTime:"2021-12",value:"2021-12"},{billTime:"2021-11",value:"2021-11"},
]
// 今年
newYear:[{billTime:"2024-01",value:"2024-01"},
]
  1. 效果如下
    全部账期【今年账期】(点击切换标签)、往年账期(下拉框)

在这里插入图片描述

二. 实现

<template><div><!-- 本年账期 --><el-radio-group v-model="newTime" @change="handleChangeNewTime" size="mini"><el-radio-button v-for="(item,index) in newYear" :key="index" :label="item.billTime" :value="item.value"><template><span>{{item.value}}</span></template></el-radio-button></el-radio-group><!-- 往年账期 --><el-button size="mini" style="margin-left: 20px" split-button>往年账期</el-button><el-select @change="billTimeSearch" v-model="preTime" size="mini" placeholder="请选择" clearable><el-option v-for="(item,index) in preYear" :key="index" :label="item.billTime" :value="item.billTime"></el-option></el-select><div>
</template><script>
import { getBillTime,getList } from "@/api/xxx"
import { dateFormat } from "@/utils/index";export default{data(){return{query:{pageNum:1,pageSize:10,startTime:"" // 选中的账期},list:[],listLoading:false,newYear:[], // 本年账期列表preYear:[], // 往年账期列表preTime:"", // 选中的往年账期newTime:""	// 选中的今年账期}},mounted(){this.fetchData()this.getBillTime()},methods:{/*** 初始化列表数据*/fetchData(){this.listLoading = truegetList(this.query).then(res=>{this.list = res.data.listthis.listLoading = false})},/*** 获取列表,并按照时间分割为【往年】和【今年】账期*/getBillTime(){getBillTime().then(res=>{let currentYear = this.formatDateYear(new Date())res.data.list.forEach(item=>{item.billTime = this.formatDate(item.billTime)let obj = {billTime:item.billTime,value:item.billTime};if(item.billTime.indexOf(currentYear) == -1 || this.newYear.length == 12){this.preYear.push(obj)// 去重this.preYear = this.preYear.reduce((preVal,curVal) => {object[curVal.billTime] ? "" : (object[curVal.billTime] = preVal.push(curVal));return preVal;}, []);}else{this.newYear.push(obj);// 账单日重复问题let object = {};this.newYear = this.newYear.reduce((preVal, curVal) => {object[curVal.billTime] ? "" : (object[curVal.billTime] = preVal.push(curVal));return preVal;}, []);}});var result = this.newYear.some(item => {if (item.billTime == "") {return true;}});if (!result) {// 如果存在this.newYear.unshift({ billTime: "", value: "全部账单" });}})},/*** 切换今年账单日标签项* @param {String} val */handleChangeNewTime(val) {this.query.startTime = val;this.preTime = ""// this.$refs.orderListRef.clearSelection();this.fetchData();},/*** 往年账单筛选 此处仅需要传一个值年月即可,后端进行模糊查询* @param {String} val */billTimeSearch(val) {this.query.startTime = val;this.newTime = ""this.fetchData();},/*** 账单日格式化* @param {Date} date */formatDate(date) {return dateFormat(new Date(date), "yyyy-MM");},formatDateYear(date) {return dateFormat(new Date(date), "yyyy");},}}
</script>

三. 时间转换

@/utils/index

//格式化时间
export function dateFormat(date, format) {format = format || "yyyy-MM-dd hh:mm:ss";if (date !== "Invalid Date") {let o = {"M+": date.getMonth() + 1, //month"d+": date.getDate(), //day"h+": date.getHours(), //hour"m+": date.getMinutes(), //minute"s+": date.getSeconds(), //second"q+": Math.floor((date.getMonth() + 3) / 3), //quarterS: date.getMilliseconds() //millisecond};if (/(y+)/.test(format))format = format.replace(RegExp.$1,(date.getFullYear() + "").substr(4 - RegExp.$1.length));for (let k in o)if (new RegExp("(" + k + ")").test(format))format = format.replace(RegExp.$1,RegExp.$1.length === 1 ?o[k] :("00" + o[k]).substr(("" + o[k]).length));return format;}return "";
}

这篇关于【Vue2】一个数组按时间分割为【今年】和【往年】俩个数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/593486

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

MySQL按时间维度对亿级数据表进行平滑分表

《MySQL按时间维度对亿级数据表进行平滑分表》本文将以一个真实的4亿数据表分表案例为基础,详细介绍如何在不影响线上业务的情况下,完成按时间维度分表的完整过程,感兴趣的小伙伴可以了解一下... 目录引言一、为什么我们需要分表1.1 单表数据量过大的问题1.2 分表方案选型二、分表前的准备工作2.1 数据评估