- 保留小數位數
Vue.filter("toFixed", function (val, acc) { //保留小數位,acc為保留幾位小數位
let num = parseFloat(val);
if (isNaN(num)) {
num = 0;
}
let accuracy = parseInt(acc);
if (isNaN(accuracy) || accuracy < 0 || accuracy > 10) {
accuracy = 2;
}
return num.toFixed(accuracy);
});
- 轉百分比
Vue.filter("toPercent", function (val, acc) { //小數轉百分比 ,acc為保留小數位
let num = parseFloat(val);
if (isNaN(num)) {
num = 0;
}
let accuracy = parseInt(acc);
if (isNaN(accuracy) || accuracy < 0 || accuracy > 10) {
accuracy = 2;
}
return (num * 100).toFixed(accuracy) + "%"
});
- 通用鍵值轉換(數據源為數組)
import _ from 'underscore';
Vue.filter("keyToValueConverter", function (val, dataSource,keyname,valuename) {
if (val != null) {
val = val.toString().trim();
let matched = _.find(dataSource, function (obj) { return obj[keyname] == val; });
if (matched) {
return matched[valuename];
}
}
return "";
});
- 通用多鍵轉多值(數據源為數組)
import _ from 'underscore';
Vue.filter("keysToValuesConverter", function (val, dataSource, keyname, valuename, keySeparator, valueSeparator) { //默認分隔符為逗號
let text = "";
if (!keySeparator || typeof (keySeparator) != 'string') {
keySeparator = ',';
}
if (!valueSeparator || typeof (valueSeparator) != 'string') {
valueSeparator = ',';
}
if (val != null) {
let valueArr = val.split(',');
let nameArr = _.map(valueArr, function (valueItem) {
let matched = _.find(dataSource, function (item) { return item[keyname] == valueItem; });
return matched ? matched[valuename] : null;
});
nameArr = _.filter(nameArr, (item) => { return item != null })
if (nameArr && nameArr.length > 0) {
text = nameArr.join(',');
}
}
return text;
});
- 日期格式化(用法同C# "yyyy-MM-dd HH:mm:ss")
Vue.filter("dateTimeFormat", function (date, fmt = 'yyyy-MM-dd HH:mm:ss') { //日期時間格式化
if (!date) {
return ''
}
if (typeof date === 'string') {
date = date.replace('T', ' ').replace('Z', '');
date = new Date(date.replace(/-/g, '/'))
}
if (typeof date === 'number') {
date = new Date(date)
}
var o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S': date.getMilliseconds()
}
var week = {
'0': '\u65e5',
'1': '\u4e00',
'2': '\u4e8c',
'3': '\u4e09',
'4': '\u56db',
'5': '\u4e94',
'6': '\u516d'
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
}
return fmt
});
- 秒、毫秒(時長)格式化為時分秒(例:65000ms => 00:01:05)
//秒、毫秒轉時分秒顯示 例:65000 => 00:01:05
//參數:isMs 是否是毫秒;dft:默認顯示
Vue.filter('timeLongFormat', function (value, isMs=false,dft = '00:00:00') {
let total = parseInt(value);
if (!isNaN(total)) {
if (isMs) {
total = total/1000;
}
let hours = parseInt(total / 3600);
let minutes = parseInt((total % 3600) / 60);
let seconds = parseInt((total % 3600) % 60);
let h = hours > 9 ? hours : '0' + hours;
let m = minutes > 9 ? minutes : '0' + minutes;
let s = seconds > 9 ? seconds : '0' + seconds;
return h + ':' + m + ':' + s;
}
else {
return dft;
}
});
7.秒、毫秒(時長)格式化為時分秒(中文)(例:65000ms => 1分5秒)
Vue.filter("timeLongFormat_zh", function (valuevalue, isMs = false, dft = '--') {
let total = parseInt(value);
if (!isNaN(total)) {
if (isMs) {
total = total / 1000;
}
let hours = parseInt(total / 3600);
let minutes = parseInt((total % 3600) / 60);
let seconds = parseInt((total % 3600) % 60);
let h = hours == 0 ? "" : `${hours}時`;
let m = minutes == 0 ? "" : `${minutes}分`;
let s = seconds == 0 ? "" : `${seconds}秒`;
return h + m + s;
}
else {
return dft;
}
});
- byte轉K、M、G
Vue.filter("bytesToSize", function (bytes) { //文件大小單位轉換
if (bytes === 0) { return '0 B' };
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(4) + ' ' + sizes[i];
});
- 數據源是對象的鍵值轉換
Vue.filter("objectKeyToValueConverter", function (val, keyValueObject) {
var isExists = _.has(keyValueObject, val);
if (isExists) {
return keyValueObject[val];
}
return "";
});