dk-number-input.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:数字组件(支持千分位,金额符)
  5. * 2.功能描述:dk-number-input组件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 周兴 2022-6-30 1.00 新建
  9. *******************************************************************************/
  10. const common = require('../../../utils/common.js')
  11. Component({
  12. options: {
  13. addGlobalClass: true,
  14. },
  15. properties: {
  16. titleValue: {
  17. type: String,
  18. value: ''
  19. },
  20. titleColor: {
  21. type: String,
  22. value: '#1B365D'
  23. },
  24. inputColor: {
  25. type: String,
  26. value: '#1B365D'
  27. },
  28. readonly: {
  29. type: Boolean,
  30. value: false
  31. },
  32. /**
  33. * 输入的数据
  34. */
  35. inputValue: {
  36. type: Float32Array,
  37. value: 0,
  38. },
  39. /**
  40. * 是否支持千分位
  41. */
  42. formatThousandth: {
  43. type: Boolean,
  44. value: true
  45. },
  46. /**
  47. * 是否可用
  48. */
  49. disabled: {
  50. type: Boolean,
  51. value: false
  52. },
  53. /**
  54. * 金额符
  55. */
  56. sign: {
  57. type: String,
  58. value: '¥'
  59. },
  60. percent: {
  61. type: String,
  62. value: '%'
  63. },
  64. percentSignFlag: {
  65. type: Boolean,
  66. value: false
  67. },
  68. /**
  69. * 标识的大小
  70. */
  71. signSize: {
  72. type: String,
  73. value: ''
  74. },
  75. /**
  76. * 是否居中
  77. */
  78. center: {
  79. type: String,
  80. value: 'center'
  81. },
  82. /**
  83. * 小数位数
  84. */
  85. digits: {
  86. type: Number,
  87. value: 2
  88. },
  89. /**
  90. * 最大值
  91. */
  92. max: {
  93. type: Number,
  94. value: 10000000
  95. },
  96. /**
  97. * 字体大小
  98. */
  99. fontSize: {
  100. type: Number,
  101. value: 13
  102. },
  103. /**
  104. * 字体大小
  105. */
  106. fontWeight: {
  107. type: String,
  108. value: 'nomal'
  109. },
  110. /**
  111. * 是否是负数处理(输入整数,自动变为负数)
  112. */
  113. negative: {
  114. type: Boolean,
  115. value: false
  116. },
  117. /**
  118. * 提示信息
  119. */
  120. placeholder: {
  121. type: String,
  122. value: ''
  123. },
  124. maxFlag: {
  125. type: Boolean,
  126. value: true
  127. },
  128. dataIndex:{
  129. type:Number,
  130. },
  131. dataIndex2:{
  132. type:Number,
  133. },
  134. },
  135. data: {
  136. focus: false,
  137. displayValue: '',
  138. clearFlag: false,
  139. },
  140. /**
  141. * 组件的方法列表
  142. */
  143. methods: {
  144. /**
  145. * @desc : 光标进入选中
  146. * @author : 周兴
  147. * @date : 2022/6/30 16:16
  148. */
  149. handleInput(e) {
  150. if (this.data.readonly) {
  151. return
  152. }
  153. setTimeout(() => {
  154. this.setData({
  155. inputValue: parseFloat(this.data.inputValue),
  156. displayValue: this.data.inputValue,
  157. focus: true
  158. })
  159. }, 30)
  160. },
  161. /**
  162. * @desc : 输入时数据的处理
  163. * @author : 周兴
  164. * @date : 2022/6/30 16:16
  165. */
  166. bindInput(e) {
  167. // console.log('ddd1',e,this.data.inputValue,e.detail.value.endsWith('.'));
  168. let flag = true;
  169. let values = e.detail.value.split('.')
  170. // 如果输入的是小数点,那么需要判断是否已经有小数点
  171. if (e.detail.value.endsWith('.')) {
  172. // 说明有多个小数点
  173. // console.log('zzz',values,this.data.inputValue,(this.data.inputValue + '').indexOf('.'));
  174. if (values && values.length > 2) {
  175. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  176. return this.data.inputValue;
  177. } else {
  178. // 可能连续输入2个小数点
  179. return this.data.inputValue + '.';
  180. }
  181. }
  182. // 如果inputValue是空,那么当成0处理
  183. if (e.detail.value == '.') {
  184. return '0.';
  185. }
  186. flag = false;
  187. }
  188. //控制小数点后数字的输入
  189. let digits = this.data.digits
  190. if (values && values.length === 2 && values[1].length > digits) {
  191. flag = false;
  192. return this.data.inputValue;
  193. }
  194. if (flag) {
  195. let value = 0;
  196. // 判断是否超过最大值
  197. if (this.data.maxFlag) {
  198. if (e.detail.value > this.data.max) {
  199. value = this.data.max
  200. } else {
  201. value = e.detail.value
  202. }
  203. } else {
  204. value = e.detail.value
  205. }
  206. this.setData({
  207. clearFlag: false,
  208. inputValue: value,
  209. })
  210. this.handleData()
  211. }
  212. },
  213. /**
  214. * @desc : 光标离开处理数据的显示
  215. * @author : 周兴
  216. * @date : 2022/6/30 16:16
  217. */
  218. bindBlur() {
  219. if (this.data.clearFlag) {
  220. this.setData({
  221. inputValue: 0,
  222. displayValue: 0,
  223. clearFlag: false
  224. })
  225. }
  226. // 处理数据
  227. if (this.data.inputValue) {
  228. this.setData({
  229. inputValue: Number(this.data.inputValue)
  230. })
  231. }
  232. // 离开的时候进行数据处理
  233. // this.handleDisplayValue();
  234. this.triggerEvent('triggerBindBlur',{
  235. inputValue:this.data.inputValue,
  236. index:this.data.dataIndex,
  237. index2:this.data.dataIndex2,
  238. })
  239. this.setData({
  240. focus: false
  241. })
  242. },
  243. /**
  244. * @desc : 绑定值
  245. * @author : 周兴
  246. * @date : 2022/6/30 16:16
  247. */
  248. handleData() {
  249. let value = this.data.inputValue;
  250. if (value == null || value == '') {
  251. this.setData({
  252. clearFlag: true
  253. })
  254. value = 0
  255. }
  256. if (isNaN(value)) {
  257. value = 0
  258. }
  259. // 如果负数标识开启
  260. if (this.data.negative) {
  261. if (value > 0) {
  262. value = -1 * value;
  263. }
  264. } else {
  265. if (value < 0) {
  266. value = -1 * value;
  267. }
  268. }
  269. this.triggerEvent('triggerBindValue', {
  270. value: value
  271. })
  272. },
  273. /**
  274. * @desc : 处理数据的显示
  275. * @author : 周兴
  276. * @date : 2022/6/30 16:16
  277. */
  278. handleDisplayValue() {
  279. let inputValue = this.data.inputValue;
  280. let displayValue = ''
  281. // if(inputValue != undefined && inputValue != 0){
  282. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  283. if (inputValue != undefined && inputValue && inputValue != 0) {
  284. // 判断是否有千分位
  285. if (this.data.formatThousandth) {
  286. displayValue = common.toThousandCents(inputValue);
  287. }
  288. // 如果是负数显示
  289. if (this.data.negative && displayValue.indexOf("-") < 0) {
  290. // displayValue = this.data.sign + "-" + displayValue;
  291. displayValue = "-" + displayValue;
  292. } else {
  293. // 显示金额符号
  294. // displayValue = this.data.sign + displayValue;
  295. }
  296. } else {
  297. displayValue = inputValue;
  298. }
  299. this.setData({
  300. displayValue: displayValue
  301. })
  302. },
  303. },
  304. /**
  305. * 组件生命周期
  306. */
  307. lifetimes: {
  308. attached: function () {
  309. //处理数据的显示
  310. this.handleDisplayValue();
  311. },
  312. detached: function () {
  313. // 在组件实例被从页面节点树移除时执行
  314. },
  315. },
  316. })