FormMath.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. if (typeof FormMath == 'undefined') {
  2. FormMath = {};
  3. }
  4. /****************数学统计的扩展方法********************/
  5. FormMath.toNumber = function(x){
  6. if(x === null || x === undefined || x === '')
  7. return '';
  8. if(typeof x == "string"){
  9. x = x.replace(/,/g, "");
  10. }
  11. var match = x.toString().match(/([$|¥])\d+\.?\d*/);
  12. if(match){
  13. x = x.replace(match[1],'');
  14. }
  15. return Number(x);
  16. };
  17. /**
  18. * 返回x的绝对值
  19. * @param {[type]} x [description]
  20. * @return {[type]} [description]
  21. */
  22. FormMath.abs = function(x){
  23. return Math.abs(x);
  24. }
  25. /**
  26. * 把x四舍五入为最接近的整数
  27. * @param {[type]} x [description]
  28. * @return {[type]} [description]
  29. */
  30. FormMath.round = function(x){
  31. return Math.round(x);
  32. }
  33. /**
  34. * 对x进行上舍入,返回等于或者大于x,并且与x最接近的整数
  35. * @param {[type]} x [description]
  36. * @return {[type]} [description]
  37. */
  38. FormMath.ceil = function(x){
  39. return Math.ceil(x);
  40. }
  41. /**
  42. * 对x进行下舍入,返回小于或者等于x,并且与x最接近的整数
  43. * @param {[type]} x [description]
  44. * @return {[type]} [description]
  45. */
  46. FormMath.floor = function(x){
  47. return Math.floor(x);
  48. }
  49. /**
  50. * 返回集合ary中最大的数
  51. * @param {[type]} ary [description]
  52. * @return {[type]} [description]
  53. */
  54. FormMath.max = function(ary){
  55. var tmp,
  56. x = 0,
  57. size = ary.length;
  58. for(var i=0;i<size;i++){
  59. x = FormMath.toNumber(ary[i]);
  60. if(isNaN(x))continue;
  61. if(tmp===undefined){
  62. tmp = x;
  63. }
  64. else{
  65. if(x>tmp)
  66. tmp = x;
  67. }
  68. }
  69. tmp = FormMath.toNumber(tmp);
  70. return tmp;
  71. }
  72. /**
  73. * 返回集合ary中最小的数
  74. * @param {[type]} ary [description]
  75. * @return {[type]} [description]
  76. */
  77. FormMath.min = function(ary){
  78. var tmp,
  79. x = 0,
  80. size = ary.length;
  81. for(var i=0;i<size;i++){
  82. x = FormMath.toNumber(ary[i]);
  83. if(isNaN(x))continue;
  84. if(tmp===undefined){
  85. tmp = x;
  86. }
  87. else{
  88. if(x<tmp)
  89. tmp = x;
  90. }
  91. }
  92. tmp = FormMath.toNumber(tmp);
  93. return tmp;
  94. }
  95. /**
  96. * 返回x的平方根
  97. * @param {[type]} x [description]
  98. * @return {[type]} [description]
  99. */
  100. FormMath.sqrt = function(x){
  101. return Math.sqrt(x);
  102. }
  103. /**
  104. * 获取ary的平均值
  105. * @param {[type]} ary [description]
  106. * @return {[type]} [description]
  107. */
  108. FormMath.average = function(ary){
  109. var tmp,
  110. x = 0,
  111. size = ary.length;
  112. for(var i=0;i<size;i++){
  113. x = FormMath.toNumber(ary[i]);
  114. if(isNaN(x))continue;
  115. if(tmp===undefined){
  116. tmp = x;
  117. }
  118. else{
  119. tmp += x;
  120. }
  121. }
  122. tmp = FormMath.toNumber(tmp/size);
  123. return tmp;
  124. };
  125. /**
  126. * 求ary的和
  127. * @param {[type]} ary [description]
  128. * @return {[type]} [description]
  129. */
  130. FormMath.sum = function(ary){
  131. var tmp,
  132. x = 0,
  133. size = ary.length;
  134. for(var i=0;i<size;i++){
  135. x = FormMath.toNumber(ary[i]);
  136. if(isNaN(x))continue;
  137. if(tmp===undefined){
  138. tmp = x;
  139. }
  140. else{
  141. tmp += x;
  142. }
  143. }
  144. tmp = FormMath.toNumber(tmp);
  145. return tmp;
  146. };
  147. /**
  148. * 返回保留小数点后b位的x的四舍五入值
  149. * @param {[type]} x [description]
  150. * @param {[type]} b [description]
  151. * @return {[type]} [description]
  152. */
  153. FormMath.tofixed = function(x,b){
  154. var tmp = FormMath.toNumber(x);
  155. b = FormMath.toNumber(b);
  156. if(isNaN(tmp)||isNaN(b))return x;
  157. return tmp.toFixed(b);
  158. };
  159. /**
  160. * 将数字转换为人民币大写
  161. * @param {[type]} x [description]
  162. * @return {[type]} [description]
  163. */
  164. FormMath.convertCurrency = function(x){
  165. var tmp = FormMath.toNumber(x);
  166. if(isNaN(tmp))return x;
  167. return $.convertCurrency(tmp);
  168. };
  169. /****************数学统计的逻辑代码********************/
  170. FormMath.doMath = function(){
  171. var valueRange="main";//这个值的范围,main代表主表,其他代表子表的名称
  172. var expRange="main";//运算符的范围,main代表主表,其他代表子表的名称
  173. var name = $(this).attr("name");
  174. if($(this).closest('div[tablename]').length>0){
  175. valueRange=$(this).closest('div[tablename]').attr('tablename');
  176. }
  177. // console.info("当前值改变的字段是:"+name);
  178. $("input[funcexp]").each(function(){
  179. var me = $(this);
  180. if(me.closest('div[tablename]').length>0){
  181. expRange=$(this).closest('div[tablename]').attr('tablename');
  182. }
  183. curName = me.attr("name");
  184. exp = me.attr("funcexp");
  185. // console.info("当前检验的表达式是:"+exp);
  186. if(curName&&curName==name)return true;
  187. if(exp.indexOf(name)==-1||valueRange!=expRange)//值跟exp的范围不一样就直接return
  188. return true;
  189. var value = FormMath.replaceSingleValue(exp,me);
  190. value = FormMath.replaceMultiValue(value);
  191. try{
  192. //计算表达式进行运算
  193. value = eval("("+value+")");
  194. }
  195. catch(e){
  196. return true;
  197. }
  198. if(/^(Infinity|NaN)$/i.test(value))return true;
  199. if(/^[0-9]*$/.test(value)){
  200. value = Number(value);
  201. //按字段的小数点设置处理小数点的问题(四舍五入)
  202. var jsonValidate = me.attr("validate");
  203. if(jsonValidate.length>0){
  204. var jsonObj = eval("("+jsonValidate+")");
  205. if(jsonObj.maxDecimalLen>=0){
  206. value = value.toFixed(jsonObj.maxDecimalLen);
  207. }
  208. }
  209. }
  210. me.val(value).trigger("change");
  211. });
  212. };
  213. FormMath.replaceSingleValue = function(exp,t){
  214. if(!exp)return 0;
  215. var reg = /\{.*?\((\w+)\)\}/g;//名称格式,就是name='fieldname'的格式,不再是name='m:tablename:fieldname'
  216. exp = exp.replace(reg,function(){
  217. var name = arguments[1],
  218. value = 0;
  219. var object;
  220. //子表
  221. if(/^s\:.+$/.test(name)){
  222. var pTr = $(t).closest("[formtype]");
  223. object = $("[name='"+name+"']",pTr);
  224. }
  225. else{
  226. object = $("table.formTable [name='"+name+"']");
  227. }
  228. if(object){
  229. var val = object.val();
  230. val = FormMath.toNumber(val);
  231. if(!isNaN(val)&&val!="")
  232. value = val;
  233. }
  234. return value;
  235. });
  236. return exp;
  237. };
  238. FormMath.repMultiFn = function(){
  239. var name = arguments[1];
  240. var value = [],
  241. object = $("[name='"+name+"']");
  242. $("[name='"+name+"']").each(function(){
  243. var me = $(this);
  244. if(me){
  245. var val=FormMath.toNumber(me.val());
  246. if(!val=='')
  247. value.push(val);
  248. }
  249. });
  250. value = '[' + value.join(',') + ']';
  251. return value;
  252. };
  253. FormMath.replaceMultiValue = function(exp){
  254. if(!exp)return 0;
  255. var reg = /\[.*?\((\w+\:\w+\:\w+)\)\]/g;
  256. exp = exp.replace(reg,FormMath.repMultiFn);
  257. return exp;
  258. };