当前位置: 首页 > news >正文

Vue3项目filter.js组件封装

1、element-plus(el-table)修改table的行样式

export function elTableRowClassName({ row, rowIndex }) {if (rowIndex % 2 != 0) {return 'default-row'}
}

2、时间戳转换格式

export function parseTimeFilter(dateTime, dateType) {if (dateTime == '' || dateTime == undefined || dateTime == 0) {return '';}let date = new Date(parseInt(dateTime) * 1000);let Year = date.getFullYear();let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());let Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());if (dateType === 'YYYY/MM/DD') {return Year + '/' + Moth + '/' + Day} else if (dateType === 'YYYY-MM-DD') {return Year + '-' + Moth + '-' + Day} else if (dateType === 'YYYY/MM/DD HH:MM:SS') {return Year + '/' + Moth + '/' + Day + '   ' + Hour + ':' + Minute + ':' + Sechond;} else if (dateType === 'YYYYMMDDHHMMSS') {return Year + '' + Moth + '' + Day + '' + Hour + '' + Minute + '' + Sechond;}return Year + '-' + Moth + '-' + Day + '   ' + Hour + ':' + Minute + ':' + Sechond;
}

3、对象中过滤掉空的对象或没用到的对象

export function filtrationObject(options, optionsArr) {for (let key in options) {if (optionsArr.indexOf(key) === -1 || options[key] === '') {delete options[key];}}return options;
}export function filtrationObjectNull(options) {for (let key in options) {if (options[key] === '' || options[key] === null) {delete options[key];}}return options;
}

4、金额正则校验

export function amountRegularCheck(val) {let money = val.toString(); // 数值转成字符串money = money.replace(/[^\d.]+/, ''); // 禁止非数字和点money = money.replace(/^\./g, ''); // 禁止以点开头money = money.replace(/\.{2,}/g, '.'); // 禁止连两次输入点money = money.replace(/(\.)(\d*)(\1*)/g, "$1$2"); // 一个点后面禁止输入点money = money.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 禁止小数超过两位money = money.replace(/^0+/, '0'); // 禁止开头连续输入两个0if (Number(money) >= 1) {money = money.replace(/^0+/, ''); // 输入字符大于等于 1 时剔除开头的 0 }return money;
}

5、随机生成字符串

const NUMS = '0123456789';
const NUMSANDLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export function randomStr(n, isPureNum = false) {let possible = isPureNum ? NUMS : NUMSANDLETTERS;let ret = '';for (let i = 0; i < n; i++) {ret += possible.charAt(Math.floor(Math.random() * possible.length));}return ret;
}

6、获取路由中的path

export function getUrlPath() {let href = location.href;let start = href.indexOf('/#') + 2;let end = href.indexOf('?');if (end === -1) {end = href.length;}return href.slice(start, end);
}

7、下载文件通用函数

// 示例:then(dowloadFile(filename))
export function downloadFile(filename, content) {const blob = new Blob([content]);const file = filename;if ('download' in document.createElement('a')) { // 非IE下载const elink = document.createElement('a');elink.download = filename;elink.style.display = 'none';elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click();URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink);} else { // IE10+下载navigator.msSaveBlob(blob, file);}
}

8、判断小数是几位

// 获取类型
export function getType(obj) {return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}// 判断类型
export function isType(obj, type) {if (this.getType(type) !== 'string') {throw new Error('type param must be string in util.js');}return this.getType(obj) === type;
}// 是否是number
export function isNumber(val) {return this.isType(val, 'number');
}export function countDecimals(n) {if (!this.isNumber(n)) {n = parseFloat(n);}if (Math.floor(n) === n.valueOf()) {return 0;}return n.toString().split('.')[1].length;
}
http://www.lryc.cn/news/259506.html

相关文章:

  • Linux: pwd命令查看当前工作目录
  • 【深度学习】PHP操作mysql数据库总结
  • 【送书活动】探究AIGC、AGI、GPT和人工智能大模型
  • Apple Find My「查找」认证芯片找哪家,认准伦茨科技ST17H6x芯片
  • java.lang.IllegalArgumentException: Could not resolve placeholder XXX‘ in value
  • 自动机器学习是什么?概念及应用
  • el-date-picker限制选择7天内禁止内框选择
  • Navicat 技术指引 | 适用于 GaussDB 分布式的调试器
  • 人工智能导论习题集(3)
  • 2023一起益企广东省中小企业数字化赋能活动(深圳站)成功举办
  • MySQL之创建表
  • 选择大于努力-鸿蒙开发应用不适合当前企业的现状态(头部应用除外)推荐一套款平台框架可以写安卓iOS 鸿蒙为企业开源节流
  • 2023.12.12 关于 Java 反射详解
  • 【Qt QML入门】Image
  • Spark编程入门
  • JVM 内存分析工具 Memory Analyzer Tool(MAT)的深度讲解
  • 浅谈 USB Bulk 深入浅出 (3) - USB Bulk 装置传输的注意事项
  • c语言结构体调用格式与对齐
  • 服务器常用命令介绍和负载监控的工具插件推荐
  • linux 防火墙systemctl (个人笔记)
  • 处理器中store指令的处理
  • 杨辉三角形-第11届蓝桥杯选拔赛Python真题精选
  • 我们一起做过的SPA——Nuxt.js介绍
  • java导出word使用模版与自定义联合出击解决复杂表格!
  • GO设计模式——9、过滤器模式(结构型)
  • fastadmin 导出
  • 六、CM4树莓派USBRS转485串口通讯
  • c++知识总结
  • python-爬取壁纸
  • 第31期 | GPTSecurity周报