yvanui.yvnumber.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * yvan.yvnumber.js
  3. * @author luoyifan
  4. * 2019-07-14 11:14:00
  5. */
  6. 'use strict';
  7. (function ($) {
  8. function init(target) {
  9. var state = $.data(target, 'yvnumber');
  10. var option = state.options;
  11. var $dom = $(target);
  12. var jqxOpt = {
  13. width: option.width,
  14. height: option.height,
  15. };
  16. $dom.jqxInput(jqxOpt);
  17. $dom.attr('type', "number");
  18. $dom.addClass('yvgrid-numberbox');
  19. var re;
  20. if (!option.precision) {
  21. //不需要小数
  22. re = new RegExp("^(\\-)*(\\d+).*$", "g");
  23. $dom.on('input propertychange', function (event) {
  24. this.value = this.value.replace(re, '$1$2');
  25. if ($.type(option.onChange) === 'function') {
  26. option.onChange.call(this, this.value, event);
  27. }
  28. });
  29. } else {
  30. //需要指定位数的小数
  31. re = new RegExp("^(\\-)*(\\d+)\\.(\\d{0," + option.precision + "}).*$", "g");
  32. $dom.on('input propertychange', function (event) {
  33. this.value = this.value.replace(re, '$1$2.$3');
  34. if ($.type(option.onChange) === 'function') {
  35. option.onChange.call(this, this.value, event);
  36. }
  37. });
  38. }
  39. }
  40. $.fn.yvnumber = function (options, param) {
  41. if (typeof options == 'string') {
  42. var method = $.fn.yvnumber.methods[options];
  43. if (method) {
  44. var args = [this];
  45. for (var i = 1; i < arguments.length; i++) {
  46. args.push(arguments[i]);
  47. }
  48. return method.apply(this, args);
  49. } else {
  50. console.error('not found method:' + options);
  51. }
  52. }
  53. options = options || {};
  54. return this.each(function () {
  55. var state = $.data(this, 'yvnumber');
  56. if (state) {
  57. $.extend(state.options, options);
  58. } else {
  59. $.data(this, 'yvnumber', {
  60. options: $.extend({}, $.fn.yvnumber.defaults, options)
  61. });
  62. }
  63. init(this);
  64. });
  65. };
  66. function setValueInner(target, newValue) {
  67. var state = $.data(target, "yvnumber");
  68. var option = state.options;
  69. //option.value = parseFloat(value);
  70. var value = option.parser.call(target, newValue);
  71. //console.log('value:' + newValue + ', parser:' + value + ', precision:' + option.precision);
  72. //var text = option.formatter.call(target, value);
  73. option.value = value;
  74. $(target).jqxInput('val', value);
  75. //$(target).textbox("setText", text).textbox("setValue", value);
  76. //text = option.formatter.call(target, $(target).textbox("getValue"));
  77. //$(target).textbox("setText", text);
  78. }
  79. $.fn.yvnumber.methods = {
  80. getValue: function ($dom) {
  81. return $(this).jqxInput('val');
  82. },
  83. setValue: function (jq, value) {
  84. return jq.each(function () {
  85. setValueInner(this, value);
  86. //return $(this).jqxInput('val', value);
  87. });
  88. },
  89. focus: function ($dom) {
  90. setTimeout(function () {
  91. $dom[0].focus();
  92. }, 200);
  93. },
  94. };
  95. $.fn.power.defaults.xtype.yvnumber = function ($targetDOM, option) {
  96. if (option.name) {
  97. var $dom = $("<input name='" + option.name + "' xtype='yvnumber' />").appendTo($targetDOM);
  98. } else {
  99. $dom = $("<input xtype='yvnumber' />").appendTo($targetDOM);
  100. }
  101. return $dom.yvnumber(option);
  102. };
  103. $.fn.yvnumber.defaults = {
  104. precision: 0,
  105. min: 0,
  106. parser: function (value) {
  107. value = value + "";
  108. var option = $.data(this, 'yvnumber').options;
  109. if (option.prefix) {
  110. value = $.trim(value.replace(new RegExp("\\" + $.trim(option.prefix), "g"), ""));
  111. }
  112. if (option.suffix) {
  113. value = $.trim(value.replace(new RegExp("\\" + $.trim(option.suffix), "g"), ""));
  114. }
  115. if (parseFloat(value) !== option.value) {
  116. if (option.groupSeparator) {
  117. value = $.trim(value.replace(new RegExp("\\" + option.groupSeparator, "g"), ""));
  118. }
  119. if (option.decimalSeparator) {
  120. value = $.trim(value.replace(new RegExp("\\" + option.decimalSeparator, "g"), "."));
  121. }
  122. value = value.replace(/\s/g, "");
  123. }
  124. var val = parseFloat(value).toFixed(option.precision);
  125. if (isNaN(val)) {
  126. val = "";
  127. } else {
  128. if (typeof (option.min) == "number" && val < option.min) {
  129. val = option.min.toFixed(option.precision);
  130. } else {
  131. if (typeof (option.max) == "number" && val > option.max) {
  132. val = option.max.toFixed(option.precision);
  133. }
  134. }
  135. }
  136. return val;
  137. },
  138. formatter: function (value) {
  139. if (!value) {
  140. return value;
  141. }
  142. value = value + "";
  143. var state = $.data(this, 'yvnumber').options;
  144. var s1 = value, s2 = "";
  145. var _1c = value.indexOf(".");
  146. if (_1c >= 0) {
  147. s1 = value.substring(0, _1c);
  148. s2 = value.substring(_1c + 1, value.length);
  149. }
  150. if (state.groupSeparator) {
  151. var p = /(\d+)(\d{3})/;
  152. while (p.test(s1)) {
  153. s1 = s1.replace(p, "$1" + state.groupSeparator + "$2");
  154. }
  155. }
  156. if (s2) {
  157. return state.prefix + s1 + state.decimalSeparator + s2 + state.suffix;
  158. } else {
  159. return state.prefix + s1 + state.suffix;
  160. }
  161. }
  162. };
  163. })(jQuery);