常规功能和模块自定义系统 (cfcmms)—056 extjs6带分节符的数值字段和百分比字段
来源:互联网 发布:es海量数据分析 编辑:程序博客网 时间:2024/06/06 05:32
056 extjs6带分节符的数值字段和百分比字段
Extjs中的数值控件NumberField中没有加入显示分节符的功能,在研究了其NumberField的源码后,对其类进行了重写,加入了可以把数值分节显示的功能和录入百分比的功能。设置的:
items : [ {xtype : 'numberfield',fieldLabel : '分节显示数值',allowSection : true,decimalPrecision : 2,decimalSeparator : '.',zeroDisplayNone : false,allowBlank : false,selectOnFocus : true,name : 'aa'}, {xtype : 'numberfield',fieldLabel : '百分比',allowSection : true,zeroDisplayNone : true,isPercent : true,decimalPrecision : 4,step : 0.0001,name : 'aa111',}, {xtype : 'numberfield',fieldLabel : '默认数值字段',decimalPrecision : 2,selectOnFocus : true,name : 'aab'} ]
具体显示如下:
源码是一个overrides的类,覆写了一些方法。
Ext.define('overrides.form.field.Number',{uses : [ 'Ext.form.field.Number' ],override : 'Ext.form.field.Number',fieldStyle : "text-align:right",/** * 数值是否加入分节符,是以逗号分隔比如: 123,452,000.00 */allowSection : false,/** * 数值分节符,为逗号(不要修改) */sectionSeparator : ',',/** * 零值是否显示,如果设置为true,则 0 值不显示。不显示的时候如果该字段不能为空,则通不过检测 */zeroDisplayNone : false,/** * 是否是百分比 * * true : 数值0.56 会显示成 56% , 提交的时候仍然是0.56 * * 小数位数是指相对于原始值的,0.56的话小数位置是2,显示56%; 0.5678的小数位置为4,显示56.78% * */isPercent : false,/** * 百分比符号,为%(不要修改) */percentSign : '%',/** * 对于百分比的数值,以下二个设置,都是对应于原始的小数值,因此对于12.34%,应设置成下面的样式: * * decimalPrecision : 4, * * step : 0.0001, * * 对于 23%,应设置成 * * decimalPrecision : 2, * * step : 0.01, * */initComponent : function() {var me = this;if (me.isPercent)me.allowSection = true;if (me.allowSection)me.baseChars = me.baseChars + me.sectionSeparator;me.callParent();},/** * 输入的数值转换成值 */rawToValue : function(rawValue) {var value = this.fixPrecision(this.parseValue(rawValue));if (value === null) {value = rawValue || null;}if (this.isPercent)value /= 100.;return value;},/** * 字符转换成录入的值 */valueToRaw : function(value) {var me = this, decimalSeparator = me.decimalSeparator;value = me.parseValue(value);value = me.fixPrecision(value);if (me.isPercent)value *= 100;value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));if (this.allowSection)value = isNaN(value) ? '' : me.valueToRawWithSection(value).replace('.', decimalSeparator);elsevalue = isNaN(value) ? '' : String(value).replace('.',decimalSeparator);return value;},/** * 将数值转换成有分节的字答卷串,百分比的话,加入 ' %' */valueToRawWithSection : function(value) {var me = this;if (me.zeroDisplayNone && !value)return '';if (this.allowDecimals) {if (me.isPercent)// 这里已经是除了100的数值,在转换的时候精度减少2位value = value.toFixed(Math.max(0, me.decimalPrecision - 2));elsevalue = value.toFixed(me.decimalPrecision);}value = String(value);var ps = value.split('.'), whole = ps[0], sub = ps[1] ? '.' + ps[1]: '', r = /(\d+)(\d{3})/;while (r.test(whole)) {whole = whole.replace(r, '$1' + me.sectionSeparator + '$2');}value = whole + sub;if (me.isPercent)value += (' ' + me.percentSign);return value;},getErrors : function(value) {value = arguments.length > 0 ? value.replace(/,/g, '').replace('%', ''): this.processRawValue(this.getRawValue());return this.callParent([ value ]);},getSubmitValue : function() {var me = this, value = me.callParent();if (!me.submitLocaleSeparator) {value = value.replace(me.decimalSeparator, '.');}if (me.isPercent) {// 如果是百分比,提交的数据除以100value = me.parseValue(value) / 100.;value = String(value);}return value;},parseValue : function(value) {// 取消掉字符串里的 ,% 这二个字符。value = parseFloat(String(value).replace(this.decimalSeparator, '.').replace(/,/g, '').replace('%', ''));return isNaN(value) ? null : value;},processRawValue : function(value) {var me = this, stripRe = me.stripCharsRe, mod, newValue;// 下面一句不加的话 bind 在有分节符或%的时候将不能正常工作value = value.replace(/,/g, '').replace('%', '');if (stripRe) {if (!stripRe.global) {mod = 'g';mod += (stripRe.ignoreCase) ? 'i' : '';mod += (stripRe.multiline) ? 'm' : '';stripRe = new RegExp(stripRe.source, mod);}newValue = value.replace(stripRe, '');if (newValue !== value) {// 这一句要去掉,不然分节符就没有了// me.setRawValue(newValue);if (me.lastValue === value) {me.lastValue = newValue;}value = newValue;}}return value;}});
1 0
- 常规功能和模块自定义系统 (cfcmms)—056 extjs6带分节符的数值字段和百分比字段
- 常规功能和模块自定义系统 (cfcmms)—016模块字段的定义
- 常规功能和模块自定义系统 (cfcmms)—051一对多(OneToMany)字段的加入
- 常规功能和模块自定义系统 (cfcmms)—012Extjs6的开发模式和发布模式
- 常规功能和模块自定义系统 (cfcmms)—055 extjs6的DateTimeField
- 常规功能和模块自定义系统 (cfcmms)—041模块附加字段和新的Grid方案设计器
- 常规功能和模块自定义系统 (cfcmms)—013给Extjs6加入多界面主题(Theme)
- 常规功能和模块自定义系统 (cfcmms)—053把一对多(OneToMany)字段的grid加入到form中
- 常规功能和模块自定义系统 (cfcmms)—024自定义grid列(8子模块聚合字段)
- 常规功能和模块自定义系统(cfcmms)—001序言
- 常规功能和模块自定义系统(cfcmms)—002目录
- 常规功能和模块自定义系统(cfcmms)—003概述
- 常规功能和模块自定义系统 (cfcmms)—043模块导航功能的重构(1)
- 常规功能和模块自定义系统 (cfcmms)—044模块导航功能的重构(2)
- 常规功能和模块自定义系统 (cfcmms)—042模块Grid的排序和筛选
- 常规功能和模块自定义系统 (cfcmms)—007Extjs的配置文件和自定义ui
- 常规功能和模块自定义系统 (cfcmms)—057 最新版的自定义系统演示
- 常规功能和模块自定义系统 (cfcmms)—036模块记录(数据)的权限设计(1)
- 软件各版本的意思
- N-Queens II
- 字符串匹配算法之:有限状态自动机
- eclipse和Android studio分别导入Library的方法
- broadcast receiver 接收设备重启意图( boot_completed Broadcast Intent)而重启定时器
- 常规功能和模块自定义系统 (cfcmms)—056 extjs6带分节符的数值字段和百分比字段
- poj 1811 (prime test)
- C#控制台基础 字符串虽然是引用类型,但是相互赋值 不产生相互联系
- 机器学习笔记(十三)——隐马尔科夫模型
- 多态
- C++Primer(第二章2.5总计 处理类型)
- codeforces 707 c Pythagorean Triples 数论+勾股数
- 离开HK后的第一篇所感--重生
- Android系统篇之----解读AMS远端服务调用机制以及Activity的启动流程