123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /**
- * yvan.yvnumber.js
- * @author luoyifan
- * 2019-07-14 11:14:00
- */
- 'use strict';
- (function ($) {
- function init(target) {
- var state = $.data(target, 'yvnumber');
- var option = state.options;
- var $dom = $(target);
- var jqxOpt = {
- width: option.width,
- height: option.height,
- };
- $dom.jqxInput(jqxOpt);
- $dom.attr('type', "number");
- $dom.addClass('yvgrid-numberbox');
- var re;
- if (!option.precision) {
- //不需要小数
- re = new RegExp("^(\\-)*(\\d+).*$", "g");
- $dom.on('input propertychange', function (event) {
- this.value = this.value.replace(re, '$1$2');
- if ($.type(option.onChange) === 'function') {
- option.onChange.call(this, this.value, event);
- }
- });
- } else {
- //需要指定位数的小数
- re = new RegExp("^(\\-)*(\\d+)\\.(\\d{0," + option.precision + "}).*$", "g");
- $dom.on('input propertychange', function (event) {
- this.value = this.value.replace(re, '$1$2.$3');
- if ($.type(option.onChange) === 'function') {
- option.onChange.call(this, this.value, event);
- }
- });
- }
- }
- $.fn.yvnumber = function (options, param) {
- if (typeof options == 'string') {
- var method = $.fn.yvnumber.methods[options];
- if (method) {
- var args = [this];
- for (var i = 1; i < arguments.length; i++) {
- args.push(arguments[i]);
- }
- return method.apply(this, args);
- } else {
- console.error('not found method:' + options);
- }
- }
- options = options || {};
- return this.each(function () {
- var state = $.data(this, 'yvnumber');
- if (state) {
- $.extend(state.options, options);
- } else {
- $.data(this, 'yvnumber', {
- options: $.extend({}, $.fn.yvnumber.defaults, options)
- });
- }
- init(this);
- });
- };
- function setValueInner(target, newValue) {
- var state = $.data(target, "yvnumber");
- var option = state.options;
- //option.value = parseFloat(value);
- var value = option.parser.call(target, newValue);
- //console.log('value:' + newValue + ', parser:' + value + ', precision:' + option.precision);
- //var text = option.formatter.call(target, value);
- option.value = value;
- $(target).jqxInput('val', value);
- //$(target).textbox("setText", text).textbox("setValue", value);
- //text = option.formatter.call(target, $(target).textbox("getValue"));
- //$(target).textbox("setText", text);
- }
- $.fn.yvnumber.methods = {
- getValue: function ($dom) {
- return $(this).jqxInput('val');
- },
- setValue: function (jq, value) {
- return jq.each(function () {
- setValueInner(this, value);
- //return $(this).jqxInput('val', value);
- });
- },
- focus: function ($dom) {
- setTimeout(function () {
- $dom[0].focus();
- }, 200);
- },
- };
- $.fn.power.defaults.xtype.yvnumber = function ($targetDOM, option) {
- if (option.name) {
- var $dom = $("<input name='" + option.name + "' xtype='yvnumber' />").appendTo($targetDOM);
- } else {
- $dom = $("<input xtype='yvnumber' />").appendTo($targetDOM);
- }
- return $dom.yvnumber(option);
- };
- $.fn.yvnumber.defaults = {
- precision: 0,
- min: 0,
- parser: function (value) {
- value = value + "";
- var option = $.data(this, 'yvnumber').options;
- if (option.prefix) {
- value = $.trim(value.replace(new RegExp("\\" + $.trim(option.prefix), "g"), ""));
- }
- if (option.suffix) {
- value = $.trim(value.replace(new RegExp("\\" + $.trim(option.suffix), "g"), ""));
- }
- if (parseFloat(value) !== option.value) {
- if (option.groupSeparator) {
- value = $.trim(value.replace(new RegExp("\\" + option.groupSeparator, "g"), ""));
- }
- if (option.decimalSeparator) {
- value = $.trim(value.replace(new RegExp("\\" + option.decimalSeparator, "g"), "."));
- }
- value = value.replace(/\s/g, "");
- }
- var val = parseFloat(value).toFixed(option.precision);
- if (isNaN(val)) {
- val = "";
- } else {
- if (typeof (option.min) == "number" && val < option.min) {
- val = option.min.toFixed(option.precision);
- } else {
- if (typeof (option.max) == "number" && val > option.max) {
- val = option.max.toFixed(option.precision);
- }
- }
- }
- return val;
- },
- formatter: function (value) {
- if (!value) {
- return value;
- }
- value = value + "";
- var state = $.data(this, 'yvnumber').options;
- var s1 = value, s2 = "";
- var _1c = value.indexOf(".");
- if (_1c >= 0) {
- s1 = value.substring(0, _1c);
- s2 = value.substring(_1c + 1, value.length);
- }
- if (state.groupSeparator) {
- var p = /(\d+)(\d{3})/;
- while (p.test(s1)) {
- s1 = s1.replace(p, "$1" + state.groupSeparator + "$2");
- }
- }
- if (s2) {
- return state.prefix + s1 + state.decimalSeparator + s2 + state.suffix;
- } else {
- return state.prefix + s1 + state.suffix;
- }
- }
- };
- })(jQuery);
|