zoukankan      html  css  js  c++  java
  • vue中常用插件(货币、日期)

    货币插件:
    价格格式化

    // https://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/currency.js
    
    const digitsRE = /(d{3})(?=d)/g
    /**
     * [currency 金额格式化函数]
     * @param  {[type]} value    [传进来的值]
     * @param  {[type]} currency [货币符号]
     * @param  {[type]} decimals [小数位数]
     * @return {[type]}          [description]
     */
    export function currency (value, currency, decimals) {
      value = parseFloat(value)
      if (!isFinite(value) || (!value && value !== 0)) return ''
      currency = currency != null ? currency : '$'
      decimals = decimals != null ? decimals : 2
      var stringified = Math.abs(value).toFixed(decimals)
      var _int = decimals
        ? stringified.slice(0, -1 - decimals)
        : stringified
      var i = _int.length % 3
      var head = i > 0
        ? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
        : ''
      var _float = decimals
        ? stringified.slice(-1 - decimals)
        : ''
      var sign = value < 0 ? '-' : ''
      return sign + currency + head +
        _int.slice(i).replace(digitsRE, '$1,') +
        _float
    }
    

    引用(全局):import {currency} from './util/currency' Vue.filter("currency",currency)
    引用(局部):import {currency} from './util/currency'
    // filters:{ // 定义局部过滤器
    // currency:currency // currency.js传过来的本就是函数
    // },
    用法(局部): {{totalPrice | currency('$')}}
    用法(全局): {{totalPrice | currency()}}

    日期插件:

    /**
     * Created by jacksoft on 17/4/26.
     */
    Date.prototype.Format = function (fmt) {
      var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      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;
    }
    module.exports = {};
    
  • 相关阅读:
    centos mysql 编译安装
    vsftp配置主动模式和被动模式
    vim中选择匹配文本删除技巧
    解决zabbix图中出现中文乱码问题
    zabbix图中出现中文乱码问题
    Centos下Yum安装PHP5.5,5.6,7.0
    一款点击图片进行无限循环的jquery手风琴特效
    一款兼容IE6并带有多图横向滚动的jquery特效
    一款jQuery满屏自适应焦点图切换特效
    一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6
  • 原文地址:https://www.cnblogs.com/vientiane/p/9938615.html
Copyright © 2011-2022 走看看