【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

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据