miniUI 扩展 金额输入控件,使用千分位隔开。。。

/**
 * miniui扩展组件
 */

if (!window.UserControl) window.UserControl = {};


/**
 * 金额输入控件
 * 2014-10-22
 * www.liuchang.org
 * 显示格式:100,000.00
 * 调用:
 * <input id="amount" name="amount" class="mini-textamount" decimal='2' useThousandsSeparator='false' thousandsSeparator='’' />
 * decimal:精度,默认两位
 * useThousandsSeparator:是否使用千分位,默认true
 * thousandsSeparator:千分位分隔符,默认 ","
 */
UserControl.TextAmount = function () {
    UserControl.TextAmount.superclass.constructor.call(this);
    this.initComponents();
    this.bindEvents();
}

mini.extend(UserControl.TextAmount, mini.TextBox, {
    uiCls: 'mini-textamount',
	decimal : 2, // 精度,默认两位
	value:'0.00', // 默认值
	useThousandsSeparator:true,// 是否使用千分位,默认true
	thousandsSeparator:',',// 千分位分隔符,默认 ","
	
    initComponents: function () {
		this.set({
			decimal:this.decimal,
			useThousandsSeparator:this.useThousandsSeparator,
			thousandsSeparator:this.thousandsSeparator
		});
    },
    
    bindEvents: function () {
		var me = this;
		// 绑定valuechanged事件,对金额格式化
        this.on('valuechanged', function(){
        	var v = me.getValue(),
        		money = 0;
        		
        	// 格式化为数字,并设置精度
        	money = parseFloat(('0' + v).match(/[\d\.]/ig).join('')).toFixed(me.decimal);
        	
        	// 格式化千分位金额格式
        	if(me.useThousandsSeparator){
        		money = money.split('').reverse().join('').replace(/(\d{3}(?=\d)(?!\d+\.|$))/g, '$1' + me.thousandsSeparator).split('').reverse().join('');
        	}
        	
        	me.setValue(money);
        });
    },

    // 重写getValue,去掉金额千分位
    getValue:function(){
        var me = this;
    var reg = "/"+me.thousandsSeparator+"/g";return this.value.replace(eval(reg), '');
    },
    
    // 返回格式化后的金额
    getText:function(){
    	return this.value;
    },
    
    getAttrs: function (el) {
        var attrs = UserControl.TextAmount.superclass.getAttrs.call(this, el);
        mini._ParseInt(el, attrs,
            ['decimal']
        );
        mini._ParseBool(el, attrs,
            ['useThousandsSeparator']
        );
        mini._ParseString(el, attrs,
            ['thousandsSeparator']
        );
        return attrs;
    }

});

mini.regClass(UserControl.TextAmount, "textamount");

 

原博主地址:https://www.liuchang.org/miniui-kuo-zhan-jin-e-shu-ru-kong-jian/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值